TCP concurrent server based on Multithreading

Implementation steps of multithreaded TCP concurrent server:

1. Create a socket


2. Bind the IP address and port information to the socket, and use the bind() function
1)IP address


2)bind

3. Set the maximum number of connections allowed, and use the function listen()

4. Wait for the connection request from the client and use the function accept()

5. Send and receive data. Use the function recv() / sendto(), or read(),write()
6. close the network connection

Start: creating files

/*DATA:         2018-1-17
 *AUTHOR:       wg
 *DESCRIPTION:  Multithreaded TCP server
 *  1´╝îint socket(int domain,int type,int protocol);
 *  2, int bind(int sockfd,const struct sockaddr *addr,socklen_t addrlen);
 *  3, int listen(int sockfd,int backlog);
 *  4, accept(int sockfd,struct sockaddr *addr,socklen_t *addrlen);
 *  5, read(),write()
 */
 #include <sys/socket.h>
#include <sys/un.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include<netinet/in.h>
#include<unistd.h>
#include <arpa/inet.h>
#include <pthread.h>

#define MAX_LISTEN 100
int ad[10];
char buf[128]={0};
struct sockaddr_in server_ip, remote_ip;

void *thread_fun(void *arg)
{
        while(1)
        {
        printf("read data from client : %s\n",inet_ntoa(remote_ip.sin_addr));
        memset(buf,0,128);
        read(ad[(int)arg],buf,128);
        printf("buf =%s\n",buf);
        }
return NULL;
}

int main()
{
        int server_len,remote_len;
        int err,sd;
        pthread_t tid[10];
        int i=0;
        
        sd=socket(AF_INET,SOCK_STREAM,0);
        if(sd == -1)
        {
                printf("create socket failure, errno =%d\n",sd);
                close(sd);
                return -1;
        }
        
        server_ip.sin_family =AF_INET;
        server_ip.sin_port = htons(10086);
        server_ip.sin_addr.s_addr =htonl(INADDR_ANY);
        memset(server_ip.sin_zero,0,8);
        
        err=bind(sd,(struct sockaddr *)(&server_ip),sizeof(struct sockaddr));
        if(err==-1)
        {
                printf("bind error,errno=%d\n",err);
                close(sd);
                return err;
        }
        err=listen(sd,MAX_LISTEN);
        if(err == -1)
        {
        printf("listen error ,errno =%d\n",err);
        close(sd);
        return err;
        }
        
        remote_len=sizeof(struct sockaddr);
        
        while(1)//listen n client quest
        {
        ad[i]=accept(sd,(struct sockaddr *)(&remote_ip),&remote_len);
        if(ad[i] !=0 )
        {
                printf("accept error,errno =%d\n",ad[i]);
                close(sd);
                return -2;
        }
        err=pthread_create(&tid[i],NULL,thread_fun,(void*)i);
        if(err !=0)
        {
        printf("create new thread failure\n");
        close(ad[i]);
        }
        ++i;
        }
        close(sd);
        
        return 0;
}

It can repeatedly monitor and receive requests from multiple customer service terminals.

To create a Tcp client:
1. Create a socket and use the function socket()
2. Set the server address and port to connect
3. Connect to the server using the connect() function
4. Send and receive data. Use the functions recv(),sendto(), or write() and read()
5. Close the network connection

Create client.c file

#include <sys/socket.h>
#include <sys/un.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include<netinet/in.h>
#include<unistd.h>
#include <arpa/inet.h>
#include <pthread.h>

char buf[100];
int sd;
struct sockaddr_in server_ip;

void *thread_write(void *arg)
{
    while(1)
    {
        write(sd,"hello",6);
        sleep(1);
    }
    
 }
 
 void *thread_read(void *arg)
 {
    while(1)
    {
        sleep(1);
        memset(buf,0,100);
        read(sd,buf,100);
        printf("client 2 say: %s\n",buf);
    }
}

int main()
{
    int server_len, remote_len;
    pthread_t tid_read,tid_write;
    int err;
    
    sd=socket(AF_INET,SOCK_STREAM,0);
    if(sd == -1)
    {
        printf("create socket failure");
        return sd;
    }
    
    server_ip.sin_family =AF_INET;
    server_ip.sin_port = htons(10086);
    server_ip.sin_addr.s_addr = htonl(INADDR_ANY);
    memcpy(server_ip.sin_zero,0,8);
    
    err = connect(sd,(struct sockaddr *)(&server_ip),sizeof(struct sockaddr));
    if(err == -1)
    {
        printf("connect failure\n");
        close(sd);
        return err;
    }
    
    err = pthread_create(&tid_read,NULL,thread_read,NULL);
    if(err !=0)
    {
        printf("create read thread failure\n");
       close(sd);
       return err;
    }
    err = pthread_create(&tid_write,NULL,thread_write,NULL);
    if(err !=0)
    {
        printf("create write thread failure\n");
       close(sd);
       return err;
    }
    
    pthread_join(&tid_read,NULL);
    pthread_join(&tid_write,NULL);
    
    close(sd);
    return 0;
    
 }

    
    
Published 35 original articles, won praise 12, visited 1509
Private letter follow

Tags: socket network

Posted on Sat, 18 Jan 2020 09:55:44 -0800 by bonaparte