getsockname-获取本地地址:比如,在绑定的时候设置端口号为0由系统自动选择端口绑定,或者使用了INADDR_ANY通配所有地址的情况下,后面需要用到具体的地址和端口,就可以用getsockname获取地址信息: getpeername-获取建立连接的对端的地址和端口: 下面为源码分析: /* * Get the local address ('name') of a socket object. Move the obtained * name to user space. */ SY…
网络编程里经常需要获得主机的相关信息,下面围绕相关的函数以及用到的结构来说明. 获得主机名:int gethostname( char FAR *name, //[out] Pointer to a buffer that receives the local host name. int namelen //[in] Length of the buffer. );返回0表示成功,失败返回SOCKET_ERROR,错误代码通过调用WSAGetLastError查看 根据主机名获得主机信息:st…
int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen); getsockname() returns the current address to which the socket sockfd is bound,in the buffer pointed to by addr. The addrlen argument should be initialized to indicate the amount…