Python stunt--UDP Server and client

Source: Internet
Author: User
Tags bitwise function prototype python script

I spring and Autumn writers:Wasrehpic0x00 Preface

In the previous article "python stunt--TCP server and client", the core protocol TCP of the transport layer is introduced, and the socket module of the Python script is used to demonstrate the communication process between the TCP server and the client.

This article will follow the same pattern, first introduce the transport layer of another core protocol UDP, and then compare the characteristics of TCP and UDP, and finally the use of Python script to demonstrate the UDP server and client communication process.

0x01 UDP Protocol

UDP (user Datagram Protocol, Subscriber Datagram Protocol) is a non-connected, unreliable, datagram-based Transport layer communication protocol.

    • UDP communication process compared with TCP is relatively simple, do not need a complex three times handshake and four waves, the embodiment of no connection;
    • UDP transmission speed is faster than TCP, but easy to drop packets, data arrival order is not guaranteed, lack of congestion control, adhering to the principle of best efforts to deliver, the embodiment of unreliable;
    • The non-connection and unreliable features of UDP are doomed to be unable to adopt the communication mode of byte stream, which can be represented by the "datagram" in the protocol name and "sock_dgram" in the socket type.

To more visually compare the similarities and differences between TCP and UDP, the author organizes them into the following tables:

TCP UDP
Connection mode Connection-oriented (single point of communication) No connection (multipoint communication)
Transmission Reliability Reliable Unreliable
Communication mode Byte stream based Based on datagrams
Header structure Complex (at least 20 bytes) Simple (8 bytes)
Transmission speed Slow Fast
Resource requirements Many Less
Order of arrival Guarantee Not guaranteed
Flow control Yes No
Congestion control Yes No
Application situations Large data transfer Small Data transfer
Supported application-layer protocols Telnet, FTP, SMTP, HTTP DNS, DHCP, TFTP, SNMP
0x02 Network Socket

Network socket is a data flow endpoint of interprocess communication in computer network, and represents an inter-process communication mechanism provided by operating system broadly.

The fundamental premise of interprocess communication (inter-process COMMUNICATION,IPC) is the ability to uniquely label each process. In the inter-process communication of the local host, each process can be uniquely labeled with PID (Process ID), but the PID is unique locally, the PID of different hosts in the network may conflict, so the "IP address + Transport Layer protocol + port number is the only way to identify a process in the network.

Tip: The IP address of the network layer can uniquely indicate a host, and the TCP/UDP protocol and port number of the transport layer can uniquely identify a process for that host. Note that the TCP protocol in the same host can use the same port number as the UDP protocol.

All programming languages that support network communication each provide a set of socket APIs, following Python 3 as an example of how the server interacts with the client to establish a UDP communication connection:

It can be seen that the UDP communication process is much simpler than TCP, the server is less listening and accepting the connection process, and the client is less the process of requesting the connection. The client only needs to know the address of the server, send the data directly to it, and the server also open the door to receive any data sent to the home address.

Tip: Since UDP uses no-connection mode, it is known that the UDP server is not aware of the client's address before receiving the data from the client, so it must be the client to send the data first, the server after the response data. TCP, however, is different when the TCP server accepts the client's connection, either sending the data to the client first or waiting for the client to send the data before responding.

0x03 UDP Server
#!/usr/bin/env python3#-*-coding:utf-8-*-import sockets = Socket.socket (socket.af_inet, socket.) SOCK_DGRAM) S.bind (( "127.0.0.1", 6000)) print (" UDP bound on port 6000 ... ") while true:    data, addr = S.recvfrom (1024"     print ( "Receive from%s:%s"% addr)     if data = = b" Exit ":        s.sendto (b "Good bye!\n", addr)         continue     S.sendto (b "Hello%s!\n"% data, addr)      
    • Line 5: Create the Socket object, the first parameter is socket.af_inet, representing the use of the IPV4 Protocol for network communication, the second parameter is the socket. Sock_dgram, which represents the use of UDP protocol for non-connected network communication.
    • Line 6: Bind the server host address ("127.0.0.1", 6000) to the socket object, which is the UDP 6000 port of the local host.
    • Line 9: Enter the cycle phase of data interaction with the client.
    • Line 10: Receive the data from the client, including the bytes object data, and the client's IP address and port number addr, where addr is a two-tuple (host, port).
    • Line 11: Print receive information indicating that the data is received from a client with an address of addr.
    • Line 12: If the Bytes object is b"exit" , the end response message is sent to the client with the address addr b"Good bye!\n" . After sending, continue to wait for other UDP clients to send the data.
    • Line 15: If the bytes object is not b"exit" , send a greeting response message to the client with the address addr b"Hello %s!\n" , which %s is the bytes object sent by the client. After sending, continue to wait for any UDP client to send the data.

