0

I am following this tutorial on sockets, and I was doing fine until the teacher ran the program again in the text editor at 34 minutes 44 seconds. On my end, I received an error message saying, socket.error: [Errno 48] Address already in use. I am using a Macbook, and my text editor is Atom. I would appreciate any help I can get with fixing or addressing this error!

Traceback

Traceback (most recent call last):
  File "/Users/lyons/Documents/sockets/server.py", line 16, in <module>
    server.bind(ADDR)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 48] Address already in use
[Finished in 0.393s]

server.py

import socket
import threading

HEADER = 64
PORT = 5050
                                                                                                       
SERVER = socket.gethostbyname(socket.gethostname()) 
ADDR = (SERVER, PORT)
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!DISCONNECT"

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)

def handle_client(conn, addr):
    print("[NEW CONNECTION] {addr} connected.")
    connected = True
    while connected:
        msg_length = conn.recv(HEADER).decode(FORMAT)       
        msg_length = int(msg_length)
        msg = conn.recv(msg_length).decode(FORMAT)
        if msg == DISCONNECT_MESSAGE:
            connected = False
        print("[{}] {}".format(addr, msg))
    conn.close()

def start():
    server.listen()
    print("[LISTENING] Server is listening on {}".format(SERVER))
    while True:
        conn, addr = server.accept()    # BLOCKING LINE OF CODE
        thread = threading.Thread(target=handle_client, args=(conn, addr))
        thread.start()
        print("[ACTIVE CONNECTIONS] {threading.activeCount() - 1}")

print("[STARTING] server is starting...")
start()

client.py

import socket

HEADER = 64
PORT = 5050

FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!DISCONNECT"
SERVER = socket.gethostbyname(socket.gethostname())
ADDR = (SERVER, PORT)

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)

  • Does this answer your question? [What is the meaning of SO\_REUSEADDR (setsockopt option) - Linux?](https://stackoverflow.com/questions/3229860/what-is-the-meaning-of-so-reuseaddr-setsockopt-option-linux) – Ture Pålsson Aug 12 '20 at 05:08
  • I'm not sure I understand that explanation with my post. Am I misunderstanding something? I am a beginner by the way –  Aug 12 '20 at 05:17
  • It looks like SO has saved the longer anser I typed up first, so I'll just post that. :) – Ture Pålsson Aug 12 '20 at 05:20

1 Answers1

0

An obvious cause would be if you already had an instance of your server running.

A more tricky case is when a connection has been shut down in a "dirty" way. This "quarantines" the address for a while before it can be used again. You can work around this by setting the SO_REUSEADDR flag on the socket.

Edit: here's some more reading, though not on SO.

Ture Pålsson
  • 6,088
  • 2
  • 12
  • 15