Table of contents

  1. Python socket.error: [Errno 98] Address already in use
  2. How to Use Raw Socket in Python?
  3. Python: Binding Socket: "Address already in use"

Python socket.error: [Errno 98] Address already in use

The "Address already in use" error, with the error code [Errno 98], typically occurs when you try to bind a socket to an IP address and port that is already in use by another process on your system. This error can happen when you're trying to start a server on a specific port, but that port is already being used by another program.

Here are some common ways to resolve this error:

  1. Change the Port Number: If you have control over the server's code, you can change the port number to a different one that is not in use. This can be done in the code where you create the socket and bind it to an address.

    import socket
    host = ''  # IP address
    port = 8080  # Change to a different port number
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind((host, port))
  2. Kill the Process Using the Port: You can find and terminate the process that is using the port you want to use. On Unix-like systems, you can use the lsof command to find the process ID (PID) using the port and then use kill to terminate it.

    lsof -i :8080  # Replace 8080 with your port number
    kill -9 PID    # Replace PID with the process ID from the previous command
  3. Wait for the Port to Be Released: If the port is used by a temporary process that will finish soon, you can wait for it to release the port naturally and then start your server.

  4. Use a Different IP Address: If you're binding to a specific IP address, try binding to a different one, such as '', which listens on all available network interfaces.

  5. Check for Zombie Processes: Sometimes, a process that uses the port may become a zombie process. Check for and remove any zombie processes on your system.

  6. Restart Your Computer: If all else fails and you cannot determine which process is using the port, restarting your computer may release the port.

It's important to note that on Unix-like systems, ports below 1024 typically require superuser (root) privileges to bind to. So, if you're trying to bind to a port below 1024, you may need to run your server as a superuser or use a port number above 1024.

How to Use Raw Socket in Python?

Using raw sockets in Python allows you to create custom network packets and send/receive them directly at the link layer, bypassing some of the higher-level protocols and libraries. However, raw sockets require administrative privileges and can be used for network reconnaissance or attacks. Therefore, use them responsibly and ensure you have the necessary permissions. Below is an example of how to use raw sockets in Python.

Please note that this example demonstrates sending raw ICMP Echo Request (ping) packets. To receive and process raw packets, you would typically need to use a lower-level library or module, such as Scapy.

Here's a basic example of how to send a raw ICMP Echo Request packet using the socket library:

import socket
import struct
import random

# Define the destination IP address (e.g.,
target_ip = ""

# Create a raw socket (AF_INET for IPv4, SOCK_RAW for raw socket)
    raw_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
except socket.error as e:
    print("Socket creation failed:", e)

# Create the ICMP packet
icmp_type = 8  # Echo Request
icmp_code = 0
icmp_checksum = 0
icmp_id = random.randint(0, 65535)
icmp_seq = 1
icmp_payload = b"Hello, World!"  # Replace with your payload

# Calculate the ICMP checksum
icmp_checksum = 0  # Placeholder
icmp_header = struct.pack("BBHHH", icmp_type, icmp_code, icmp_checksum, icmp_id, icmp_seq)
icmp_checksum = socket.htons(socket.htons(sum(struct.unpack("H", icmp_header + icmp_payload))))

# Re-create the ICMP header with the correct checksum
icmp_header = struct.pack("BBHHH", icmp_type, icmp_code, icmp_checksum, icmp_id, icmp_seq)

# Combine the ICMP header and payload
packet = icmp_header + icmp_payload

# Send the packet
    raw_socket.sendto(packet, (target_ip, 0))
    print(f"ICMP Echo Request sent to {target_ip}")
except socket.error as e:
    print("Packet sending failed:", e)

In this example:

  1. We create a raw socket using socket.socket() with the AF_INET address family (IPv4) and SOCK_RAW socket type.

  2. We define various fields of the ICMP packet, such as type, code, ID, sequence number, and payload.

  3. We calculate the ICMP checksum by creating a placeholder, calculating the sum of the header and payload, and then applying the proper byte order.

  4. We recreate the ICMP header with the correct checksum.

  5. We combine the header and payload to create the final ICMP packet.

  6. We send the ICMP packet using raw_socket.sendto() to the target IP address.

Please note that this example sends a single ICMP Echo Request packet and doesn't handle responses. To work with raw packets more extensively or for other protocols, consider using libraries like Scapy or other specialized networking libraries. Additionally, remember that raw socket operations typically require elevated privileges, such as running the script as an administrator or using sudo on Unix-like systems.

Python: Binding Socket: "Address already in use"

The error message "Address already in use" occurs when you try to bind a socket to a specific address and port, but that combination is already in use by another process or a previously closed socket that hasn't been released yet. This error is quite common when working with network sockets.

To resolve this issue, you can take the following steps:

  1. Change the Port: If you have control over the code, you can try changing the port number to a different one that is not currently in use. This is often the easiest solution.

  2. Wait for the Socket to be Released: If the previous socket that used the same address and port has not been fully closed and released, you may need to wait for it to be released. This can happen if you recently closed a socket but it's still in the TIME_WAIT state. This state typically lasts for a few minutes.

  3. Reuse the Address: You can set the SO_REUSEADDR socket option to allow the reuse of the same address and port combination. This can be helpful if you need to quickly restart a server on the same address and port. In Python, you can do this with the socket.SO_REUSEADDR option:

    import socket
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server.bind(('', 8080))

    Be cautious when using SO_REUSEADDR, as it can lead to unexpected behavior if not used correctly.

  4. Check for Other Processes: Make sure there are no other processes or applications running on your system that are using the same address and port combination.

  5. Close Existing Sockets: If your code is responsible for opening sockets, ensure that you properly close and release them when they are no longer needed. Use the socket.close() method to close the socket.

  6. Check for Leaked Sockets: If your application is complex and has many sockets, ensure that you are correctly closing all sockets in all code paths.

  7. Use a Different Address: If possible, use a different IP address along with the port to bind the socket. This can be especially useful when dealing with multiple network interfaces.

By following these steps, you can resolve the "Address already in use" error and successfully bind your socket to the desired address and port.

More Python Questions

More C# Questions