The UDP server does not have to use multi-threading as compared to a TCP server, because it does not need to create an independent connection for each communication process, but instead uses a "ready to go" mode, which once again embodies the non-connectivity of UDP.

0x04 UDP Client
#!/usr/bin/env Python3#-*-Coding:utf-8-*-Import sockets = Socket.socket (socket.af_inet, socket. Sock_dgram) addr = ("127.0.0.1", 6000)while true:data = input (' please input your name: ') if not data: Continue S.sendto (Data.encode (), addr) response, addr = S.recvfrom (1024x768) print (Response.decode ()) c9>if data = = "Exit": Print ("Session is over from theserver%s:%s\n"% addr) Breaks.close () /c12> 
    • Line 5: Create the Socket object, the first parameter is socket.af_inet, representing the use of the IPV4 Protocol for network communication, the second parameter is the socket. Sock_dgram, which represents the use of UDP protocol for non-connected network communication.
    • Line 6: Initialize the address of the UDP server ("127.0.0.1", 6000), which is the UDP 6000 port of the local host.
    • Line 8: Enter the cycle phase of data interaction with the server.
    • Line 9: Requires the user to enter a name.
    • Line 10: When the user's input is empty, the cycle starts again, requiring the user to reenter it.
    • Line 12: When the user's input is non-empty, the string is converted to a bytes object and sent to a UDP server with the address ("127.0.0.1", 6000).
    • Line 13: Response data from the receiving server, including the Bytes object response, and the server's IP address and port number addr, where addr is a two-tuple (host, port).
    • Line 14: Prints the output after converting the Response bytes Object response to a string.
    • Line 15: When the user's input is, the end-of-session information is printed, and the "exit" loop phase of the server interaction data is terminated, closing the socket shortly.
    • Line 19: Closes the socket and no longer sends data to the server.
0x05 UDP interprocess Communication

By naming the UDP server and client scripts separately udp_server.py and udp_client.py then saving them to the desktop, the author will demonstrate with PowerShell under Windows 10.

Tip: To make sure that Python 3 is installed on your computer when the reader is reproducing, note that the default boot path name has been changed by the author python python3 .

Single Server VS multi-client

    1. Run a command in one of the PowerShell python3 ./udp_server.py , the server binds the UDP 6000 port of the host, and prints the information UDP bound on port 6000... , waiting for the client to send the data;
    2. Run the command separately in the other two PowerShell python3 ./udp_client.py and send the string to the Client1 server Client2 ;
    3. The server prints the received information, indicating that the data is received from UDP 63643, 63644 ports, and sends greeting response information to the client respectively;
    4. Client1the client sends an empty string, which is required to be re-entered;
    5. The client Client2 sends the string Alice , receives the server greeting response information, sends the string exit , obtains the server's end response information, finally prints the session end information, terminates the data interaction with the server;
    6. The client Client1 sends a string exit , gets the end response information of the server, and prints the session end information, terminating the data interaction with the server;
    7. The server prints the received information in the order in which the client's data is sent, and continues to wait for any UDP client to send the data.
0x06 Python API referencesocket Module

This section describes the built-in module sockets used in the above code, which is the core module of Python network programming.

Socket () function

The socket () function is used to create a socket object in network traffic. The function prototypes are as follows:

