41

I'm trying to create a very basic server in python that listens in on a port, creates a TCP connection when a client tries to connect, receives data, sends something back, then listens again (and repeats the process indefinitely). This is what I have so far:

from socket import *

serverName = "localhost"
serverPort = 4444
BUFFER_SIZE = 1024

s = socket(AF_INET, SOCK_STREAM)
s.bind((serverName, serverPort))
s.listen(1)

print "Server is ready to receive data..."

while 1:
        newConnection, client = s.accept()
        msg = newConnection.recv(BUFFER_SIZE)

        print msg

        newConnection.send("hello world")
        newConnection.close()

Sometimes this seems to work perfectly well (if I point my browser to "localhost:4444" the server prints out the HTTP GET request and the webpage print the text "hello world"). But I'm getting the following error message sporadically when I try to start the server after closing it in the last few minutes:

Traceback (most recent call last):
  File "path\server.py", line 8, in <module>
    s.bind((serverName, serverPort))
  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'm programming in python using Windows 7. Any ideas on how to fix this?

3
  • 1
    A higher level module like twisted is away more productive than using socket directly. Commented Sep 11, 2012 at 4:04
  • 2
    @PauloScardine: Yeah, but it's an exercise I'm doing in which I need to use sockets (to learn more about the lower-level stuff). Commented Sep 11, 2012 at 4:06
  • 2
    @PauloScardine incidentally, I'm also getting this error using Twisted on Windows Commented Jun 22, 2017 at 0:28

7 Answers 7

65

On Windows, you can try these steps:

1. check which process uses the port.

# 4444 is your port number
netstat -ano|findstr 4444

you will get something like this:

# 19088 is the PID of the process
TCP    0.0.0.0:4444           *:*                                    19088

2. kill this process

With:

tskill 19088

Or:

taskkill /F /PID 19088

Good luck.

Sign up to request clarification or add additional context in comments.

2 Comments

This should be the top answer: the error shows up primarily during debugging.
This answer was the key to my problem. This answer stackoverflow.com/a/12362623/2759116 also pushed me a step ahead but it is not the main solution to this problem.
36

Enable the SO_REUSEADDR socket option before calling bind(). This allows the address/port to be reused immediately instead of it being stuck in the TIME_WAIT state for several minutes, waiting for late packets to arrive.

s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

Comments

5

In the article posted by @JohnKugelman it is stated that even after enabling SO_REUSEADDR you cannot use the socket to connect to the same remote end as before:

SO_REUSADDR permits you to use a port that is stuck in TIME_WAIT, but you still can not use that port to establish a connection to the last place it connected to.

I see that you are just testing/playing around. However, to avoid this error you really need to make sure that you terminate the connection properly. You could also mess with the tcp timings of the operating system: http://www.linuxquestions.org/questions/linux-networking-3/decrease-time_wait-558399/

For testing purposes it would also be fine if you just change your serverPort in a round-robin fashion, what do you think?

Comments

2

It may be because you have not terminated the server code and tried to run it again on a another cmd. Server cannot be hosted on the same port number, try killing the previous hosted server.

Comments

0

It's important (on Windows specifically) to close the socket. Otherwise, you have to wait for it to timeout after closing Python.

Would:

try:
    while 1:
        newConnection, client = s.accept()
        msg = newConnection.recv(BUFFER_SIZE)

        print msg

        newConnection.send("hello world")
        newConnection.close()
finally:
    s.close()

help?

Comments

0

If you are trying to rerun the server without stopping the last instant of the server it won't work. If you want to stop the current instant go to

shell-->restart shell.

If you have already closed the shell without stopping the server go to the task manager and end task python process in the background processers. This will stop the last instant of your server.

Comments

0

I changed my port number into different one and it works.

if __name__ == '__main__':
    socketio.run(app, debug = True, use_reloader = False, port=1111)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.