Re: [問題] socket thread

作者: pi2324 (我愛PTT)   2019-03-29 19:31:53
※ 引述《pi2324 (PTT)》之銘言:
: 不好意思問的很簡陋
: 超級新手目前想建立簡單的socket連線
: server端接收文字
: client端可發送文字
: 以上已實現....
: 目前想要兩端皆可發收訊息
: 用thread會卡住
: 能不能請前輩提點
: 往lock的方向尋找嗎?
: 謝謝
先謝謝
以下是程式碼
Server 端
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
import socket
import threading
import time
HOST=""
PORT=5066
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind((HOST,PORT))
server.listen()
client , address =server.accept()
def re():
while 1:
print(client.recv(1024).decode())
ree = threading.Thread(target=re(),args=())
ree.start()
def se():
while 1:
server.send(str.encode(input()))
see = threading.Thread(target=se(),args=())
see.start()
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Client 端
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
import socket
import threading
HOST = input()
PORT = 5066
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.connect((HOST,PORT))
def se():
while 1:
content = input()
client.send(content)
def re():
while 1:
response = client.recv(4096)
print(response)
ree = threading.Thread(target=re(),args=())
ree.start()
see = threading.Thread(target=se(),args=())
see.start()
作者: jn8029 (宅男8029)   2019-03-29 22:42:00
Target=re不要放刮號
作者: pi2324 (我愛PTT)   2019-03-29 23:40:00
謝謝沒括號真的就可以了
作者: Angesi (小雲豹)   2019-03-30 16:45:00
哇 原來用python也能寫socket

Links booklink

Contact Us: admin [ a t ] ucptt.com