Question

I've been programming my chat but then I get an error which crashes the server and so then the next time I won't be able to use the same port and so I get socket.bind error when I run it again. How to fix? Source code:

import socket
from threading import Thread

def clientHandler():
    conn, addr = sock.accept()
    print addr, "is Connected"
    while 1:
        data = conn.recv(1024)
        if not data:
            break
        print "Received Message", repr(data)
HOST = ''
PORT = raw_input('PORT: ')
PORT = int(PORT)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((HOST, PORT))
sock.listen(5)

print "Server is running......"

#Thread(target=clientHandler).start()
#Thread(target=clientHandler).start()
#Thread(target=clientHandler).start()

for i in range(5):
    Thread(target=clientHandler).start()


sock.close()

This is what happens:

    Server is running......
Exception in thread Thread-5:
Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
    self.run()
  File "C:\Python27\lib\threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "C:/Users/Judicaël/Desktop/server_test.py", line 5, in clientHandler
    conn, addr = sock.accept()
  File "C:\Python27\lib\socket.py", line 202, in accept
    sock, addr = self._sock.accept()
  File "C:\Python27\lib\socket.py", line 170, in _dummy
    raise error(EBADF, 'Bad file descriptor')
error: [Errno 9] Bad file descriptor

>>> ==================================================== RESTART ====================================================
>>> 

Traceback (most recent call last):
  File "C:/Users/Judicaël/Desktop/server_test.py", line 15, in <module>
    sock.bind((HOST, PORT))
  File "C:\Python27\lib\socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
error: [Errno 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted

I am using Win7 64bit python 2.7.6 64bit version and is there also a way to restore the ports which had been blocked or does it auto restore when restart comp?

*******FIXED by Daniel************

Fixed Code:

import socket, sys
from threading import Thread

def clientHandler(conn, addr):
    print addr, "is Connected"
    while 1:
        data = conn.recv(1024)
        if data == 'Admin: /server shutdown':
            sock.close()
            print 'quitting...'
            quit()

        if not data:
            break
        conn.sendall(data) # only sends to original sender
        print "Received Message", repr(data)
HOST = ''
PORT = int(raw_input('PORT: '))

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((HOST, PORT))
sock.listen(999999)

print "Server is running......"

#Thread(target=clientHandler).start()
#Thread(target=clientHandler).start()
#Thread(target=clientHandler).start()

for i in range(999999):
    conn, addr = sock.accept()
    Thread(target=clientHandler, args=(conn, addr)).start()

sock.close()

How do I now send the messages received to all clients?

Was it helpful?

Solution

The functions you have to call at server side is the following: - create a socket - bind it to a port - listen - accept - handle the client communication

You have multiple threads for accept. Then you close the socket, so all accepts fail. The correct way is

while True:
    client, addr = sock.accept()
    Thread(target=clientHandler, args=(client, addr)).start()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top