getsockname/getpeername使用注意事项
1.得到的结果全是0,可能原因是没有初始化len变量(unp94-96)
struct sockaddr_storage storageaddr;
int len=sizeof(storageaddr);
bzero(&storageaddr,sizeof (storageaddr));
getsockname(sockfd,(SA *)&storageaddr,&len);
if(storageaddr.ss_family==AF_INET){
struct sockaddr_in clientaddr=*(struct sockaddr_in *)&storageaddr;
int clientport=ntohs(clientaddr.sin_port);
printf("port:%d\n",clientport);
int clientintaddr=ntohl(clientaddr.sin_addr.s_addr);
char str[INET_ADDRSTRLEN];
inet_ntop(AF_INET,&clientintaddr,str,sizeof (str));
printf("str:%s\n",str);
}else if(storageaddr.ss_family==AF_INET6){
}