import socket
import threading
host = '127.0.0.1'
port = 36250
def RetrFile(sock):
from account import posts
filename = 'posts.txt'
for item in posts(filename):
sock.send(item)
sock.send("DONE".encode())
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
h_name = socket.gethostname()
IP_address = socket.gethostbyname(h_name)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((IP_address,port))
print("Server started")
print("IP address:", IP_address)
while True:
print("Waiting for clients...")
s.listen()
c, addr = s.accept()
print("Client connected, IP: " + str(addr))
data = s.recv(1024)
if data == "POSTS".encode():
t = threading.Thread(target=RetrFile, args=(c))
t.start()
s.close()
错误:
Traceback (most recent call last):
File "C:\Users\####\Downloads\Social-media-server (1)\main.py", line 26, in <module>
data = s.recv(1024)
OSError: [WinError 10057] En begäran att skicka eller ta emot data tilläts inte eftersom socketen inte är ansluten och (när du skickar på en datagramsocket med hjälp av ett sendto-anrop) ingen adress angavs
翻译:
Traceback (most recent call last):
File "C:\Users\####\Downloads\Social-media-server (1)\main.py", line 26, in <module>
data = s.recv(1024)
OSError: [WinError 10057] A request to send or receive data was not allowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was specified
我试过了 sendall
但它没有改变一件事,我能做什么?
1条答案
按热度按时间e7arh2l61#
问题是:
应该是
c.recv
从接受的客户端套接字读取,而不是s.recv
从侦听套接字读取。