Linux-C language network TCP single communication, multiple communications, and multi-thread communication are gradually realized

1. TCP communication, only send once and end the program

Function description:

1. The server can only connect to one client at a time.
2. The client can only send a message to the server once. After the message is returned, the client and server programs end.
3. The client sends a string to the server, and the server changes the string to uppercase and returns it to the client.

step one:

Compile the client and server source programs in sequence, and then use the ifconfig command to view the virtual machine IP address, which will be used by the client to access the server using this IP address.
Insert image description here

Step 2:

Open two terminals, one for the client and one for the server. Run the server first, then the client. After running both, you can send messages to the server through the client.
Insert image description here

Client source code:

#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#include<stdlib.h>

#define SERVPORT 8080

int main(int argc,int *argv[])
{
    
    
    int sockfd;
    int recv_len;
    struct sockaddr_in servaddr,cliaddr;
    char sendline[100];
    char recvline[100];

    if(argc != 2)
    {
    
    
        printf("need server address\n");
        exit(0);
    }

    sockfd = socket(AF_INET,SOCK_STREAM,0);

    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(SERVPORT);
    servaddr.sin_addr.s_addr = inet_addr(argv[1]);

    connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));

    while(fgets(sendline,100,stdin) != NULL)
    {
    
    
        sendto(sockfd,sendline,strlen(sendline),0,
               (struct sockaddr *)&servaddr,sizeof(servaddr));

        recv_len = recvfrom(sockfd,recvline,100,0,NULL,NULL);

        recvline[recv_len] = '\0';

        fputs(recvline,stdout);

    }

    close(sockfd);

    return 0;
}

Server source code:

#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<string.h>

#include<ctype.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<fcntl.h>

#define SERVPORT 8080

int main(int argc)
{
    
    
    int listenfd,connfd,recv_len,send_len;
    socklen_t clilen;
    struct sockaddr_in servaddr,cliaddr;
    char buff[100];

    listenfd = socket(AF_INET,SOCK_STREAM,0);

    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(SERVPORT);
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

    bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
    printf("bind is sucessifully!\n");

    listen(listenfd,10);

    connfd = accept(listenfd,(struct sockaddr *)&cliaddr,&clilen);
    printf("connect sucessifully!\n");

    recv_len = recv(connfd,buff,100,0);

    buff[recv_len] = '\0';
    printf("received the following:\n");
    printf("%s",buff);

    for(int i=0; i<recv_len; i++)
    {
    
    
        buff[i] = toupper(buff[i]);
    }
    send_len = send(connfd,buff,recv_len,0);

    close(listenfd);
    return 0;
}

2. TCP communication, the server and the client realize multiple communications

Function description:

1. The server can only connect to one client at a time.
2. The client can send messages to the server multiple times until the client sends an empty message, and the client and server programs end.
3. The client sends a string to the server, and the server changes the string to uppercase and returns it to the client.

The steps are the same as the first one (province)

Client source code:

#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
#include<stdlib.h>
#include<arpa/inet.h>
#include<unistd.h>
#define SERVPORT 8080

int main(int argc,char *argv[])
{
    
    
    int sockfd;
    int recv_len;
    struct sockaddr_in servaddr,cliaddr;
    char sendline[100];
    char recvline[100];

    if(argc != 2)
    {
    
    
        printf("need server address\n");
        exit(0);
    }

    sockfd = socket(AF_INET,SOCK_STREAM,0);

    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(SERVPORT);
    servaddr.sin_addr.s_addr = inet_addr(argv[1]);

    connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));

    printf("send to service  :  ");
    while(fgets(sendline,100,stdin) != NULL)
    {
    
    
        send(sockfd,sendline,strlen(sendline),0);

        recv_len = recv(sockfd,recvline,100,0);

        recvline[recv_len] = '\0';
        printf("back from service:  ");
        fputs(recvline,stdout);
        printf("------------------------------\n");
        printf("send to service  :  ");
        //close(sockfd);
        //break;
    }

    close(sockfd);

    return 0;
}

Server source code:

#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<string.h>

#include<ctype.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<fcntl.h>

#define SERVPORT 8080

