954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with python server and client side program.

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

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

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:?:

MasterHacker110
Junior Poster in Training
63 posts since Dec 2011
Reputation Points: 6
Solved Threads: 0
Infraction Points: 5
 

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.

NewbieXcellence
Light Poster
32 posts since Jul 2010
Reputation Points: 10
Solved Threads: 2
 

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.

ihatehippies
Junior Poster
190 posts since Oct 2008
Reputation Points: 33
Solved Threads: 13
 

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

MasterHacker110
Junior Poster in Training
63 posts since Dec 2011
Reputation Points: 6
Solved Threads: 0
Infraction Points: 5
 

Your code example calls 'close()' which normally works fine. The alternative is to call
socket.shutdown(). Here is a good post explaining the difference between the two.

http://stackoverflow.com/questions/409783/socket-shutdown-vs-socket-close

ihatehippies
Junior Poster
190 posts since Oct 2008
Reputation Points: 33
Solved Threads: 13
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You