定义如下
#include<sys/socket.h>
int getsockname(int sockfd, struct sockaddr *localaddr, socklen_t *addrlen);
int getpeername(int sockfd, struct sockaddr *peeraddr, socklen_t *addrlen);
//得到客户端ip以及端口号信息
getpeername(connectd,(struct sockaddr *)&client,&len);
printf("ip=%s\n",inet_ntoa(client.sin_addr));
printf("port=%d\n",ntohs(client.sin_port));
Reference
https://blog.csdn.net/workformywork/article/details/24554813