getpeername()函数
2017-09-02 12:45
295 查看
学unix网络编程时看到这个函数,我百度了一下,先看看百度怎么解释的:获取socket的对方地址。简单明了,accept函数也有这个功能,看代码吧。
[mapan@localhost TCP]$ ls
client.cpp makefile server.cpp
[mapan@localhost TCP]$ cat server.cpp
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#define MAXLINE 4096
int main()
{
int listenfd,connfd;
char ip[30]={0};
socklen_t clilen;
struct sockaddr_in cliaddr,servaddr;
listenfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(8888);
bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
listen(listenfd,5);
clilen=sizeof(cliaddr);
//connfd=accept(listenfd,(struct sockaddr *)&cliaddr,&clilen);
connfd=accept(listenfd,(struct sockaddr *)NULL,NULL);
getpeername(connfd,(struct sockaddr *)&cliaddr,&clilen);
inet_ntop(AF_INET,&cliaddr.sin_addr,ip,sizeof(ip));
printf("%s,%d",ip,ntohs(cliaddr.sin_port));
getchar();
getchar();
close(listenfd);
return 0;
}
[mapan@localhost TCP]$ cat client.cpp
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#define MAXLINE 4096
int main()
{
int sockfd;
struct sockaddr_in servaddr;
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(8888);
servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
int ret=connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
getchar();
getchar();
close(sockfd);
return 0;
}
[mapan@localhost TCP]$ cat makefile
all:server client
server.o:server.cpp
g++ -c server.cpp
client.o:client.cpp
g++ -c client.cpp
server:server.o
g++ -o server server.o
client:client.o
g++ -o client client.o
clean:
rm -f server client *.o
[mapan@localhost TCP]$
编译,运行服务端,再开启另一个窗口运行客户端。可以看到
[mapan@localhost TCP]$ make
g++ -c server.cpp
g++ -o server server.o
g++ -c client.cpp
g++ -o client client.o
[mapan@localhost TCP]$ ./server
127.0.0.1,36046
再查看此时的网络状态
[mapan@localhost ~]$
[mapan@localhost ~]$ netstat -na | grep 8888
tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:8888 127.0.0.1:36046 ESTABLISHED
tcp 0 0 127.0.0.1:36046 127.0.0.1:8888 ESTABLISHED
[mapan@localhost ~]$
结果与我们打印的一致。inet_ntop用来将IP地址在"点分十进制"和"二进制整数"之间转换。
[mapan@localhost TCP]$ ls
client.cpp makefile server.cpp
[mapan@localhost TCP]$ cat server.cpp
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#define MAXLINE 4096
int main()
{
int listenfd,connfd;
char ip[30]={0};
socklen_t clilen;
struct sockaddr_in cliaddr,servaddr;
listenfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(8888);
bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
listen(listenfd,5);
clilen=sizeof(cliaddr);
//connfd=accept(listenfd,(struct sockaddr *)&cliaddr,&clilen);
connfd=accept(listenfd,(struct sockaddr *)NULL,NULL);
getpeername(connfd,(struct sockaddr *)&cliaddr,&clilen);
inet_ntop(AF_INET,&cliaddr.sin_addr,ip,sizeof(ip));
printf("%s,%d",ip,ntohs(cliaddr.sin_port));
getchar();
getchar();
close(listenfd);
return 0;
}
[mapan@localhost TCP]$ cat client.cpp
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#define MAXLINE 4096
int main()
{
int sockfd;
struct sockaddr_in servaddr;
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(8888);
servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
int ret=connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
getchar();
getchar();
close(sockfd);
return 0;
}
[mapan@localhost TCP]$ cat makefile
all:server client
server.o:server.cpp
g++ -c server.cpp
client.o:client.cpp
g++ -c client.cpp
server:server.o
g++ -o server server.o
client:client.o
g++ -o client client.o
clean:
rm -f server client *.o
[mapan@localhost TCP]$
编译,运行服务端,再开启另一个窗口运行客户端。可以看到
[mapan@localhost TCP]$ make
g++ -c server.cpp
g++ -o server server.o
g++ -c client.cpp
g++ -o client client.o
[mapan@localhost TCP]$ ./server
127.0.0.1,36046
再查看此时的网络状态
[mapan@localhost ~]$
[mapan@localhost ~]$ netstat -na | grep 8888
tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:8888 127.0.0.1:36046 ESTABLISHED
tcp 0 0 127.0.0.1:36046 127.0.0.1:8888 ESTABLISHED
[mapan@localhost ~]$
结果与我们打印的一致。inet_ntop用来将IP地址在"点分十进制"和"二进制整数"之间转换。
相关文章推荐
- getsockname()/getpeername()函数第一次被调用得到0.0.0.0结果
- 用getpeername函数获取套接字的地址族
- UNIX网络编程4.7fork和exec函数4.8并发服务器4.9close函数4.10getsockname getpeername
- getsockname和getpeername函数
- GetPeerName函数说明
- perl函数说明(getpeername )
- 关于getsockname()/getpeername()函数第一次被调用得到0.0.0.0结果的说明
- 我的网络编程学习之路——getsockname 和 getpeername 函数
- server端和clien端关于getsockname和getpeername函数的用法
- Unix Network Programming(17)---getsockname 和 getpeername函数
- getsockname和getpeername函数
- UNIX网络编程学习(9)--getsockname和getpeername的用法及实例
- GetModuleFileName函数得到当前应用程序路径
- Windows API一日一练(72)GetUserName函数
- AIX迁移到LINUX后,getpeername产生segmentation fault(SIGSEGV)
- 函数getElementsByClassName
- MFC获得当前应用程序目录的GetCurrentDirectory()和GetModuleFileName()函数
- GetModuleFileName函数
- getsockname与getpeername
- gethostname()函数出错:WSAGetLastError 10093 解决方法