|
-
Apr 14th, 2008, 03:03 AM
#1
Thread Starter
Frenzied Member
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
-
Apr 14th, 2008, 03:19 AM
#2
Thread Starter
Frenzied Member
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
-
Apr 14th, 2008, 12:00 PM
#3
Re: assign sin_family a string in C programming
This:
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'.
-
Apr 15th, 2008, 08:48 AM
#4
Thread Starter
Frenzied Member
Re: assign sin_family a string in C programming
-
Apr 16th, 2008, 06:00 PM
#5
Re: assign sin_family a string in C programming
So you've solved your problem?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|