Results 1 to 5 of 5

Thread: assign sin_family a string in C programming

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    assign sin_family a string in C programming

    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
    steve

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: assign sin_family a string in C programming

    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
    steve

  3. #3
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: assign sin_family a string in C programming

    This:
    C Code:
    1. (int) *domain;
    Will give you the ASCII value of the first character in the char-pointer. Thats not what you want is it? 65 is the ascii value of 'A'.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: assign sin_family a string in C programming

    I found the value on this site

    http://www.freepascal.org/docs-html/...s/af_inet.html

    Thanks,
    steve

  5. #5
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: assign sin_family a string in C programming

    So you've solved your problem?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width