标签:
#include <stdio.h> #include <sys/socket.h> #include <sys/types.h> #include <netdb.h> #include <netinet/in.h> #include <string.h> #include <stdlib.h> const char *getIPAddrbyHostname(const char *hostname) { static char s[128]; memset(s, 0, sizeof(s)); struct hostent *h; h = gethostbyname(hostname); strcpy(s, inet_ntoa(*((struct in_addr *) h->h_addr))); return s; }
const char *getIPAddrbyaddr_in(struct sockaddr_in *client_addr) { return inet_ntoa(client_addr->sin_addr); } void sockaddr_toa(const struct sockaddr_in *addr, char *IPAddr) { unsigned char *p = (unsigned char *)&(addr->sin_addr.s_addr); sprintf(IPAddr, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]); }
int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen); 得到远端sockaddr int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen); 得到自身sockaddr
标签:
原文地址:http://www.cnblogs.com/shichuan/p/4496323.html