如何从sockaddr获取IP地址 [英] How to get IP address from sockaddr

查看:1017
本文介绍了如何从sockaddr获取IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试在调用accept后获取客户端的ip地址。这是我到目前为止,但我只是最终得到一些长的数字,显然不是一个IP地址。可能是错误的?

I want to try and get the ip address of a client after calling accept. This is what I have so far, but I just end up getting some long number that is clearly not an ip address. What could be wrong?

int tcp_sock = socket(AF_INET, SOCK_STREAM, 0);

sockaddr_in client;
client.sin_family = AF_INET;
socklen_t c_len = sizeof(client);

int acc_tcp_sock = accept(tcp_sock, (sockaddr*)&client, &c_len);
cout << "Connected to: " << client.sin_addr.s_addr << endl;


推荐答案

IP地址,整数形式(一个IP地址只是一个整数,毕竟,当我们拆分八位字节并将其转换为点符号时,人们更容易使用)。

That long number is the IP address, in integer form (an IP address is just an integer, after all; it's just easier for people to use when we split the octets apart and put it into dot notation).

您可以使用 inet_ntoa 将整数值转换为标准点符号。

You can use inet_ntoa to convert the integer value to standard dot notation.

这篇关于如何从sockaddr获取IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