Reputation: 3
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()
The error:
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
Translated:
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
I have tried sendall
but it did not change a thing, what can I do?
Upvotes: 0
Views: 1107
Reputation: 111
Here is the problem:
data = s.recv(1024)
It should be c.recv
to read from the accepted client socket, not s.recv
to read from the listening socket.
Upvotes: 2