Linux網路編程之TCP通訊

來源:互聯網
上載者:User

用戶端代碼

client.cpp

#include <stdlib.h>#include <sys/types.h>#include <netinet/in.h>#include <sys/socket.h>#include <arpa/inet.h>#include <unistd.h>#include <stdio.h>int main() {//建立一個socketint sock_fd = socket(AF_INET, SOCK_STREAM, 0);//建立好地址struct sockaddr_in address;address.sin_family = AF_INET;address.sin_port = htons(1991); //連接埠和伺服器端對應address.sin_addr.s_addr = inet_addr("127.0.0.1");//開始串連伺服器int result = connect(sock_fd, (struct sockaddr *) &address, sizeof(address));if(result == -1){perror("connect failed: ");exit(1);}char ch = 'A';//給伺服器發送 一個字元write( sock_fd, &ch, 1);printf("client says: %c\n", ch);// 讀取一個字元read(sock_fd, &ch, 1);printf("get char from server:%c\n", ch);//關掉串連close(sock_fd);return 0;}

伺服器端

server.cpp

#include <stdlib.h>#include <sys/types.h>#include <netinet/in.h>#include <sys/socket.h>#include <arpa/inet.h>#include <unistd.h>#include <stdio.h>int main() {//用 SOCK_STREAM 標識建立 TCP串連int sockfd = socket(AF_INET, SOCK_STREAM, 0);struct sockaddr_in server_addr;server_addr.sin_family = AF_INET;server_addr.sin_port = htons(1991); //需要轉化位元組序//需要把點分10進位的地址轉化/* * Name: inet_addr Prototype: uint32_t inet_addr (const char *name) Description: This function converts the IPv4 Internet host address name from the standard numbers-and-dots notation into binary data. If the input is not valid, inet_addr returns INADDR_NONE. This is an obsolete interface to inet_aton, described immediately above. It is obsolete because INADDR_NONE is a valid address (255.255.255.255), and inet_aton provides a cleaner way to indicate error return. */server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");/*綁定*/bind(sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr));/* * Name: listen Prototype: int listen (int socket, int n) Description: The listen function enables the socket socket to accept connections, thus making it a server socket. The argument n specifies the length of the queue for pending connections. When the queue fills, new clients attempting to connect fail with ECONNREFUSED until the server calls accept to accept a connection from the queue. The listen function returns 0 on success and -1 on failure. The following errno error conditions are defined for this function: */listen(sockfd, 10);char ch;struct sockaddr_in client_addr;socklen_t len = sizeof(client_addr);//socklen_t len = 0;while(1){int client_sockfd;printf("server waiting: \n");client_sockfd = accept(sockfd, (struct sockaddr *) &client_addr, &len);//唯讀取一個字元read(client_sockfd, &ch, 1);printf("get char from client: %c\n", ch);++ch;//把該字元 +1後再發回給用戶端write(client_sockfd, &ch, 1);close(client_sockfd);}//printf("%0x", server_addr.sin_addr.s_addr);return 0;}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.