Hi,

I've inherited some code in C that acts as a server and receives requests over a socket. That is all working fine. I wanted to make a very small change to the server code to print out the hostname of the client connection. However, I'm always getting 0.0.0.0 as the answer...

struct sockaddr_in mysocket;
  int length;
  
  
  
 if ((s = accept(ls, (struct sockaddr *) &mysocket, &length)) < 0) {
     perror("accept()");
     exit(1);
 }
 
 printf ( "Connection received from: %s\n", inet_ntoa(mysocket.sin_addr));

Connection received from: 0.0.0.0

Any idea what I'm doing wrong?

Shouldn't there be a little more code in between there?
http://beej.us/guide/bgnet/output/html/syscalls.html#accept

There's plenty of stuff before it and after, but I figured all I needed to show was the accept. (Since the socket is working fine, just thought it was how I was printing or accessing the sin_addr variable...)

I'm no sockets guru by any stretch of the imagination, but for me the devil is usually found in the details that are omitted.

accept() call fills in this structure with the address of the
connecting entity, as known to the underlying protocol. In the case
of AF_UNIX sockets, the peer's address is filled in only if the peer
had done an explicit bind() before doing a connect(). Therefore, for
AF_UNIX sockets, in the common case, when the peer had not done an explicit bind() before doing a connect(), the structure is filled with
a string of nulls for the address.

the address 0.0.0.0 is really all \0 chars

the address 0.0.0.0 is really all \0 chars

jim,

I'm a bit confused. If the client attempts to do a 'bind', won't it get an error that it's already in use?

hi,
1>I think u can use the gethostname() or the getpeername() procedures to get the IP addresses and the user names..
2>this may be round about, there is a cool function which lets u execute shell scripts from within teh C program itself .check the man pages for "system()".. U can "ping" the other machine name(if u know the name that is) through this command.
Hope this works...

hi,
1>I think u can use the gethostname() or the getpeername() procedures to get the IP addresses and the user names..
2>this may be round about, there is a cool function which lets u execute shell scripts from within teh C program itself .check the man pages for "system()".. U can "ping" the other machine name(if u know the name that is) through this command.
Hope this works...

I tried #1, still got 0.0.0.0 with it.. As far as #2, the issue is that I want to know what machine it is, so I don't know what to ping (and in any case, there's no need for me to ping it, since if it's connected to me, I know I can reach it...)

Are u sure about the values u passed as arguments to getpeername? THe socket descriptor should be the one that stores the value accept() returns and not the listening socket and also the address details should be about the connector and not ur machine's..It has to work

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.