为编程爱好者分享易语言教程源码的资源网
好用的代理IP,游戏必备 ____广告位招租____ 服务器99/年 ____广告位招租____ ____广告位招租____ 挂机,建站服务器
好用的代理IP,游戏必备 ____广告位招租____ 服务器低至38/年 ____广告位招租____ ____广告位招租____ 挂机,建站服务器

网站首页 > 网络编程 > 其它综合 正文

Python TCP 协议网络编程《三》

三叶资源网 2022-12-18 20:18:50 其它综合 295 ℃ 0 评论

今日主题:在 Python TCP 协议网络编程《二》的基础上增加了当服务器重启后客户端不挂掉,客户端能够重试连接服务端的功能。


代码实现如下:

server.py文件内容如下

 1from socket import socket,SOCK_STREAM,AF_INET #导入模块
 2
 3def tcp_server():
 4    tcp_server_socket=socket(AF_INET,SOCK_STREAM) #创建TCP服务端套接字对接,UDP是SOCK_DGRAM
 5    server_address=('127.0.0.1',9999)#定义本TCP服务端的ip and port
 6    tcp_server_socket.bind(server_address) #绑定本机的9999端口
 7    tcp_server_socket.listen() #执行监听
 8    client_socket,client_info = tcp_server_socket.accept() #接收客户端的socker and info
 9    while 1:
10        data=client_socket.recv(1024)#接收客户端发过来的消息
11        datas=data.decode("utf-8")
12        if(len(datas)>1):
13            print("the client say:",datas)#打印客户端发送过来的信息
14            if(datas=="bye"):
15                break
16            server_answer=input('>>>')#从键盘录入信息
17            client_socket.send(server_answer.encode("utf-8"))
18
19if __name__ == '__main__':
20    print("the TCP server is running ...")
21    tcp_server()


client.py文件内容如下

 1import time
 2
 3from socket import socket,SOCK_STREAM,AF_INET #导入模块
 4
 5
 6def connection_socket():
 7    tcp_client_socket = socket(AF_INET, SOCK_STREAM)  # 创建TCP客户端套接字对接
 8    server_address = ('127.0.0.1', 9999)  # 定义本TCP服务端的ip and port
 9    try:
10        tcp_client_socket.connect(server_address)  # 连接到服务端
11    except:
12        print("connection failed!!!")
13        exit(0)
14    return tcp_client_socket
15
16def tcp_client():
17    while 1:
18        try:
19            tcp_client_socket=connection_socket()
20            message=input("client>>>") #定义要发送的消息
21            print("the message is:",message)
22            print(tcp_client_socket)
23            tcp_client_socket.send(message.encode("utf-8"))#向服务端发送消息
24            response=tcp_client_socket.recv(1024)
25            print("the server say:",response.decode("utf-8"))
26        except:
27            tcp_client_socket.close()#关闭原有socket连接
28            print("the socket connection is failed, try again to reconnection...")
29            time.sleep(3)
30
31if __name__ == '__main__':
32    print("the TCP client is running ...")
33    tcp_client()
34    print("the message was send")


client console output:

 1the TCP client is running ...
 2
 3client>>>abc
 4the message is: abc
 5<socket.socket fd=200, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 55699), raddr=('127.0.0.1', 9999)>
 6the server say: def
 7
 8client>>>aa
 9the message is: aa
10<socket.socket fd=1888, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 55700), raddr=('127.0.0.1', 9999)>
11the socket connection is failed, try again to reconnection...
12
13client>>>abc
14the message is: abc
15<socket.socket fd=1888, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 55703), raddr=('127.0.0.1', 9999)>
16the server say: def
17client>>>

Tags:

来源:三叶资源网,欢迎分享,公众号:iisanye,(三叶资源网⑤群:21414575

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

百度站内搜索
关注微信公众号
三叶资源网⑤群:三叶资源网⑤群

网站分类
随机tag
设置系统Edge浏览器内存皮肤公众号短链接Exdui界面花椒直播工具屏幕锁屏工具易语言基础教程抗锯齿自定义执行代码Sqlite3数据库田英章宏插件扫码登录群发例程post登陆多页面浏览器远程端口GIF录屏API读写内存
最新评论