getpeername()函数
生活随笔
收集整理的這篇文章主要介紹了
getpeername()函数
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
? ? ? ? ? ? ? ?學(xué)unix網(wǎng)絡(luò)編程時(shí)看到這個(gè)函數(shù),我百度了一下,先看看百度怎么解釋的:獲取socket的對(duì)方地址。簡(jiǎn)單明了,accept函數(shù)也有這個(gè)功能,看代碼吧。
?
[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]$
編譯,運(yùn)行服務(wù)端,再開(kāi)啟另一個(gè)窗口運(yùn)行客戶(hù)端。可以看到
?
?
[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再查看此時(shí)的網(wǎng)絡(luò)狀態(tài)
?
?
[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 ~]$?
結(jié)果與我們打印的一致。inet_ntop用來(lái)將IP地址在"點(diǎn)分十進(jìn)制"和"二進(jìn)制整數(shù)"之間轉(zhuǎn)換。
?
?
?
?
?
?
?
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專(zhuān)家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的getpeername()函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: memset的妙用
- 下一篇: socket实现进程间通信