Based on UDP protocol socket programming

UDP sockets simple example

Server

import socket

server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  # 数据报协议-》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)  # 数据报协议-》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 a link, which end will not be started error

  • UDP protocol is a datagram protocol, will send air time comes header, so the client input empty, the server can also receive

UPD socket problem-free stick package

Server

import socket

server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  # 数据报协议-》udp
server.bind(('127.0.0.1', 8080))

data, client_addr = server.recvfrom(1024)  # b'hello'==>b'h'
print('第一次:', client_addr, data)

data, client_addr = server.recvfrom(1024)  # b'world' =>b'world'
print('第二次:', client_addr, data)
#
# data,client_addr=server.recvfrom(1024)
# print('第三次:',client_addr,data)

server.close()

Client

import socket

client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  # 数据报协议-》udp

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 typically not used for transmission of large data.

  • Although there is no stick package UDP socket problem, but not a substitute for a TCP socket, because UPD protocol has a flaw: If the data is sent on his way, the data is lost, the data is lost, and there will be no such defects TCP protocol , it is generally irrelevant UPD socket user data transmission, e.g. qq chat.

qq chat

  • Since the connectionless UDP, multiple clients can be simultaneously talk communication server

Server

#_*_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)  #买手机
UDP_server_sock.bind(ip_port)

while True:
    qq_msg, addr = UDP_server_sock.recvfrom(1024)
    print('来自[%s:%s]的一条消息:\033[1;44m%s\033[0m' %
          (addr[0], addr[1], qq_msg.decode('utf-8')))
    back_msg = input('回复消息: ').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 = {
    '狗哥alex': ('127.0.0.1', 8081),
    '瞎驴': ('127.0.0.1', 8081),
    '一棵树': ('127.0.0.1', 8081),
    '武大郎': ('127.0.0.1', 8081),
}

while True:
    qq_name = input('请选择聊天对象: ').strip()
    while True:
        msg = input('请输入消息,回车发送: ').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('来自[%s:%s]的一条消息:\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 = {
    '狗哥alex': ('127.0.0.1', 8081),
    '瞎驴': ('127.0.0.1', 8081),
    '一棵树': ('127.0.0.1', 8081),
    '武大郎': ('127.0.0.1', 8081),
}

while True:
    qq_name = input('请选择聊天对象: ').strip()
    while True:
        msg = input('请输入消息,回车发送: ').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('来自[%s:%s]的一条消息:\033[1;44m%s\033[0m' %
              (addr[0], addr[1], back_msg.decode('utf-8')))

UDP_client_socket.close()

operation result

  • The server runs the results

125- based on UDP socket programming - chat 1.png

  • The client runs 1 results

125- based on UDP socket programming - chat 2.png

  • Client 2 run results

125- based on UDP socket programming - chat 3.png

Guess you like

Origin www.cnblogs.com/nickchen121/p/11032116.html