LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   python: socket broadcast produces a broken pipe (http://www.linuxquestions.org/questions/programming-9/python-socket-broadcast-produces-a-broken-pipe-718821/)

eantoranz 04-13-2009 12:59 PM

python: socket broadcast produces a broken pipe
 
Hi!

I'm trying to have a client detect what host a service is running on by doing a broadcast instead of telling the client what host to use.

Now, I when I try to send the date for the broadcast (on the sendto), I get a broken pipe:

Here's the output:
Code:

Traceback (most recent call last):
  File "flisolclient.py", line 22, in <module>
    sock.sendto("exit", ('<broadcast>', SERVICE_PORT))
socket.error: [Errno 32] Broken pipe

From strace:
Code:

sendto(3, "exit"..., 4, 0, {sa_family=AF_INET, sin_port=htons(9000), sin_addr=inet_addr("255.255.255.255")}, 16) = -1 EPIPE (Broken pipe)
--- SIGPIPE (Broken pipe) @ 0 (0) ---

Here's the offending code
Code:

SERVICE_PORT = 9000
import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.setblocking(True)
sock.settimeout(5) # 5-second timeout to detect the service
sock.sendto("exit", ('<broadcast>', SERVICE_PORT))
server = 0
try:
    buff, server = sock.recvfrom(2048)
except:
    # probably, timed out
    print "Couldn't find registration service"
    exit(-1)

When I change the destination address to localhost (the service is running on this box for testing), I get the same error.

What am I missing?

Hko 04-14-2009 06:20 AM

Quote:

Originally Posted by eantoranz (Post 3507557)
Code:

sendto(3, "exit"..., 4, 0, {sa_family=AF_INET, sin_port=htons(9000), sin_addr=inet_addr("255.255.255.255")}, 16) = -1 EPIPE (Broken pipe)
--- SIGPIPE (Broken pipe) @ 0 (0) ---


Uh? Trying to broadcast over the entire internet?

Quote:

Originally Posted by eantoranz (Post 3507557)
Here's the offending code
Code:

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)


You're using TCP for broadcasting. This looks strange (wrong?) to me. I'm by no means an expert, so I may be wrong at this.

eantoranz 04-14-2009 08:24 AM

Yes.... I gave up that route. I'm trying with zeroconf... but I'm stuck there as well:

http://www.linuxquestions.org/questi...server-718899/

Any ideas?


All times are GMT -5. The time now is 12:25 AM.