LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Python socket: does not recieve anything (https://www.linuxquestions.org/questions/programming-9/python-socket-does-not-recieve-anything-631134/)

Hewson 03-27-2008 07:16 PM

Python socket: does not recieve anything
 
Hi LQers,

I am new to sockets in general. I have a program that is supposed to listen for udp packets sent to the broadcast address. I can see (sniffing with wireshark) that the packets are comming accross the interface, but nothing is returned. I do not know how to debug this problem or if any this is poorly configured or what.

Any suggestions would be great.
PHP Code:

# create udp socket
mysocket socket.socket(socket.AF_INETsocket.SOCK_DGRAM)
# allow send/recieve from broacast address
mysocket.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,1)
# allow the socket to be re-used
mysocket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
mysocket.bind((self.listen_addressself.listen_port))
# wait for a message for 30 seconds.
mysocket.settimeout(30)
#this times out every time.
data mysocketsocket.recv(1024

Thanks,

Steve

ghostdog74 03-27-2008 07:48 PM

is that all your code? take a look at the HOWTO for more info

Hewson 03-28-2008 11:06 AM

ghostdog,

thanks for the response. No, that is not all of my code. (The actual full program includes around 800 lines of code) Most of it is not related to my socket issue, but I did realize I may have left out key part of my problem. Before I listen on the socket, I send a message:

PHP Code:


# create udp socket
mysocket socket.socket(socket.AF_INETsocket.SOCK_DGRAM)
# allow send/recieve from broacast address
mysocket.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,1)
# allow the socket to be re-used
mysocket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
mysocket.bind((self.listen_addressself.listen_port))

# send broadcast packet  (this packet is sent successfully.)
mysocket.sendto(packet.EncodePacket(),("255.255.255.255" ,self.emit_port))

# wait for a (response) message for 30 seconds.
mysocket.settimeout(30)
#this times out every time.
data mysocketsocket.recv(1024

Thanks for the pointer to that guide, for others in my position . . .
Here is a very useful guide that I started with: http://www.scribd.com/doc/134861/Soc...ng-with-python
The man page for socket is also useful.

Hewson 03-28-2008 12:30 PM

Well I have figured out my problem:

It was not related to my socket code. It was related to the address I was attempting to bind to. My linux box has 4 interfaces. I used bind to specify which interface to use. I then sent a broadcast packet. The response is not sent to my address (that I specified in the the bind() ), but to 255.255.255.255.

My new problem: how do I specify an interface and listen for broadcast messages?

Hewson 03-28-2008 12:53 PM

I'm going to just use two sockets: one for sending and a second for listening to the broadcast address.


All times are GMT -5. The time now is 06:10 PM.