[linux] C language Linux system programming socket echo client

Echo client:

1. The so-called "echo" refers to that the client sends a piece of data to the server, and the server returns the data to the client as is, just like the sound, when encountering obstacles, it will be "bounced back".

2. The client can also use the write() / send() function to send data to the server, and the server can also use the read() / recv() function to receive data

 

#define BUF_SIZE 100

1. The preprocessing command begins

2. Define is a macro definition. When compiling and preprocessing, macro substitution / macro expansion will be performed on the macro name, that is, replace it

 

scanf("%s", bufSend);

1. When scanf() reads a space, it considers that a string input ends

2. You can use gets() to replace

echo_server.c

 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define BUF_SIZE 100
int main(){
        //Create socket
        int serv_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        //Put socket and IP,Port binding
        struct sockaddr_in serv_addr;
        memset(&serv_addr, 0, sizeof(serv_addr));  //Each byte is filled with 0
        serv_addr.sin_family = AF_INET;  //use IPv4 address
        serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");  //concrete IP address
        serv_addr.sin_port = htons(1234);  //port
        bind(serv_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
        //Enter the listening state and wait for the user to initiate the request
        listen(serv_sock, 20);
        //Receive client requests
        struct sockaddr_in clnt_addr;
        socklen_t clnt_addr_size = sizeof(clnt_addr);
        int clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_addr, &clnt_addr_si
ze);

        char buffer[BUF_SIZE];  //buffer
        int strLen = read(clnt_sock, buffer, BUF_SIZE);  //Receive data from client
        write(clnt_sock, buffer,sizeof(buffer));
        //Close socket
        close(clnt_sock);
        close(serv_sock);
        return 0;
}

 

echo_client.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define BUF_SIZE 100
int main(){
        //Create socket
        int sock = socket(AF_INET, SOCK_STREAM, 0); 
        //To the server (specific IP And port)
        struct sockaddr_in serv_addr;
        memset(&serv_addr, 0, sizeof(serv_addr));  //Each byte is filled with 0
        serv_addr.sin_family = AF_INET;  //use IPv4 address
        serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");  //concrete IP address
        serv_addr.sin_port = htons(1234);  //port
        connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));

        //Echo client
        char bufSend[BUF_SIZE];
        printf("Input string: ");
        scanf("%s",bufSend);
        write(sock, bufSend,sizeof(bufSend));

        //Read the data returned by the server
        char buffer[40];
        read(sock, buffer, sizeof(buffer)-1);

        printf("Server return: %s\n", buffer);

        //Close socket
        close(sock);
        return 0;
}

effect:

Use while(1) to let the code go into a loop and listen to the client's requests all the time

echo_server.c

        while(1){
                int clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_addr, &clnt
_addr_size);

                char buffer[BUF_SIZE];  //buffer
                int strLen = read(clnt_sock, buffer, BUF_SIZE);  //Number of incoming clients
according to
                write(clnt_sock, buffer,sizeof(buffer));
                //Close socket
                close(clnt_sock);
        }

echo_client.c

        while(1){
                //Create socket
                int sock = socket(AF_INET, SOCK_STREAM, 0); 
                //To the server (specific IP And port)
                struct sockaddr_in serv_addr;
                memset(&serv_addr, 0, sizeof(serv_addr));  //Each byte is filled with 0
                serv_addr.sin_family = AF_INET;  //use IPv4 address
                serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");  //concrete IP address
                serv_addr.sin_port = htons(1234);  //port
                connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));

                //Echo client
                char bufSend[BUF_SIZE];
                printf("Input string: ");
                scanf("%s",bufSend);
                write(sock, bufSend,sizeof(bufSend));

                //Read the data returned by the server
                char buffer[40];
                read(sock, buffer, sizeof(buffer)-1);

                printf("Server return: %s\n", buffer);

                //Close socket
                close(sock);}

Effect:

Tags: C socket

Posted on Fri, 15 May 2020 11:31:07 -0400 by mannyee