一个小的UDP通信的Project

    上学期的一个课程设计,要实现管理员端和客户端的一些通信功能,比如管理员要给客户端发消息,客户端要做点啥告诉管理员端之类的。用的模型是网店,客户端要开网店,进别人网店通知别人,买东西之类的。先上源代码

服务器端的:

import socket
import threading


def receiver(s, address):
	while True:
		recvmessage, addr = s.recvfrom(32768)
		data=recvmessage.decode()
		data.strip()
		if data[0]=="1":
			print("your userid is"+data[1:])
		else:
			print(data[1:])


if __name__ == '__main__':
	udp_client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
	address = ('127.0.0.1', 8081)
	usname = input("What is your name?")
	username='1'+usname
	udp_client_socket.sendto(username.encode(), address)
	t = threading.Thread(target=receiver, args=(udp_client_socket, address))
	t.daemon = True
	t.start()
	user_in=""
	shopname=''
	while True:
		action=input("Do what?")
		if action=="shops":
			actions="2"
			udp_client_socket.sendto(actions.encode(), address)
		if action[:5]=="enter":
			shopname=action[6:]
			user_in=shopname
			action='3'+action
			udp_client_socket.sendto(action.encode(), address)
			udp_client_socket.sendto(usname.encode(), address)
		if (action[:5]=="goods"):
			if user_in=='':
				action='4'+action+' '+username[1:]
			else:
				action='4'+action+' '+shopname
			udp_client_socket.sendto(action.encode(), address)
		if (action[:9]=="customers"):
			if user_in=="":
				action='5'+username[1:]
			else:
				action='5'+shopname
			udp_client_socket.sendto(action.encode(), address)
		if (action[:3]=='buy'):
			action='6'+action+'@'+shopname+','+username[1:]
			udp_client_socket.sendto(action.encode(), address)
		if (action[:5]=="leave"):
			user_in=""
			action='7'+action+' '+shopname+','+username[1:]
			udp_client_socket.sendto(action.encode(), address)
		if (action[:8]=="addgoods"):
			action='8'+action+','+username[1:]
			udp_client_socket.sendto(action.encode(), address)
	#------------------------------------#
客户端的:
import socket
import threading


def receiver(s, address):
	while True:
		recvmessage, addr = s.recvfrom(32768)
		data=recvmessage.decode()
		data.strip()
		if data[0]=="1":
			print("your userid is"+data[1:])
		else:
			print(data[1:])


if __name__ == '__main__':
	udp_client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
	address = ('127.0.0.1', 8081)
	usname = input("What is your name?")
	username='1'+usname
	udp_client_socket.sendto(username.encode(), address)
	t = threading.Thread(target=receiver, args=(udp_client_socket, address))
	t.daemon = True
	t.start()
	user_in=""
	shopname=''
	while True:
		action=input("Do what?")
		if action=="shops":
			actions="2"
			udp_client_socket.sendto(actions.encode(), address)
		if action[:5]=="enter":
			shopname=action[6:]
			user_in=shopname
			action='3'+action
			udp_client_socket.sendto(action.encode(), address)
			udp_client_socket.sendto(usname.encode(), address)
		if (action[:5]=="goods"):
			if user_in=='':
				action='4'+action+' '+username[1:]
			else:
				action='4'+action+' '+shopname
			udp_client_socket.sendto(action.encode(), address)
		if (action[:9]=="customers"):
			if user_in=="":
				action='5'+username[1:]
			else:
				action='5'+shopname
			udp_client_socket.sendto(action.encode(), address)
		if (action[:3]=='buy'):
			action='6'+action+'@'+shopname+','+username[1:]
			udp_client_socket.sendto(action.encode(), address)
		if (action[:5]=="leave"):
			user_in=""
			action='7'+action+' '+shopname+','+username[1:]
			udp_client_socket.sendto(action.encode(), address)
		if (action[:8]=="addgoods"):
			action='8'+action+','+username[1:]
			udp_client_socket.sendto(action.encode(), address)
	#------------------------------------#

管理员端的:

import socket
import threading
def receiver(s, address):
	while True:
		recvmessage, addr = s.recvfrom(32768)
		data=recvmessage.decode()
		data.strip()
		if data[0]!='0':
			print(data[1:])

if __name__ == '__main__':
	udp_client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
	address = ('127.0.0.1', 8081)
	usname = 'admin'
	username='1'+usname
	udp_client_socket.sendto(username.encode(), address)
	t = threading.Thread(target=receiver, args=(udp_client_socket, address))
	t.daemon = True
	t.start()
	user_in=""
	shopname=''
	while True:
		action=input("Do what?")
		if action[:11]=="opennewshop":
			action="A"+action
			udp_client_socket.sendto(action.encode(), address)
		if action[:3]=="msg":
			action='9'+action
			udp_client_socket.sendto(action.encode(), address)
		if action=="shops":
			actions="2"
			udp_client_socket.sendto(actions.encode(), address)
		if action[:5]=="enter":
			shopname=action[6:]
			user_in=shopname
			action='3'+action
			udp_client_socket.sendto(action.encode(), address)
			udp_client_socket.sendto(usname.encode(), address)
		if (action[:5]=="goods"):
			if user_in=='':
				action='4'+action+' '+username[1:]
			else:
				action='4'+action+' '+shopname
			udp_client_socket.sendto(action.encode(), address)
		if (action[:9]=="customers"):
			action='5'+shopname
			udp_client_socket.sendto(action.encode(), address)
		if (action[:5]=="leave"):
			user_in=""
			action='7'+action+' '+shopname+','+username[1:]
			udp_client_socket.sendto(action.encode(), address)
		if (action[:5]=="users"):
			action='B'+action
			udp_client_socket.sendto(action.encode(), address)
		if (action[:9]=='closeshop'):
			action='C'+action
			udp_client_socket.sendto(action.encode(), address)
	#------------------------------------#

    思路是 先用十六进制的1-C定义好行为是什么,比如要添加商品就是【8addgoodsxxx】,然后发送信息给服务器端,服务器端再将其转交给管理员端,商品数据都是储存在管理员端本地的,最后由管理员端直接告诉客户端添加成功。

同时多进程也是必须的,因为要一边监听一边处理数据。UDP还是比较简单的,监听着端口就行了,也不用connect啥的。

    没啥技术含量,都是体力活。然而上学期网络课成绩很烂,我估计是这个project没有而是直接弹出,测试的时候一旦输入有误就得重启。时间比较紧,本来想用pyqt做一个图形化界面出来,但是后来觉得图形化界面也只是体力活,与这门课程没什么关系。

    至于pyqt写图形化界面的,先在qt designer里面写一个form.ui,然后用库把他转成form.py,然后import一下就行。写ui的时候就把槽函数写好,这样后面写起来比较简单。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值