从套接字描述符获取IP地址? [英] Get IP address from socket descriptor?

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

问题描述

我打开了一个 TCP 套接字服务器(我省略了一些东西,它取自 这里

I've opened a TCP socket server (I've omitted a few stuff, it is taken from here

sockfd = socket(p->ai_family, p->ai_socktype,
            p->ai_protocol))

是否可以从 sockfd 获取服务器的 IP 地址?如果不是,我应该去哪里看?

Is it possible to get the IP address of the server from sockfd? If not where should I look?

我想知道服务器的地址(这是在任何客户端连接之前).

I want to know the address of the server (this is before any client connects).

推荐答案

如果你想知道套接字的另一端是谁,你可以使用 getpeername 在 Linux 中.getsockname 会告诉你你是谁.不过,您可以在 bind 时间决定您希望服务器最初位于哪个地址.

If you want to know who's at the other end of your socket you can use getpeername in Linux. getsockname will tell you who you are. You decide what address you want your server to sit on initially though, at bind time.

您可能还会发现这个 SO 问题很有用:将套接字绑定到网络接口

You may also find this SO question useful: bind socket to network interface

还有 W. Richard Stevens 所著的Unix Network Programming, vol 1"一书.

And the book "Unix Network Programming, vol 1", by W. Richard Stevens.

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

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