I'm using the connect function (http://linux.die.net/man/2/connect) and it only works sometimes. It used to work correctly all the time before, now it hardly ever works. The code hasn't changed since I first wrote it about 2 weeks ago so the result shouldn't be changing. I'm thinking it has to do with my network. I'm using port 5301 (pretty much chosen at random) so maybe there's some sort of blocking going on? I'm using the local IP 127.0.0.1.
Code for the server:
int connectionID = 0, listenID = 0;
struct sockaddr_in sad;
listenID = socket (AF_INET, SOCK_STREAM, 0);
memset (&sad, 0, sizeof(sad));
sad.sin_family = AF_INET;
sad.sin_addr.s_addr = INADDR_ANY;
sad.sin_port = htons(5301);
bind (listenID, (struct sockaddr *)&sad, sizeof(sad));
Code for the client:
int sockID = 0;
struct sockaddr_in sad;
sockID = socket (AF_INET, SOCK_STREAM, 0);
sad.sin_family = AF_INET;
sad.sin_port = htons(5301);
inet_pton (AF_INET, serverIP, &sad.sin_addr.s_addr);
if (connect (sockID, (struct sockaddr *)&sad, sizeof(sad)) < 0)
{
printf ("Error Connecting to Server\n");
return;
}
The IP is passed in as a parameter