socket programming based on UDP protocol

Catalog Simple example of UDP socket Server side Client UPD socket non stick package problem Server side Client qq chat Server side Client 1 Client 2...
Server side
Server side
Client
Server side
Client 1
Client 2
Operation result

Catalog

Simple example of UDP socket

Server side

import socket server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Datagram Protocol - UDP server.bind(('127.0.0.1', 8080)) while True: data, client_addr = server.recvfrom(1024) print('===>', data, client_addr) server.sendto(data.upper(), client_addr) server.close()
Client
import socket client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Datagram Protocol - UDP while True: msg = input('>>: ').strip() # msg='' client.sendto(msg.encode('utf-8'), ('127.0.0.1', 8080)) data, server_addr = client.recvfrom(1024) print(data) client.close()
  • UDP is unlinked. No error will be reported on which end of UDP is started first.

  • UDP protocol is a datagram protocol. When it is empty, it will also bring its own header. Therefore, if the client input is empty, the server can also receive it.

UPD socket non stick package problem

Server side

import socket server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Datagram protocol server.bind(('127.0.0.1', 8080)) data, client_addr = server.recvfrom(1024) # b'hello'==>b'h' print('For the first time:', client_addr, data) data, client_addr = server.recvfrom(1024) # b'world' =>b'world' print('The second time:', client_addr, data) # # data,client_addr=server.recvfrom(1024) # print('third time: ', client [addr, data) server.close()

Client

import socket client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Datagram protocol client.sendto('hello'.encode('utf-8'), ('127.0.0.1', 8080)) client.sendto('world'.encode('utf-8'), ('127.0.0.1', 8080)) # client.sendto(''.encode('utf-8'),('127.0.0.1',8080)) client.close()
  • UPD protocol is generally not used to transmit big data.

  • Although UDP socket has no packet sticking problem, it can not replace TCP socket, because UPD protocol has a defect: if data is lost on the way to send, data will be lost, while TCP protocol will not have this defect, so generally UPD socket users do not care about data transmission, such as qq chat.

qq chat
  • Since UDP has no connection, multiple clients can communicate with the server at the same time

Server side

#_*_coding:utf-8_*_ __author__ = 'nick' import socket ip_port = ('127.0.0.1', 8081) UDP_server_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #Buy cell phone UDP_server_sock.bind(ip_port) while True: qq_msg, addr = UDP_server_sock.recvfrom(1024) print('Come from[%s:%s]A message from:\033[1;44m%s\033[0m' % (addr[0], addr[1], qq_msg.decode('utf-8'))) back_msg = input('Reply message: ').strip() UDP_server_sock.sendto(back_msg.encode('utf-8'), addr)

Client 1

#_*_coding:utf-8_*_ __author__ = 'nick' import socket BUFSIZE = 1024 UDP_client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) qq_name_dic = { 'Dog elder brother alex': ('127.0.0.1', 8081), 'Blind donkey': ('127.0.0.1', 8081), 'A tree': ('127.0.0.1', 8081), 'Wu Da Long': ('127.0.0.1', 8081), } while True: qq_name = input('Please select a chat object: ').strip() while True: msg = input('Please enter a message,Carriage return: ').strip() if msg == 'quit': break if not msg or not qq_name or qq_name not in qq_name_dic: continue UDP_client_socket.sendto(msg.encode('utf-8'), qq_name_dic[qq_name]) back_msg, addr = UDP_client_socket.recvfrom(BUFSIZE) print('Come from[%s:%s]A message from:\033[1;44m%s\033[0m' % (addr[0], addr[1], back_msg.decode('utf-8'))) UDP_client_socket.close()

Client 2

#_*_coding:utf-8_*_ __author__ = 'nick' import socket BUFSIZE = 1024 UDP_client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) qq_name_dic = { 'Dog elder brother alex': ('127.0.0.1', 8081), 'Blind donkey': ('127.0.0.1', 8081), 'A tree': ('127.0.0.1', 8081), 'Wu Da Long': ('127.0.0.1', 8081), } while True: qq_name = input('Please select a chat object: ').strip() while True: msg = input('Please enter a message,Carriage return: ').strip() if msg == 'quit': break if not msg or not qq_name or qq_name not in qq_name_dic: continue UDP_client_socket.sendto(msg.encode('utf-8'), qq_name_dic[qq_name]) back_msg, addr = UDP_client_socket.recvfrom(BUFSIZE) print('Come from[%s:%s]A message from:\033[1;44m%s\033[0m' % (addr[0], addr[1], back_msg.decode('utf-8'))) UDP_client_socket.close()

Operation result

  • Server operation results

  • Client 1 run results

  • Client 2 run results

2 November 2019, 02:12 | Views: 4664

Add new comment

For adding a comment, please log in
or create account

0 comments