会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132647个问题
Python 全系列/第三阶段:Python 网络与并发编程/网络通信 931楼
Python 全系列/第三阶段:Python 网络与并发编程/并发编程 936楼
Python 全系列/第三阶段:Python 网络与并发编程/并发编程 938楼

TCP多线通信问题.docx



def recv_data():
   while True:
       # 接收服务器端数据
       recv_data = client_socket.recv(1024)
       recv_content = recv_data.decode('gbk')
       print(f"服务端说:{recv_content}")
       if recv_content == "end":
           print("结束接收消息")
           break


def send_data():
   while True:
       # 给服务端发消息
       msg = input(">")
       client_socket.send(msg.encode('gbk'))
       if msg == "end":
           break


if __name__ == '__main__':
   client_socket = socket(AF_INET, SOCK_STREAM)
   client_socket.connect(("127.0.0.1", 8899))

   t1 = Thread(target=recv_data)
   t2 = Thread(target=send_data)
   t1.start()
   t2.start()

   client_socket.close()


# coding=utf-8
from socket import *
from threading import Thread


def recv_data():
   while True:
       recv_data = client_socket.recv(1024)
       recv_content = recv_data.decode('gbk')
       print(f"客户端说:{recv_content},来自{client_info}")
       if recv_content == "end":
           print("结束接收消息")
           break


def send_data():
   while True:
       msg = input(">")
       client_socket.send(msg.encode('gbk'))
       if msg == "end":
           print("结束发送消息!")
           break


if __name__ == '__main__':
   server_socket = socket(AF_INET, SOCK_STREAM)
   server_socket.bind(("127.0.0.1", 8899))
   server_socket.listen(5)
   print("等待接收连接!")
   client_socket,client_info = server_socket.accept()
   print("一个客户端建立连接成功!")
   t1 = Thread(target=recv_data)
   t2 = Thread(target=send_data)
   t1.start()
   t2.start()

   t1.join()
   t2.join()

   client_socket.close()
   server_socket.close()


Python 全系列/第三阶段:Python 网络与并发编程/网络通信 939楼
Python 全系列/第三阶段:Python 网络与并发编程/并发编程 940楼
Python 全系列/第三阶段:Python 网络与并发编程/并发编程 941楼
Python 全系列/第三阶段:Python 网络与并发编程/并发编程 942楼

在mac下会报错 没有join的情况,为什么报如下的错误:

Traceback (most recent call last):

  File "<string>", line 1, in <module>

  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/spawn.py", line 116, in spawn_main

Traceback (most recent call last):

      File "<string>", line 1, in <module>

exitcode = _main(fd, parent_sentinel)

  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/spawn.py", line 126, in _main

  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/spawn.py", line 116, in spawn_main

    self = reduction.pickle.load(from_parent)

  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/synchronize.py", line 110, in __setstate__

    exitcode = _main(fd, parent_sentinel)

  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/spawn.py", line 126, in _main

    self = reduction.pickle.load(from_parent)

  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/synchronize.py", line 110, in __setstate__

    self._semlock = _multiprocessing.SemLock._rebuild(*state)

FileNotFoundError: [Errno 2] No such file or directory

    self._semlock = _multiprocessing.SemLock._rebuild(*state)

FileNotFoundError: [Errno 2] No such file or directory

Traceback (most recent call last):

  File "<string>", line 1, in <module>

  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/spawn.py", line 116, in spawn_main

    exitcode = _main(fd, parent_sentinel)

  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/spawn.py", line 126, in _main

    self = reduction.pickle.load(from_parent)

  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/synchronize.py", line 110, in __setstate__

    self._semlock = _multiprocessing.SemLock._rebuild(*state)

FileNotFoundError: [Errno 2] No such file or directory


Python 全系列/第三阶段:Python 网络与并发编程/并发编程 943楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637