""" 网络发送信息 """ import socket import threading def handlesend(tcpclient): """ 发送 :return: """ while True: message = input() tcpclient.send(message.encode()) def handlerecv(tcpclient): """ 接收 :return: """ recvdata = tcpclient.recv(1024) print(recvdata.decode()) # 中文 # print(recvdata.decode('gbk')) def send(): #TCP 客户端 #1 创建TCP socket 对象 # socket.AF_INET ip4 # socket.SOCK_STREAM TCP 协议 # socket.DGRAM upd 协议 tcpclient=socket.socket(socket.AF_INET,socket.SOCK_STREAM) #2 链接TCP 服务端 ip地址, port 端口 tcpclient.connect('172.16.1.106',6666) t1=threading.Thread(target=handlesend,args=(tcpclient,)) t1.start() t2=threading.Thread(target=handlerecv,args=(tcpclient,)) t2.start() ''' #3 发送信息 while True: message=input() tcpclient.send(message.encode()) #中文 #tcpclient.send(message.encode('gbk')) #4 接收信息 用多线程来解决卡顿 recvdata=tcpclient.recv(1024) print(recvdata.decode()) #中文 #print(recvdata.decode('gbk')) ''' #5 关闭 socket tcpclient.close()
标签:tcpclient,socket,python,TCP,decode,recvdata,message From: https://www.cnblogs.com/geovindu/p/17574819.html