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)
print "Server is ready to receive data..."
newConnection, client = s.accept()
msg = newConnection.recv(BUFFER_SIZE)
Traceback (most recent call last):
File "path\server.py", line 8, in <module>
File "C:\Python27\lib\socket.py", line 224, in meth
error: [Errno 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted
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(SOL_SOCKET, SO_REUSEADDR, 1)