getpeername()函数
生活随笔
收集整理的這篇文章主要介紹了
getpeername()函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? ? ? ? ? ? ?學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 4096int 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 4096int 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 clientserver.o:server.cppg++ -c server.cpp client.o:client.cppg++ -c client.cpp server:server.og++ -o server server.o client:client.og++ -o client client.oclean: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地址在"點分十進制"和"二進制整數"之間轉換。
?
?
?
?
?
?
?
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的getpeername()函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: memset的妙用
- 下一篇: socket实现进程间通信