日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

getpeername()函数

發布時間:2025/6/15 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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()函数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。