Hi,
Following is the code i have been checking...
Code:
void createsocket()
{
struct sockaddr_in server_addr;
unsigned int port = 0;
char server_ip[255];
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket");
exit(1);
}
memset( &server_addr , 0 , sizeof(server_addr) );
server_addr.sin_family = AF_INET;
CheckIPAddress(server_ip);
CheckPort(&port); //This reads the value from flash file
server_addr.sin_addr.s_addr = inet_addr(server_ip);
server_addr.sin_port = htons(27015); //While hardcoding the port establishs connection
server_addr.sin_port = htons(port); //This gives an error failed to connect
//Now connect to remote server
if(connect( sock , (const struct sockaddr*) &server_addr , sizeof(server_addr) ) < 0)
{
}
}
1. I have a issue while assiging port to htons() ---> if hardcoded it seems to establish the connection.
2. If the same is replace with a read value from a file. Connection established fails.............
Am I doing something wrong with the way the variable is defined.
I am using an unsigned int -----> while the htons() takes unsinged int.
But value read from the file is stored in an int variable.
Is this the problem?
Let me know where i am wrong?
Thanks in advance!!!!!!!