0

I am trying to write a simple socket (server side) to communicate with a process on the same machine, using UDP as Transport Protocol. This is the code:

from socket import *
serverPortS = 1234 
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind(('localhost', serverPortS))
print ('Il server e pronto a ricevere')
messageS, clientAddressS =serverSocket.recvfrom(2048)
modifiedMessageS = messageS.upper()
serverSocket.sendto(modifiedMessageS, clientAddressS)
serverSocket.close()

The console gives me the error, just launching the server (no client yet):

Traceback (most recent call last):
  File "E:\Laboratorio Python\udp_server.py", line 12, in <module>
    serverSocket.bind(('localhost', serverPortS))
OSError: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted

I don't know how to fix it or how does it means, and searching on the Web I found more complex problem; would love to hear a possible solution!

L. Repetti
  • 175
  • 1
  • 6
  • This is not a 'server socket'. It is a UDP socket. Why are you binding it to 'localhost'? – user207421 Apr 05 '17 at 10:02
  • @EJP The socket needs to be used from host to host, so I thought (my teacher) said us to do so; is this wrong? – L. Repetti Apr 05 '17 at 10:07
  • Binding it to 'localhost' is indeed wrong unles you're only trying to communicate within that host. Use 0.0.0.0. – user207421 Apr 05 '17 at 10:17
  • @EJP that's exactly what I'm doing, I'm trying to communicate whitin "myself", the same host! – L. Repetti Apr 05 '17 at 10:25
  • Possible duplicate of [Python: Binding Socket: "Address already in use"](http://stackoverflow.com/questions/6380057/python-binding-socket-address-already-in-use) – Noctis Skytower Apr 05 '17 at 15:02
  • My psychic powers tell that you are trying to do interprocess communication between two different applications with a socket. And you are trying to bind to the same port on each instance. – selbie Apr 05 '17 at 17:31
  • @selbie Yes! Is that wrong? Sorry for didn't write it properly – L. Repetti Apr 05 '17 at 17:56
  • 1
    You should have the server process bind to a fixed port (1234) and the client process bind to port 0 (such that the OS will big an available port). When the server needs to respond to the client, it reads the address off the packet it got from recvfrom to infer which port to send back to. Or just have the client use port 1235. – selbie Apr 06 '17 at 00:24

0 Answers0