Summary and implementation of socket

Introduction to socket

Socket originally means "socket". In the field of computer communication, socket is translated as "socket". It is a convention or a way of communication between computers. Through the socket agreement, a computer can receive data from other computers or send data to other computers.

According to the transmission mode of data, Internet sockets can be divided into two types. When you create a connection through the socket() function, you must tell it which data transmission method to use.

Connection oriented socket

Stream Sockets, also known as "connection oriented sockets", use sock in the code_ Stream means.

SOCK_STREAM is a reliable two-way communication data flow. Data can reach another computer accurately. If it is damaged or lost, it can be sent again.

SOCK_STREAM has the following characteristics:

  • Data will not disappear during transmission;

  • Data is transmitted in sequence;

  • The sending and receiving of data are not synchronous (some tutorials are also called "no data boundary"):

    There is a buffer (i.e. character array) inside the stream format socket, and the data transmitted through the socket will be saved to this buffer. The receiving end does not necessarily read the data immediately after receiving it. As long as the data does not exceed the capacity of the buffer, the receiving end may read it once or several times after the buffer is filled.

    In other words, no matter how many times the data is transmitted, the receiving end only needs to read according to its own requirements, and does not have to read immediately when the data arrives. The transmitter has its own rhythm and the receiver has its own rhythm. They are inconsistent.

Sock can be_ Stream is compared to a conveyor belt. As long as the conveyor belt itself has no problem (no network disconnection), it can ensure that data is not lost; At the same time, the data transmitted later will not arrive first, and the data transmitted earlier will not arrive late, which ensures that the data is transmitted in order.

Why can streaming sockets achieve high-quality data transmission? Because it uses The Transmission Control Protocol (TCP), the TCP protocol will control your data to arrive in order without error.

In "TCP/IP": TCP is used to ensure the correctness of data, and IP (Internet Protocol) is used to control how data reaches its destination from the source, which is often referred to as "routing".

Application scenario: the http protocol used by the browser is based on the connection oriented socket, because the data must be accurate, otherwise the loaded HTML will not be parsed.

Connectionless socket

Datagram Sockets, also known as "connectionless sockets", use sock in the code_ Dgram representation.

The computer only transmits data without data verification. If the data is damaged in transmission or does not reach another computer, there is no way to remedy it. In other words, if the data is wrong, it is wrong and cannot be retransmitted. Because datagram socket does less verification work, it is higher than stream format socket in transmission efficiency.
Sock can be_ Dgram is compared to high-speed mobile motorcycle express, which has the following characteristics:

  • Emphasize fast transmission rather than transmission sequence;
  • The transmitted data may be lost or damaged;
  • Limit the size of data transmitted each time;
  • The sending and receiving of data are synchronous (some tutorials are also called "there is a data boundary").

As we all know, speed is the life of the express industry. There is no need to guarantee the order of two packages sent by motorcycle to the same place, as long as they are delivered to the customer as soon as possible. There is a risk of damage or loss in this way, and the package size is limited. Therefore, if you want to deliver a large number of packages, you have to distribute them.

In addition, if two packages are sent by two motorcycles, the receiver also needs to receive them twice, so "the sending and receiving of data are synchronous"; In other words, the number of times received should be the same as the number of times sent.

In short, the purpose of transmitting data by socket is unreliable and unreliable.

Datagram socket also uses IP protocol for routing, but it does not use TCP protocol, but uses UDP protocol (User Datagram Protocol).

QQ video chat and voice chat use SOCK_DGRAM is used to transmit data, because first of all, it is necessary to ensure the efficiency of communication and minimize the delay, and the correctness of data is secondary. Even if a small part of data is lost, video and audio can be parsed normally, with noise or noise at most, which will not have a real impact on the communication quality.

##Summary of two sockets

The characteristics of the two sockets determine their application scenarios. Some services require high reliability. If the data packet can be delivered completely and correctly, you have to choose a connected socket (TCP service), such as HTTP, FTP, etc; Other services do not need such high reliability. Efficiency and real-time are what they care about. Then you can choose connectionless sockets (UDP services), such as DNS, instant chat tools, etc.

Connection oriented socket flow

At the code level, the socket implementation has a fixed process, as follows:

  • The server and client initialize the socket to get the file descriptor;
  • The server calls bind to bind to the IP address and port;
  • The server calls listen to listen;
  • The server calls accept and waits for the client to connect;
  • The client calls connect and initiates a connection request to the address and port of the server; The server accept s and returns the file descriptor of the socket used for transmission;
  • The client calls write to write data; The server calls read to read data;
  • When the client disconnects, it will call close, so when the server reads the data, it will read the EOF. After processing the data, the server will call close, indicating that the connection is closed.

Simple socket implementation:

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

int main(){
    //Create socket
    int serv_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    //Bind socket to 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");  //Specific 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 a 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_size);

    //Send data to client
    char str[] = "http://c.biancheng.net/socket/";
    write(clnt_sock, str, sizeof(str));
   
    //Close socket
    close(clnt_sock);
    close(serv_sock);

    return 0;
}
// Client code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

int main(){
    //Create socket
    int sock = socket(AF_INET, SOCK_STREAM, 0);

    //Make a request 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");  //Specific IP address
    serv_addr.sin_port = htons(1234);  //port
    connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
   
    //Read the data returned by the server
    char buffer[40];
    read(sock, buffer, sizeof(buffer)-1);
   
    printf("Message form server: %s\n", buffer);
   
    //Close socket
    close(sock);

    return 0;
}

Keywords: C++ network TCP/IP

Added by suzzane2020 on Tue, 08 Feb 2022 09:44:53 +0200