I got this code, when i compile it it shows this error:

Traceback (most recent call last):
File "C:\Users\User\Desktop\", line 13, in <module>
UDPSock.bind(addr)
File "C:\Python27\lib\", 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

If you can help me fix this code or maby write a better client and server side program in python.

Here is the Client side:

# Client program

from socket import *

# Set the socket parameters
host = "localhost"
port = 21567
buf = 1024
addr = (host,port)

# Create socket
UDPSock = socket(AF_INET,SOCK_DGRAM)

def_msg = "===Enter message to send to server===";
print "\n",def_msg

# Send messages
while (1):
	data = raw_input('>> ')
	if not data:
		break
	else:
		if(UDPSock.sendto(data,addr)):
			print "Sending message '",data,"'....."

# Close socket
UDPSock.close()

And here is the server side program:

# Server program

from socket import *

# Set the socket parameters
host = "localhost"
port = 21567
buf = 1024
addr = (host,port)

# Create socket and bind to address
UDPSock = socket(AF_INET,SOCK_DGRAM)
UDPSock.bind(addr)

# Receive messages
while 1:
	data,addr = UDPSock.recvfrom(buf)
	if not data:
		print "Client has exited!"
		break
	else:
		print "\nReceived message '", data,"'"

# Close socket
UDPSock.close()

Thank you:?:

Dani AI

Generated

On Windows, error 10048 means something is already bound to that IP/port. With UDP there is no TIME_WAIT, so this almost always indicates a previous instance of your server is still running (or another app is using the same port). Find and stop it, or use a different port.

Quick checks on Windows:

netstat -ano | findstr :21567
tasklist /FI "PID eq <the_pid_from_netstat>"
taskkill /PID <the_pid> /F

To make restarts more reliable and ensure the socket always closes, wrap your bind/loop in a try/finally. On Windows you can also request exclusive use of the port so another process cannot silently share it:

import socket, sys

addr = ("127.0.0.1", 21567)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
    if sys.platform.startswith("win"):
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1)
    sock.bind(addr)
    # ... recvfrom loop ...
finally:
    sock.close()

Avoid reusing the same variable name for both the server bind address and the client address returned by recvfrom; use something like client_addr to prevent confusion.

Microsoft documents WSAEADDRINUSE (10048) at Windows Sockets error codes. The Python socket API and setsockopt options are described in the official docs at socket. For identifying which process holds a port, see the Windows netstat command reference at netstat.

Recommended Answers

All 4 Replies

Your code works fine for me on both windows and linux. You probably had a problem on a previous run and the socket hadn't closed. They usually close after a while, or reboot your system.

if you end up using threads make sure the threads exit. I've ran into that error a few times only to find an extra instance of python.exe running in the background because a thread never exited.

Thanks guys, I will try that. Just how do I close a socket in the python program?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.