Python socket throwing the following error ConnectionResetError: [Errno 54] Connection reset by peer

I have been trying to create a messaging service in python using sockets. I have written the code to host two connections and allow one to send messages to the other using username and host_addr.

But every time I try to connect the second client and send a message from the first getting the following error.

ConnectionResetError: [Errno 54] Connection reset by peer

Server.py

import socket
import _thread as thread
HOST = "127.0.0.1" # Standard loopback interface address (localhost)
PORT = 1237 # Port to listen on (non-privileged ports are > 1023)
user_mapping = {}
def on_new_client(conn, addr): data = conn.recv(1024) data = data.decode() print(data) print(user_mapping) if data[:8] == "username": user_mapping[data[9:]] = (addr, data[9:]) elif data[0] == "@": for i in user_mapping.values(): if i[0] == addr: from_user = i[1] else: str = "user not found" conn.sendto(str.encode(), addr) str = "%s:%s" % (from_user, data[data.find(":") + 1:]) conn.sendto(str.encode(), user_mapping[data[1:data.find(":")](0)]) else: pass
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(10)
while True: conn, addr = s.accept() thread.start_new_thread(on_new_client,(conn,addr))
s.close()

Client.py

import socket
HOST = "127.0.0.1" # The server's hostname or IP address
PORT = 1237 # The port used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
username = input("Enter user name")
str = "username: %s" % username
s.send(str.encode())
while True: message = input(username+">") s.send(message.encode()) data = s.recv(1024) print(data)

Was hoping some would could answer why this is happening and guide me to any good links where there is Information on creating a messaging service in python.

1 Answer

Client is sending 2 messages and then receiving one. But server just listen once and then send one or two packages.

Chronologically:

  1. Client sends a package, and server reads it.
  2. Then both client and server try to send a package. Both packages that won't meet a listening peer.
  3. Then client try to receive a package, but server won't send (he already sent it before) or it may send but its too late because communication is already broken.

Concepts you may implement always:

  1. If one talk, another one may listen.
  2. If a package is mean to be sent, it shall be sent anyway. Dont let a 'if' statment that send package when at 'else' that does not (or viceversa).

==== EDIT ====

About solution:

You need to work with paralel loops. Take a look at this code

He uses two threads on client, one for keep listening for new server updates (messages for other people) and another one to wait input from user.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like