Client/Server app under unix

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.
 
K

Keith Thompson

Karim said:
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.
[...]

Try comp.unix.programmer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,491
Latest member
mohitk

Latest Threads

Top