You can understand it in vernacular, "six word true formula" socket

All communication software and online games we see must realize network communication between two terminals, such as:

1. Web surfing refers to the communication between the person operating the web page and the server deployed on the web page;

2. King glory is that 10 players communicate with the game server in pairs. If player 1 uses skills, the computer of player 1 first tells the server through the network, and then the server tells 2345678910;

3. Wechat means that all users communicate with the wechat server in pairs. The server receives your message and then forwards it to others.

It is very troublesome to realize network communication on different types of hosts and different types of operating systems.

Fortunately, all network communication protocols are the same and are based on the five layer model:







The professionals corresponding to these five layers are: physical layer, data link layer, network layer - > network communication personnel; Transport layer - > kernel programmer; Application layer - > application programmer.

For a programmer who writes application software, he only needs to care about the network communication on the "application layer". In order to realize network communication, they need to write some "network communication related codes", which are socket s.

In a word, socket makes it easy for programmers to realize network communication.

Without socket s, these programmers have to care about the implementation of the "transport layer", that is, they have to study the knowledge related to the kernel (linux kernel, window kernel, IOS kernel).

With socket, it's easy. Programmers only need to remember the "six word formula" -- binding prison, receiving and sending:

Binding: the server creates a socket to bind the IP address and port of the local machine, and the code is bind(address, port);

Monitor: the server monitors which clients are connected. The code is listen();

Receiving: the server starts to wait to accept the connection of the client, and each time it accepts a connection, it returns the IP and port of a client, with the code accept();

Connect: the client connects to the server through IP and port, with code connect(address, port);

Send: after the client and server are connected, they can send information to each other with the code send(data);

Receiving: after the client and the server are connected, they can receive the information sent by the other party recv().

With socket, programmers don't need to care about operations such as TCP triple handshake when writing code, because it has been quietly hidden in the reception and connection in the six word true formula!



Relationship between socket, transport layer and application layer:

Socket is a set of api, which does not belong to the transport layer or application layer. In short, socket is a layer of interface encapsulated by the transport layer kernel code according to the "six word truth formula" of binding, monitoring and receiving; The application layer, such as Http, is a special communication mechanism implemented by socket.


The following is a simple communication between a local server and a client socket in python:

import socket
import threading

def handle_client(c, addr):
    print(addr, "connected.")
    n = 0

    while True:
        # receive
        data = c.recv(1024)
        if not data:
        n = n + 1
        print(addr, n)  
        # send out  

# Create a new socket s,with It's syntax sugar when the code leaves with Called automatically when s.close()
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    # Bind Represents any network card on the host
    s.bind(("", 1234))
    # monitor

# Simple version, single thread+Receive primary data
    # c, addr = s.accept()
    # data = c.recv(1024)
    # if not data:
    #         break
    #     n = n + 1
    #     print(addr, n)    
    #     c.sendall(data)

# Complex version, multithreading+Always receiving data
    while True:
        # Accept connection
        c, addr = s.accept()
        # Create a thread to handle client connections
        t = threading.Thread(target=handle_client, args=(c, addr))
import socket
import time
# Create a new socket s,with It's syntax sugar when the code leaves with Called automatically when s.close()
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    # connect
    s.connect(("", 1234))

    # Simple version, send a piece of data
    s.sendall(b"Hello, Kael2")
    data = s.recv(1024)
    print("Received:", repr(data))

    # Complex version, send a piece of data every 1 second
    # while True:
    #     s.sendall(b"Hello, Kael")
    #     data = s.recv(1024)
    #     print("Received:", repr(data))
    #     time.sleep(1)

Run server Py and client Py, you can realize a simple network communication.


reference material:

Added by tasairis on Sun, 09 Jan 2022 08:22:20 +0200