K
Karim
Hi,
I am trying to make a simple client/server aplication under unix. and
the connect() call always fails for some reason. The client and server
are running on the same machine. Connection to the server via telnet
works perfectly.
Here is a piece of my code:
int main (int argc,char * argv[])
{
int i, s, len;
struct sockaddr_in serverAddr = {0};
struct hostent* pHost =0;
struct hostent *host;
struct in_addr addr;
inet_aton("127.0.0.1", &addr);
pHost = gethostbyaddr(&addr, sizeof(addr), PF_INET);
//pHost = gethostbyname("localhost") ; //this one fails too
if (!pHost)
{
fprintf(stderr,"pHost \n");
fflush(stderr);
return OK;
}
if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
fprintf(stderr,"socket \n");
fflush(stderr);
return OK;
}
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(PORT);
memcpy(&serverAddr.sin_addr.s_addr,pHost->h_addr_list[0],pHost->h_length);
if (serverAddr.sin_addr.s_addr)
{
fprintf (stderr,"connect args: %d , %u , %lu
",s,serverAddr.sin_port,serverAddr.sin_addr.s_addr);
fflush(stderr);
}
else
{
fprintf(stderr,"its null\n");
fflush(stderr);
}
if (connect(s, (struct sockaddr *)&serverAddr,sizeof(serverAddr) ) ==
-1) {
fprintf(stderr,"connect failed again \n");
fflush(stderr);
return OK;
}
the output is always : connect args: 3 , 47635 , 16777343 connect
failed again
I tried using gethostByAddr for 127.0.0.1 and it also failed. Also I
tried gethostname and passing the result to gethostbyname and it also
failed.
Any advices are greately appreciated.
ps. this is a line from my ./etc/hosts
127.0.0.1 localhost.localdomain localhost
Thanks alot.
I am trying to make a simple client/server aplication under unix. and
the connect() call always fails for some reason. The client and server
are running on the same machine. Connection to the server via telnet
works perfectly.
Here is a piece of my code:
int main (int argc,char * argv[])
{
int i, s, len;
struct sockaddr_in serverAddr = {0};
struct hostent* pHost =0;
struct hostent *host;
struct in_addr addr;
inet_aton("127.0.0.1", &addr);
pHost = gethostbyaddr(&addr, sizeof(addr), PF_INET);
//pHost = gethostbyname("localhost") ; //this one fails too
if (!pHost)
{
fprintf(stderr,"pHost \n");
fflush(stderr);
return OK;
}
if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
fprintf(stderr,"socket \n");
fflush(stderr);
return OK;
}
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(PORT);
memcpy(&serverAddr.sin_addr.s_addr,pHost->h_addr_list[0],pHost->h_length);
if (serverAddr.sin_addr.s_addr)
{
fprintf (stderr,"connect args: %d , %u , %lu
",s,serverAddr.sin_port,serverAddr.sin_addr.s_addr);
fflush(stderr);
}
else
{
fprintf(stderr,"its null\n");
fflush(stderr);
}
if (connect(s, (struct sockaddr *)&serverAddr,sizeof(serverAddr) ) ==
-1) {
fprintf(stderr,"connect failed again \n");
fflush(stderr);
return OK;
}
the output is always : connect args: 3 , 47635 , 16777343 connect
failed again
I tried using gethostByAddr for 127.0.0.1 and it also failed. Also I
tried gethostname and passing the result to gethostbyname and it also
failed.
Any advices are greately appreciated.
ps. this is a line from my ./etc/hosts
127.0.0.1 localhost.localdomain localhost
Thanks alot.