views:

53

answers:

3

Hi everyone,

I'm a socket programming newbie. Here's a snippet:

struct sockaddr_storage client_addr;

...

client_addr_size = sizeof(client_addr);
client_socket = accept( server_socket,
    (struct sockaddr *)&client_addr, &client_addr_size );

...

result = inet_ntop( AF_INET,
    &((struct sockaddr_in *)&client_addr)->sin_addr,
    client_addr_str, sizeof(client_addr_str) );

I'm working as a server. Whenever the client connects the address I get is 0.0.0.0 regardless from the host. Can anybody explain, what I'm doing wrong?

Thanks.

A: 

Can you show a bit more code...what IP address/service are you trying to connect to?

The clue is in the IP address itself, 0.0.0.0, commonly a situation where a network interface has no IP address assigned, possibly looking for a DHCP server to renew/accept a DHCP lease from somewhere..

I am shooting myself in the foot as you have not provided enough information and hence would be deemed unfair to do so and get downvoted as a result as this answer does not satisfy your question!!

tommieb75
He's not connecting *to* anything; he's acting as a server and accepting connections.
Tyler McHenry
A: 
Nikolai N Fetissov
+2  A: 

Check client_addr.ss_family - it may be returning an AF_INET6 family address.

caf
Yes, that was right. I've fixed the problem. Yet another one arouse. I use getaddrinfo function with AI_PASSIVE in hints.The address I later bind my socket to is :: . Is it okay?
Negai
Yes, that's fine (as long as the socket is still an `AF_INET6` one).
caf