※ 引述《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()