gpt4 book ai didi

linux - 在服务器端,获取客户端连接的服务器IP

转载 作者:太空宇宙 更新时间:2023-11-04 05:42:36 25 4
gpt4 key购买 nike

服务器有多个接口(interface)。服务器绑定(bind)到 0.0.0.0,这意味着它正在监听它拥有的所有接口(interface)。因此,客户端可以使用这些服务器 IP 之一进行连接。那么,在服务器端如何获取客户端连接的IP。

最佳答案

accept()返回的套接字上使用getsockname(),它将绑定(bind)到您的服务器用于与接受的客户端通信的本地地址。

NAME
getsockname — get socket name
LIBRARY
Standard C Library (libc, -lc)
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>

int
getsockname(int s, struct sockaddr * restrict name,
socklen_t * restrict namelen);

DESCRIPTION
The getsockname() system call returns the current name for the specified
socket. The namelen argument should be initialized to indicate the
amount of space pointed to by name. On return it contains the actual
size of the name returned (in bytes).

关于linux - 在服务器端,获取客户端连接的服务器IP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45934711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com