Maksim Skurydzin
Maksim Skurydzin

Reputation: 10541

Strange client's address returned throug accept(..) function

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.

Upvotes: 1

Views: 424

Answers (3)

caf
caf

Reputation: 239051

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

Upvotes: 2

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84169

Just a guess - what's the declaration of client_addr_str? If it's char* then sizeof(client_addr_str) would return size of pointer (4 or 8, depending on 32- or 64-bit platform.) Try the following:

char client_addr_str[INET_ADDRSTRLEN];
if ( inet_ntop( AF_INET,
    &((struct sockaddr_in *)&client_addr)->sin_addr,
    client_addr_str, INET_ADDRSTRLEN ) == NULL )
{
    /* complain */
}

Upvotes: 0

t0mm13b
t0mm13b

Reputation: 34592

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!!

Upvotes: 0

Related Questions