如果你要使用此代码请先安装PySimpleGUI库
pip3 install PySimpleGUI
如果不成功请参考其他文章
上代码
1.A用户
from threading import Thread
from socket import *
import PySimpleGUI as sg
print('''\033[34m _ _
___| |__ __ _| |_ _ __ ___ ___ _ __ ___
/ __| '_ \ / _` | __| | '__/ _ \ / _ \| '_ ` _ \
| (__| | | | (_| | |_ | | | (_) | (_) | | | | | |
\___|_| |_|\__,_|\__| |_| \___/ \___/|_| |_| |_|
\033[0m''')
print("\033[31m开发人员:\033[0mnew_dreamsea")
print("\033[32m版本信息:1.0\033[0m")
ip=input("请输入\033[35m对方ip\033[0m 列如:localhost\n")#让用户输入对方ip
my_ip=input("请输入\033[36m自己ip\033[0m 列如:localhost\n")#让用户输入自己的ip
port=9013 #对方端口号
s=socket(AF_INET,SOCK_DGRAM)
layout=[[sg.T("确认对方ip"),sg.In("localhost",key="-ip-"),sg.Button("确认对方")],
[sg.T("确认对方ip"),sg.In("localhost",key="-my_ip-"),sg.Button("确认自己")],
[sg.T("显示框"),sg.ML(size=(70,10),
autoscroll=True,
reroute_stdout=True,
enable_events=True,
key="-xi-",
default_text=''
)],
[sg.T("输入框"),sg.ML(size=(70,8),
autoscroll=True,
reroute_stdout=True,
enable_events=True,
key="-sh-",
default_text='')],
[sg.T("请输入昵称"),sg.In(
enable_events=True,
key="-da-",
default_text='')],
[sg.Button("发送")]
]
win=sg.Window("聊天室",layout,keep_on_top=True)
def jieshou():
while 1:
s.bind((my_ip,9011))
msg=s.recv(1024)
xiaoxi=msg.decode()
win["-xi-"].print(xiaoxi,justification='l')
def tuxin():
while 1:
event,zhi=win.read()
if event is None:
break
if event=="发送":
info=zhi["-da-"]+": "+zhi["-sh-"]
win["-xi-"].print(info,justification='r')
s.sendto(info.encode(),(ip,port))
win['-xi-'].update()
win.close()
ts=Thread(target=tuxin)
rt=Thread(target=jieshou)
rt.start()
ts.start()
2.B用户
from threading import Thread
from socket import *
import PySimpleGUI as sg
print('''\033[34m _ _
___| |__ __ _| |_ _ __ ___ ___ _ __ ___
/ __| '_ \ / _` | __| | '__/ _ \ / _ \| '_ ` _ \
| (__| | | | (_| | |_ | | | (_) | (_) | | | | | |
\___|_| |_|\__,_|\__| |_| \___/ \___/|_| |_| |_|
\033[0m''')
print("\033[31m开发人员:\033[0mnew_dreamsea")
print("\033[32m版本信息:1.0\033[0m")
ip=input("请输入\033[35m对方ip\033[0m 列如:localhost\n")#让用户输入对方ip
my_ip=input("请输入\033[36m自己ip\033[0m 列如:localhost\n")#让用户输入自己的ip
port=9011 #对方端口号
s=socket(AF_INET,SOCK_DGRAM)
layout=[[sg.T("确认对方ip"),sg.In("localhost",key="-ip-"),sg.Button("确认对方")],
[sg.T("确认对方ip"),sg.In("localhost",key="-my_ip-"),sg.Button("确认自己")],
[sg.T("显示框"),sg.ML(size=(70,10),
autoscroll=True,
reroute_stdout=True,
enable_events=True,
key="-xi-",
default_text=''
)],
[sg.T("输入框"),sg.ML(size=(70,8),
autoscroll=True,
reroute_stdout=True,
enable_events=True,
key="-sh-",
default_text='')],
[sg.T("请输入昵称"),sg.In(
enable_events=True,
key="-da-",
default_text='')],
[sg.Button("发送")]
]
win=sg.Window("聊天室",layout,keep_on_top=True)
def jieshou():
while 1:
s.bind((my_ip,9013))
msg=s.recv(1024)
xiaoxi=msg.decode()
win["-xi-"].print(xiaoxi,justification='l')
def tuxin():
while 1:
event,zhi=win.read()
if event is None:
break
if event=="发送":
info=zhi["-da-"]+": "+zhi["-sh-"]
win["-xi-"].print(info,justification='r')
s.sendto(info.encode(),(ip,port))
win['-xi-'].update()
win.close()
ts=Thread(target=tuxin)
rt=Thread(target=jieshou)
rt.start()
ts.start()
对于这段代码你需要了解UDP协议,详情参考以下网址
同时此代码还用到了socket 通信
这里简单说一下socket 通信
你的电脑就好比一个大房子,一个端口就像一个开放的大门。外面的信息可以从大门进来,你也可以通过大门发出信息。电脑的IP地址就像房子的位置,端口号就像大门。
ip=input("请输入\033[35m对方ip\033[0m 列如:localhost\n")#让用户输入对方ip
这样确定了对方的房子地址(这里是局域网通信,首先要保证两台电脑连接同一个局域网)
port=9013 #对方端口号
这里我们选用对方电脑的一个端口(一般是其他软件不用的端口,一定要选择空闲的端口,否则无法通讯)
我们还需要选择自己的一个端口作为接受消息的窗口
s=socket(AF_INET,SOCK_DGRAM)#确定使用UDP协议
def jieshou():
while 1:#整个过程放在循环中
s.bind((my_ip,9011))#绑定自己IP
msg=s.recv(1024)#接收不超过1024字节的数据
xiaoxi=msg.decode()#将信息解码
win["-xi-"].print(xiaoxi,justification='l')#打印在图形界面上
同理我们将我们要发的信息编码发送到对方电脑的固定端口上
s.sendto(info.encode(),(ip,port))
最后发送与接收都是同时进行的,所以要使用多线程模块threading
剩下的就是图形化编程了,这个需要另外学习。(可以使用tkinter、QT等)
这里有最简单版本的聊天室,去除了上面的图形化,演示代码的核心部分
from threading import Thread
from socket import *
#接收信息
def recvData():
while 1==1:
msg=s.recv(1024)
print(">>:",msg.decode())
#发送信息
def sendData():
while 1==1:
info=input("<<:")
s.sendto(info.encode(),(ip,port))
#----------------------------
ip=input("请输入对方IP:例如 localhost\n") #对方ip
port=9011 #对方端口号
s=socket(AF_INET,SOCK_DGRAM)
s.bind(("localhost",9012))
#创建线程
tr=Thread(target=recvData)
ts=Thread(target=sendData)
tr.start()
ts.start()
以上内容只是粗略讲述,有不懂的细节请私信我