TCP构建流程示例

TCP构建流程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import socket


def main():
# 创建TCP套接字
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 建立连接
tcp_socket.connect(("127.0.0.1", 8888))
# 发送数据/接收数据
while True:
# 发送数据
send_content = input("请输入要发送的消息:")
if(send_content == "exit"):
break
tcp_socket.send(send_content.encode("gbk"))
# 接收数据
recv_data = tcp_socket.recv(1024)
# 打印接收到的数据
print(recv_data.decode("gbk"))
# 关闭连接
tcp_socket.close()


if __name__ == "__main__":
main()

评论



2019-2021 覃浩的博客 鄂ICP备19020398号-4 正在载入...

PoweredHexo
HostedGitHub & Coding
DNSAliyun & DNSPod
CDNjsDelivr
PictureBedjsDelivr
ThemeVolantis