By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,443 Members | 1,409 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,443 IT Pros & Developers. It's quick & easy.

Client/Server app under unix

P: n/a
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.

Dec 24 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Karim" <ka**********@gmail.comwrites:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 24 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.