socket.socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None)
    • The family parameter represents the address family, and the default value is af_inet, which is used to IPv4 network traffic, as well as Af_inet6, which is used to IPV6 network traffic. The optional value of the family parameter depends on the native operating system.
    • The type parameter represents the types of sockets, the default value is Sock_stream, which is used for TCP protocol (connection-oriented) network traffic, and the commonly used Sock_dgram, which is used for UDP protocol (no connection) network traffic.
    • The proto parameter represents a socket protocol, the default value is 0, which is generally ignored unless the family parameter is af_can, Proto The parameter needs to be set to Can_raw or can_bcm.
    • The Fileno parameter represents the file descriptor of the socket, the default value is None, and if this parameter is set, the other three parameters are ignored.

After you create a socket object, you use the object's built-in function to complete the network communication process. Note that the "socket" in the following function prototype refers to the socket object, not the socket module above.

Bind () function

The bind () function is used to bind the IP address and port number to the socket object. Note that the socket object must not be bound and the port number is not occupied, otherwise an error will be received. The function prototypes are as follows:

socket.bind(address)
    • The address parameter represents the addresses to be bound by the socket, and the format depends on the socket's family parameter. If the family parameter is af_inet, the address parameter is represented as a two-tuple (host, Port), where host is a string-represented address, and port is the port number represented by an integral type.
SendTo () function

The SendTo () function is used to send data to a remote socket object. Note that this function is used for non-connected communication between UDP processes, and the address of the remote socket is specified in the parameter, so you do not need to connect to the remote socket before using it. In contrast, the connection-oriented communication process between TCP processes requires the use of the Send () function. The function prototypes are as follows:

socket.sendto(bytes[, flags], address)
    • The bytes parameter represents the bytes object data that will be sent. For example, for "hello world!" a string, you need to convert the encode () function to a bytes object for b"hello world!" network transmission.
    • flags Optional parameters are used to set the special function of the sendto () function, with a default value of 0or one or more predefined values separated by a bitwise OR operator | . For details, refer to SendTo (2) in the Unix function manual, the common values of theflags parameter are Msg_oob, Msg_eor, Msg_dontroute, and so on.
    • The address parameter represents the location of the remote socket, and its format depends on the family parameter of the socket. If the family parameter is af_inet, the address parameter is represented as a two-tuple (host, Port), where host is a string-represented address, and port Is the port number represented by an integral type.

The return value of the SendTo () function is the number of bytes of data sent.

Recvfrom () function

The Recvfrom () function is used to receive data from a remote socket object. Note that unlike the sendto () function, the recvfrom () function can be used for both UDP interprocess communication and for TCP interprocess communication. The function prototypes are as follows:

socket.recvfrom(bufsize[, flags])
    • The bufsize parameter represents the maximum number of bytes that a socket can receive data. Note that in order for the hardware device to better match the network transmission, the value of thebufsize parameter is preferably set to a power of 2, such as 4096.
    • flags Optional parameters are used to set the special function of the recv () function, with a default value of 0or one or more predefined values separated by a bitwise OR operator | . For details, refer to Recvfrom (2) in the Unix function manual, the common values of theflags parameter are Msg_oob, Msg_peek, Msg_waitall, and so on.

The return value of the Recvfrom () function is a two-tuple (bytes, address), where bytes is the bytes object data received, and address is the sender's IP address and port number, represented by a two-tuple (host, port). Note that the return value of the recv () function is only bytes object data.

Close () function

The close () function closes the local socket object and frees all resources connected to the socket.

socket.close()
0X07 Summary

This paper introduces the basic knowledge of UDP protocol, compares it with TCP protocol, and then realizes and demonstrates the communication process between UDP server and client by using Python 3, and finally makes reference index of the Python API involved in the script, which helps the reader understand the implementation process.

Thank you for reading, the author's limited level, if there are deficiencies or errors, please understand and inform, hope that their shallow understanding of TCP and UDP, can help readers better understand the transport layer protocol.

For the reference of this article, please go to:

The most complete difference between TCP and UDP
The difference between TCP and UDP
UDP programming-Liaoche's official website

Python stunt--UDP Server and client

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.