Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string assignment 1

Status
Not open for further replies.

robUK2

Programmer
Mar 10, 2008
57
0
0
TH
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
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top