On the python-socket programming

1. What is a socket?

  socket socket is the English name, we know the TCP / IP protocol suite system, the state is divided into four state network application layer, transport layer, network layer, physical layer and so on, while the socket is closely related to the transport layer its main achievement of the protocol is TCP and UDP. To-end communications transport layer, therefore, each of the transport layer connection has two ends. So, what transport layer connection endpoint is it? Not the host , not the host's IP address , not the application process, nor is the transport layer protocol port. Transport layer connection endpoint called socket (socket). According to the definition of RFC793: port number spliced to form the socket IP address. The so-called socket, in fact, is a communication endpoint, each socket has a socket number, IP address of the host and comprises a 16-bit host port number, i.e. the form (host IP address: port number) . For example, if the IP address is 210.37.145.1, and port number is 23, then the socket is obtained (210.37.145.1:23).
In short, the socket Socket = (IP address: port number), the socket representation is written after the IP address of the upper port of the decimal point, by a colon or a comma separated. Each transport layer connection endpoints uniquely two ends of the communication (i.e., two sockets) are determined.
Sockets can be seen as a two endpoint applications in communication networks, each communication connection. A piece of information communication, wherein a network application to be transmitted in a Socket host writes it in the Socket Socket transmits this information through a transmission medium to the network interface card in another host, so that this information can be conveyed to other programs. Thus, the data transmission between the two is accomplished through the application socket.
In web application design, since TCP / IP is encapsulated in the core of the operating system, if the application uses TCP / IP, the system may be provided by TCP / IP to implement a programming interface. In the Windows environment, network application programming interface called Windows Socket. To support the user to develop application-oriented communication program, most systems provide a set of TCP or UDP based application programming interface (the API), the interface is usually in the form of a set of functions, also referred to as a socket (Socket ).

1. socket programming flowchart substantially

     

 2. Use the socket and multi-threaded chat thread communication

  Service-Terminal:

import socket
import threading

HOST, PORT = "localhost", 8020
address = (HOST, PORT)

# socket.AF_INET代表IPV4, socket.AF_INET6代表IPV6
# socket.SOCK_STREAM代表TCP, SOCK_DGRAM代表UDP
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(address)

server.listen()

def handlesock(sock, addr):
    while True:
        data = sock.recv(1024).decode("utf-8")
        if data == "exit":
             BREAK 
        Print ( " received from the client [{0}] of the message content is [{}. 1] " .format (addr, Data)) 
        Response = INPUT ( " Reply [{0}]: \ T " . . format (addr)) encode ( " utf8 " ) 
        sock.send (the Response) 

the while True: 
    our sock, addr = server.accept () 
    cur_sock = threading.Thread (target = handlesock, args = (our sock, addr)) 
    cur_sock. start ()

 

  Client:

socket Import 

Client = socket.socket (socket.AF_INET, socket.SOCK_STREAM) 
the client.connect (( " localhost " , 8020 )) 

the while True: 
    the Response = the INPUT ( " server replies: " ) .encode ( " utf8 " ) 
    Client. Send (Response) 
    IF Response == " Exit " :
         BREAK 
    Data = client.recv ( 1024 ) .decode ( " UTF8 " ) 
    Print ( " message is received from the server:% S " % data)

client.close()

  Explanation:

Guess you like

Origin www.cnblogs.com/kisun168/p/11221420.html