Linux C++ TCP Socket通訊執行個體

來源:互聯網
上載者:User

標籤:for   技術分享   png   gets   family   code   bin   erro   訊息   

環境:Linux 語言:C++ 通訊方式:TCP

  下面用TCP協議編寫一個簡單的伺服器、用戶端,其中伺服器端一直監聽原生6666號連接埠。如果收到串連請求,將接收請求並接收用戶端發來的訊息;用戶端與伺服器端建立串連並發送一條訊息。

server.cpp
 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<errno.h> 5 #include<sys/types.h> 6 #include<sys/socket.h> 7 #include<netinet/in.h> 8 #include<unistd.h> 9 10 #define MAXLINE 409611 12 int main(int argc, char** argv){13     int  listenfd, connfd;14     struct sockaddr_in  servaddr;15     char  buff[4096];16     int  n;17 18     if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ){19         printf("create socket error: %s(errno: %d)\n",strerror(errno),errno);20         return 0;21     }22 23     memset(&servaddr, 0, sizeof(servaddr));24     servaddr.sin_family = AF_INET;25     servaddr.sin_addr.s_addr = htonl(INADDR_ANY);26     servaddr.sin_port = htons(6666);27 28     if( bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1){29         printf("bind socket error: %s(errno: %d)\n",strerror(errno),errno);30         return 0;31     }32 33     if( listen(listenfd, 10) == -1){34         printf("listen socket error: %s(errno: %d)\n",strerror(errno),errno);35         return 0;36     }37 38     printf("======waiting for client‘s request======\n");39     while(1){40         if( (connfd = accept(listenfd, (struct sockaddr*)NULL, NULL)) == -1){41             printf("accept socket error: %s(errno: %d)",strerror(errno),errno);42             continue;43         }44         n = recv(connfd, buff, MAXLINE, 0);45         buff[n] = ‘\0‘;46         printf("recv msg from client: %s\n", buff);47         close(connfd);48     }49     close(listenfd);50     return 0;51 }
 client.cpp
 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<errno.h> 5 #include<sys/types.h> 6 #include<sys/socket.h> 7 #include<netinet/in.h> 8 #include<arpa/inet.h> 9 #include<unistd.h>10 #define MAXLINE 409611 12 int main(int argc, char** argv){13     int   sockfd, n;14     char  recvline[4096], sendline[4096];15     struct sockaddr_in  servaddr;16 17     if( argc != 2){18         printf("usage: ./client <ipaddress>\n");19         return 0;20     }21 22     if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){23         printf("create socket error: %s(errno: %d)\n", strerror(errno),errno);24         return 0;25     }26 27     memset(&servaddr, 0, sizeof(servaddr));28     servaddr.sin_family = AF_INET;29     servaddr.sin_port = htons(6666);30     if( inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0){31         printf("inet_pton error for %s\n",argv[1]);32         return 0;33     }34 35     if( connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0){36         printf("connect error: %s(errno: %d)\n",strerror(errno),errno);37         return 0;38     }39 40     printf("send msg to server: \n");41     fgets(sendline, 4096, stdin);42     if( send(sockfd, sendline, strlen(sendline), 0) < 0){43         printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);44         return 0;45     }46     close(sockfd);47     return 0;48 }
makefile
 1 all:server client 2 server:server.o 3     g++ -g -o server server.o 4 client:client.o 5     g++ -g -o client client.o 6 server.o:server.cpp 7     g++ -g -c server.cpp 8 client.o:client.cpp 9     g++ -g -c client.cpp10 clean:all11     rm all

   執行make命令後,產生server和client兩個可執行檔。分別開啟兩個終端視窗,一個執行./server命令,一個執行./client 127.0.0.1命令,表示連上原生6666連接埠,執行./server命令的要先執行。執行./client 127.0.0.1命令後,會提示說要發給server的內容,輸入“hello”後,client用戶端執行完畢,這時可以看到server的那個終端視窗輸出“recv msg from client: hello”。繼續執行./client 127.0.0.1命令後,再輸入“haha”,server的終端繼續輸出“recv msg from client: haha”。結果。

TCP協議通訊互動流程:

    

參考文獻:

《後台開發:核心技術與應用實踐》第六章,p206

Linux C++ TCP Socket通訊執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.