linux uses socket to realize simple network programming

brief introduction

Socket is an interprocess communication mechanism (socket IPC) under Linux. As mentioned earlier, using socket IPC can enable communication between applications on different hosts (network communication). Of course, it can also be different applications on the same host. Socket IPC usually uses the mode of client < - > server to complete communication. Mu lt iple clients can connect to the server at the same time to complete data interaction with the server.
The kernel provides socket interface to the application layer. For application developers, we only need to call socket interface to develop our own application! Socket is an intermediate software abstraction layer for communication between application layer and TCP/IP protocol. It is a group of interfaces. In the design mode, socket is actually a facade mode. It hides the complex TCP/IP protocol behind the socket interface. For users, a group of simple interfaces is all, allowing the socket to organize data to comply with the specified protocol. Therefore, we do not need to deeply understand various complex TCP/IP protocols such as tcp/udp. Socket has been encapsulated for us. We only need to follow the provisions of socket to program, and the written program naturally follows the tcp/udp standard.
At present, the mainstream programming in the network uses socket for programming, because it is simple and easy to use. It is also a standard (BSD socket) and can be easily transplanted on different platforms. For example, if your application is written based on socket interface, it can be transplanted to any platform that implements BSD socket standard, such as LwIP, which is compatible with BSD socket; Another example is Windows, which also implements a socket interface based on socket. Even in domestic operating systems, such as RT thread, it also implements the socket interface of BSD socket standard.

Code implementation (routine)

Server side program

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

#define SERVER_PORT 8888 / / select an uncommon port number
int main(void)
{
    struct sockaddr_in server_addr = {0};
    struct sockaddr_in client_addr = {0};
    char ip_str[20] = {0};
    int sockfd, connfd;
    int addrlen = sizeof(client_addr);
    char recvbuf[512];
    int ret;
    /* Open the socket and get the socket descriptor */
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (0 > sockfd) {
        perror("socket error");
        exit(EXIT_FAILURE);
    }
    /* Binds the socket to the specified port number */
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    server_addr.sin_port = htons(SERVER_PORT);
    ret = bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
    if (0 > ret) {
        perror("bind error");
        close(sockfd);
        exit(EXIT_FAILURE);
    }
    /* Put the server into listening state */
    ret = listen(sockfd, 50);
    if (0 > ret) {
        perror("listen error");
        close(sockfd);
        exit(EXIT_FAILURE);
    }
    /* Blocking waiting for client connections */
    connfd = accept(sockfd, (struct sockaddr *)&client_addr, &addrlen);
    if (0 > connfd) {
        perror("accept error");
        close(sockfd);
        exit(EXIT_FAILURE);
    }
    printf("Client access...\n");
    inet_ntop(AF_INET, &client_addr.sin_addr.s_addr, ip_str, sizeof(ip_str));
    printf("Client host IP address: %s\n", ip_str);
    printf("The port number of the client process: %d\n", client_addr.sin_port);
    /* Receive data sent by the client */
    for ( ; ; ) {
        // Receive buffer reset
    memset(recvbuf, 0x0, sizeof(recvbuf));
    // Read data
    ret = recv(connfd, recvbuf, sizeof(recvbuf), 0);
    if(0 >= ret) {
        perror("recv error");
        close(connfd);
        break;
    }
    // Print the read data as a string
    printf("from client: %s\n", recvbuf);
    // If "exit" is read, close the socket and exit the program
    if (0 == strncmp("exit", recvbuf, 4)) {
        printf("server exit...\n");
        close(connfd);
        break;
    }
    }
    /* Close socket */
    close(sockfd);
    exit(EXIT_SUCCESS);
}

Client program

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define SERVER_PORT 8888 / / the port number of the server
#define SERVER_IP "192.168.103.107" / / the IP address of the server
int main(void)
{
    struct sockaddr_in server_addr = {0};
    char buf[512];
    int sockfd;
    int ret;
    /* Open the socket and get the socket descriptor */
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (0 > sockfd) {
        perror("socket error");
        exit(EXIT_FAILURE);
    }
    /* Call connect to connect to the remote server */
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(SERVER_PORT); //Port number
    inet_pton(AF_INET, SERVER_IP, &server_addr.sin_addr);//IP address
    ret = connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
    if (0 > ret) {
        perror("connect error");
        close(sockfd);
        exit(EXIT_FAILURE);
    }
    printf("Server connection succeeded...\n\n");
    /* Send data to server */
    for ( ; ; ) {
    // Clear buffer
    memset(buf, 0x0, sizeof(buf));
    // Receive string data entered by the user
    printf("Please enter a string: ");
    fgets(buf, sizeof(buf), stdin);
    // Send the data entered by the user to the server
    ret = send(sockfd, buf, strlen(buf), 0);
    if(0 > ret){
        perror("send error");
        break;
    }
    //Enter "exit" to exit the loop
    if(0 == strncmp(buf, "exit", 4))
        break;
    }
    close(sockfd);
    exit(EXIT_SUCCESS);
}

Tags: Linux network server

Posted on Tue, 26 Oct 2021 04:31:07 -0400 by fxmzb123