0

That's what happens: After the client sends some messages, it gives ConnectionAbortedError: [WinError 10053] and the server keeps running

Images:

server socket: enter image description here

client socket: enter image description here

Here's my server code:

from socket import *

def server(address, port):
    sock = socket(AF_INET, SOCK_STREAM)
    sock.bind((address, port))
    sock.listen(10)
    while True:
        clientsock, addr = sock.accept()
        ip, _ = addr
        msg = input('YOU: ')
        clientsock.send(bytes(msg, 'utf-8'))
        data = clientsock.recv(2048)
        print('%s - ' % ip, data.decode('utf-8'))
        if not data:
            break
        clientsock.shutdown(SHUT_WR)
        clientsock.close()
    sock.close()

if __name__ == '__main__':
    server('192.168.0.101', 5000)

Client:

from socket import *

def client(address, port):
    sock = socket(AF_INET, SOCK_STREAM)
    sock.connect((address, port)) #0.0.0.0 isnt valid
    while True:
        data = sock.recv(2048)
        print('%s - ' % address, data.decode('utf-8'))
        msg = input('YOU: ')
        sock.send(bytes(msg, 'utf-8'))
    sock.close()

if __name__ == '__main__':
    client('192.168.0.101', 5000)
nelson450
  • 1
  • 3
  • I suspect there are answers in this post: https://stackoverflow.com/questions/1472876/why-is-host-aborting-connection – doctorlove Jan 17 '19 at 13:04

1 Answers1

0

Server in each loop accept new client sends one message receive one message and disconnect client. Then it waits for another client.

You need another loop for handling client.

That another loop could be placed in another thread.

If you nest that loop in this loop, you will be able to handle just one client at the time. I modified your server like that:

def server(address, port):
    sock = socket(AF_INET, SOCK_STREAM)
    sock.bind((address, port))
    sock.listen(10)
    while True: # server loop
        clientsock, addr = sock.accept()
        ip, _ = addr
        msg = "Hello to client from %s" % ip
        # next line is here because your client need message from server to send message
        clientsock.send(bytes(msg, 'utf-8'))  
        while True: #  client loop
            data = clientsock.recv(2048)
            msg = '%s - %s' % (ip, data.decode('utf-8'))
            print(msg)
            if not data:
                break
            clientsock.send(bytes(msg, 'utf-8'))  # for multiple clients you need send msg to all
        clientsock.shutdown(SHUT_WR)
        clientsock.close()
    sock.close()

To handle multiple clients you do not want to block server loop until client loop ends. You could run client loop in another thread and continue waiting for next client clientsock, addr = sock.accept().

Similarly you might want to separate receiving messages from server and waiting for client input.

Frane
  • 534
  • 6
  • 13
  • I extended my answer. Now it should be clear why your chat fails. Please accept answer. For tutorial how to make client-server chat see https://www.geeksforgeeks.org/simple-chat-room-using-python/ – Frane Feb 26 '19 at 12:19