int main(int argc)
{
    
    
    int listenfd,connfd,recv_len,send_len;
    socklen_t clilen;
    struct sockaddr_in servaddr,cliaddr;
    char buff[100];

    listenfd = socket(AF_INET,SOCK_STREAM,0);

    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(SERVPORT);
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

    bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
    printf("bind is sucessifully! \n");

    listen(listenfd,10);
    printf("Waiting for clients to connect...\n");
    connfd = accept(listenfd,(struct sockaddr *)&cliaddr,&clilen);
    printf("%d connect sucessifully!\n",connfd);

    while(1)
    {
    
    
        recv_len = recv(connfd,buff,100,0);
        if(recv_len == 0)
        {
    
    
            break;
        }
        buff[recv_len] = '\0';
        printf("received the following:\n");
        printf("%s",buff);

        for(int i=0; i<recv_len; i++)
        {
    
    
            buff[i] = toupper(buff[i]);
        }
        send_len = send(connfd,buff,recv_len,0);
    }

    close(listenfd);
    return 0;
}

3. Multi-threading to implement TCP-Socket communication

Function description:

1. The server can connect to multiple clients at the same time.
2. Multiple clients run concurrently and communicate with the server at the same time.
3. The client sends a string to the server, and the server changes the string to uppercase and returns it to the client.
4. When all clients are closed, the server is still running and continues to work when new clients are connected.

step one:

Compile the client and server source programs in sequence, and then use the ifconfig command to view the virtual machine IP address, which will be used by the client to access the server using this IP address.
Insert image description here

Step 2:

Then open three terminals, one for the server and two for the client.
Insert image description here

Step three:

According to the picture below, run the server first and then the client. After everything is running, you can send messages to the server through the client.
Insert image description here

Show results:

Insert image description here

Client source code:

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

#define SERVPORT 8080

int main(int argc,char *argv[])
{
    
    
    int sockfd;
    int recv_len;
    struct sockaddr_in servaddr,cliaddr;
    char sendline[100];
    char recvline[100];

    if(argc != 2)
    {
    
    
        printf("need server address\n");
        exit(0);
    }

    sockfd = socket(AF_INET,SOCK_STREAM,0);

    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(SERVPORT);
    servaddr.sin_addr.s_addr = inet_addr(argv[1]);

    connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));

    printf("send to service  :  ");
    while(fgets(sendline,100,stdin) != NULL)
    {
    
    
        send(sockfd,sendline,strlen(sendline),0);

        recv_len = recv(sockfd,recvline,100,0);

        recvline[recv_len] = '\0'; // 给接收到的结果加上字符串结束符
        printf("back from service:  ");
        fputs(recvline,stdout); //将收到的结果发送到控制台
        printf("------------------------------\n");
        printf("send to service  :  ");
        //close(sockfd);
        //break;
    }
    
    close(sockfd);
    return 0;
}

Server source code

#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<string.h>

#include<ctype.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<pthread.h>

#define SERVPORT 8080
#define CONNECTNUM 10
void thread_fun(void * cconnfd)
{
    
    
    int connfd =*(int *)cconnfd;
    int recv_len,send_len;
    char buff[100];
    
    while(1)
    {
    
    
        recv_len = recv(connfd,buff,100,0);
        if(recv_len == 0)
        {
    
    
            break;
        }
        buff[recv_len] = '\0';
        printf("received from client%d the following:\n",connfd);
        printf("%s",buff);


        for(int i=0; i<recv_len; i++)
        {
    
    
            buff[i] = toupper(buff[i]);
        }
        send_len = send(connfd,buff,recv_len,0);
        printf("have replied client%d ,over. \n ",connfd);
        printf("------------------------------ \n");
    }
    pthread_exit(NULL);
}
int main(int argc)
{
    
    
    int listenfd,connfd,recv_len,send_len;
    socklen_t clilen;
    struct sockaddr_in servaddr,cliaddr;
    char buff[100];
    pthread_t pid[CONNECTNUM];

    listenfd = socket(AF_INET,SOCK_STREAM,0);

    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(SERVPORT);
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

    bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
    printf("bind is sucessifully! \n");

    listen(listenfd,10);
    printf("Waiting for clients to connect...\n");

    for(int j =0; j<CONNECTNUM; j++)
    {
    
    
        connfd = accept(listenfd,(struct sockaddr *)&cliaddr,&clilen);
        printf("%d connect sucessifully!\n",connfd);
        pthread_create(&pid[j],NULL,(void *)thread_fun,&connfd);
    }

    close(listenfd);
    return 0;
}

Guess you like

Origin blog.csdn.net/quxuexi/article/details/125148518