开发者

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.


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


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


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 */
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