Linux creates a TCP connection process

Steps for Linux to create TCP

TCP programming requires two sets of codes, client and server, and the process of creating TCP is not completely consistent

Server

  1. Create a socket using the socket function
  2. Use the setsockopt function to set socket properties
  3. Use the bind function to bind the IP address and port information to the socket
  4. Use the listen function to listen to the specified port
  5. Use the accept function to receive the connection request from the client
  6. Use send/recv and read/write functions to send and receive data
  7. Use the close function to close the network connection and monitor

client

  1. Create a socket using the socket function
  2. Set socket properties using the setsockopt function
  3. Use the bind function to bind IP address and port information
  4. Set the IP address and port to be connected
  5. Use the connect function to request a connection
  6. Use send/recv and read/write functions to send and receive data
  7. Use the close function to close the network connection

TCP establishment process

insert image description here

sample code

server

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <time.h>
#define MAXSIZE 128

char news[MAXSIZE];
int res;			//用以接收函数返回值

void* pthread_chat(void * arg)		//创建线程用以接收数据
{
    
    
    int confd = *(int *)arg;
    while(1)
    {
    
    
        res  = recv(confd, news, sizeof(news), 0);
        if(res <= 0)
        {
    
    
        perror("recv");
        break;
        }
        printf("The news is: %s\n",news);
        memset(news,0,MAXSIZE);
        send(confd,"OK",2,0);
    }

    printf("One client over\n");
    close(confd);
}

char *Time()					//获取当前时间
{
    
    
	time_t timer;
	struct tm *tblock;
	timer = time(NULL);
	tblock = localtime(&timer);
	return asctime(tblock);
}

void save(char *s)				//储存日志文件
{
    
    
    int fd;
    fd = open("journal",O_RDWR|O_APPEND|O_CREAT);

    if(fd < 0)
        perror("open");
    else
    {
    
    
        char *buf = Time();
        strcat(buf,s);
                
        write(fd,buf,MAXSIZE);
        lseek(fd,0,SEEK_END);
        
        if(res < 0)
            perror("write");
    }
}

int main()
{
    
    
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in saddr, caddr;
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(6666);
    saddr.sin_addr.s_addr = inet_addr("127.0.0.1");

    res = bind(sockfd,(struct sockaddr*)&saddr, sizeof(saddr));
    if(res < 0)
        perror("bind");

    listen(sockfd, 5);			//监听端口

    while(1)
    {
    
    
        int len = sizeof(caddr);
        int confd = accept(sockfd,(struct sockaddr*)&caddr, &len);
        if(confd < 0)
        {
    
    
            perror("accept");
            continue;
        }else
        {
    
    
            save(inet_ntoa(caddr.sin_addr));
        }

        printf("Accept confdis:%d, ip=%s\n",confd,inet_ntoa(caddr.sin_addr));

        pthread_t tid;
        pthread_create(&tid, NULL, pthread_chat, &confd);
    }
}

client

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>  
#define MAXSIZE 128

char news[MAXSIZE];
int res;					//用来接收函数返回值
int main()
{
    
    
    printf("------Welcome join the chat room-----\n");
    printf("If you want to quit,please input --bye--\n");
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
      
    struct sockaddr_in saddr;
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(6666);
    saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  
    int confd = connect(sockfd,(struct sockaddr*)&saddr,sizeof(saddr));
    if(confd < 0)
        perror("connect");
    
    while(1)
    {
    
    
        printf("Please input the news\n");
        fgets(news,MAXSIZE,stdin);
          
        if(strncmp(news,"bye",3) == 0)
        {
    
    
            break;
        }

        send(sockfd, news, strlen(news), 0);
        memset(news,0,MAXSIZE);
        recv(sockfd, news, sizeof(news), 0);
          printf("The serve's news is: %s\n",news);
    }

    close(sockfd);
    exit(0);
}

Please note that because the server uses multi-threaded development, you need to add the -lpthread option when compiling

The effect of the program operation is as follows:
insert image description here

Guess you like

Origin blog.csdn.net/Wangguang_/article/details/117718143