Linux network programming | TCP programming examples

TCP programming example

TCP programming is divided into two parts: client and server. The server first establishes a socket, then binds to the local port, then begins to receive the client's connection request and establishes a connection with it, and finally receives the message sent by the client ; The client calls the connect() function to establish a connection after the socket is established.
The process of using TCP on the server side and the client side is shown below:
Insert picture description here
Server side code:

/*****server.c*****/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define BUFFER_SIZE 128

int main(int argc, char *argv[]){
    
    
	int listenfd, connfd;
	struct sockaddr_in servaddr, cliaddr;
	socklen_t peerlen;
	char[BUFFER_SIZE];

	if(argc < 3){
    
    
		printf("Usage: %s <ip> <port>\n",argv[0]);
		exit(-1);
	}

	if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
    
    		//建立socket连接
		perror("socket");
		exit(-1);
	}
	printf("listenfd = %d\n",listenfd);
	
	//设置sockaddr_in结构体中相关参数
	bzero(&servaddr,szieof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(atoi(argv[2]));
	servaddr.sin_addr.s_addr = inet_addr(argv[1]);
	
	if(bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0){
    
    	//bind()绑定地址信息
		perror("bind");
		exit(-1);
	}
	printf("bind success!\n");
	
	if(listen(listenfd, 10) == -1){
    
    		//listen()设置监听模式
		perror("listen");
		exit(-1);
	}
	printf("Listening...\n");
	
	peerlen = sizeof(cliaddr);		//调用accept()等待客户端的连接
	while(1){
    
    
		if((connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &peerlen)) <0){
    
    
			perror("accept");
			exit(-1);
		}
		
		memset(buf, 0, sizeof(buf));	//调用recv()函数接收客户端发送的数据
		if(recv(connfd, buf, BUFFER_SIZE, 0) == -1){
    
    
			perror("recv");
			exit(-1);
		}
		printf("Received a message: %s\n",buf);
		strcpy(buf, "Welcome to server");
		send(connfd, buf, BUFFER_SIZE, 0);
		close(connfd);
	}
	
	close(listenfd);
	exit(0);
}

Client code:

/*****client.c*****/
//头文件同server.c
#define BUFFER_SIZE 128

int main(int argc, char *argv[]){
    
    
	int sockfd;
	char buf[BUFFER_SIZE] = "Hello Server";
	struct sockaddr_in servaddr;

	if(argc < 3){
    
    
		printf("Usage: %s <ip> <port>\n",argv[0]);
		exit(-1);
	}
	
	if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
    
    		//创建socket
		perror("socket");
		exit(-1);
	}
	
	//设置sockaddr_in结构体中相关参数
	bzero(&servaddr,szieof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(atoi(argv[2]));
	servaddr.sin_addr.s_addr = inet_addr(argv[1]);
	
	if(connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1){
    
    
		perror("connect");		//调用connect()向服务器端建立TCP连接
		exit(-1);
	}
	
	send(sockfd, buf, sizeof(buf), 0);	//发送消息给服务器端
	if(recv(sockfd, buf, sizeof(buf), 0) == -1){
    
    
		perror("recv");
		exit(-1);
	}
	printf("Recv from server: %s\n",buf);
	close(sockfd);
	exit(0);
}

When running, start the server first, then start the client

linux@linux-virtual-machine:~/andy/net$ ./server 192.168.1.100 9999
listenfd = 3
Bind success!
Listening...
Received a message: Hello Server
linux@linux-virtual-machine:~/andy/net$ ./client 192.168.1.100 9999
Recv from server: Welcome to server

Guess you like

Origin blog.csdn.net/Chuangke_Andy/article/details/108428036