Thread: assigning a string

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    assigning a string

    Hello,

    I am doing some socket programming and have created a library.

    Code:
    int CreateSocket(char* domain, char* type, int protocal, unsigned int portNumber, char* IPAddress)
    {
    	struct sockaddr_in serverAddress;
    	int sockfd = 0;
    	int connectionResult = 0;
    
    	char family[10];
    
    	strcpy(family, domain);
    	printf("family = %s", family);
    	printf("Char* domain = %s\n", domain);
    	printf("(int) domain = %d\n", (int) domain);
    	printf("(int) *domain = %d\n", (int) *domain);
    
    	serverAddress.sin_family = *domain; //PROBLEM HERE ASSIGNING THE FAMILY DOMAIN.
     	serverAddress.sin_port = htons(portNumber);
     	serverAddress.sin_addr.s_addr = inet_addr(IPAddress);
    
    	memset(serverAddress.sin_zero, '\0', sizeof(serverAddress.sin_zero));
    	
    	sockfd = socket(PF_INET, SOCK_STREAM, 0);
    	printf("socket(PF_INET, SOCK_STREAM) = %d", sockfd);
    
    	if(sockfd == -1)
    	{
    		WriteErrorLog(1);
    		return -1;
    	}
    	.
            .
            .

    I have above function that will pass the address family (AF_INET) with some other parameters. The parameter name is domain.

    I think the problem is that the AF_INET is a key word so cannot assign a string representative. As you can see from my print outs I have tried many things.

    I need this parameter passed in as the user will either enter the address family or protocol family that they want to use. This will also be the same for the SOCK_STREAM or SOCK_DGRAM.

    Many thanks for any advice,

    Steve

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I think you already have an understanding of why the problem exists. I guess I have to know why you need to use strings for this instead of just using a short value or an int?

  3. #3
    UK2
    Join Date
    Sep 2003
    Posts
    112
    Hello,

    I have just discovered that this works below:

    serverAddress.sin_family = (int) *domain;

    This gives the value of 65 that is assigned to the sin_family.

    I guess the 65 is the value that is assigned to the AF_INET. Correct if I am wrong.

    Steve

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Chars can lead to overflow/underflow problems when not explicitly casted. Though to answer your question, AF_INET is a platform specific value. So I guess it depends on what you are running. Though with a value like 65, I can't imagine a problem such as under/overflow.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by steve1_rm View Post
    Hello,

    I have just discovered that this works below:

    serverAddress.sin_family = (int) *domain;

    This gives the value of 65 that is assigned to the sin_family.

    I guess the 65 is the value that is assigned to the AF_INET. Correct if I am wrong.

    Steve
    Well: you're wrong. 65 is the ASCII code for the letter 'A' (the first letter in "AF_INET").

    I don't know enough about networking to know what sin_family is defined to be. If it's also a char *, then you need to use strcpy to copy it over. If you're supposed to be using the int that AF_INET is #defined to be, then you shouldn't have a string in the first place.

    If worst comes to worst, you may have to parse the string yourself to convert string -> constant.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    ^ Bingo (about the string thing--which I questioned earlier too). I am not even finding a system that defines it as 65.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM