工程内容需要在java和python项目之间进行数据传输
因传输数据量较大,一次通信无法传输完整数据,这里规定一段完整数据是以"news"开头
python服务端
import socket
import threading
def serverinit():
address = ('localhost', 8082)
try:
# 创建socket对象,同时设置通信模式,AF_INET代表IPv4,SOCK_STREAM代表流式socket,使用的是tcp协议
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 防止socket server重启后端口被占用(socket.error: [Errno 98] Address already in use)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# 绑定到我们刚刚设置的ip和端口元组,代表我们的服务运行在本机的8082端口上
server.bind(address)
# 开始监听,5位最大挂起的连接数
server.listen(5)
except socket.error as msg:
print(msg)
print("server start!")
return server
def serverDeal(client,addr):
print("Access client!")
nowData = "receive data..."
while True:
data &#