views:

32

answers:

1

hey guys, i got this code from http://www.evolt.org/node/60276 and modified it to listen for a single "1" coming from the other side

but whenever i run this program it stops and python IDLE goes to non-responding on "data1,addr = UDPSock.recvfrom(1024)"

def get1():
# Server program, receives 1 if ball found
# ff1 is file w/ received data

import socket
import time

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

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

# Receive messages
while 1:
            print "waiting..............."
            data1,addr = UDPSock.recvfrom(1024)
            print "got 1"
            if not data1:
                print "Client has exited!"
                break
            else:
                print "\nReceived message '", data1,"'"
                UDPSock.close() # Close socket
                print "socket closed\n"
                #call some other function that uses 1

and client side

def send1():
# Client program, sends 1 if ball found
# mf1 is file with data to be sent

import socket

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

# Create socket (UDP)
UDPSock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

mf1=1
print mf1

# Send messages
if(UDPSock.sendto(str(mf1),addr)):
        print "Sending message '",str(mf1),"'....."

# Close socket
UDPSock.close()

does anyone know what might be the cause of this? (sorry for long post)

A: 

As a second guess (I replaced my first guess with this) I suspect that you are running the receiver in IDLE and then IDLE is hanging so you can't run the client. I don't know exactly how IDLE works as I never use it, but the line containing recvfrom will stop the Python thread its running in until data is sent. So you need to start the client in a separate instance of IDLE or from the command line or something.

At any rate, I have tested the program in question on my Python with 127.0.0.1 as the host, and it worked fine, for some values of fine. The recvfrom does hang, but only until some data is sent, then it comes back with the data and prints it out and everything. You do have a bug that happens after that though. :-)

Omnifarious
that was a good guess (not that i know the answer) but i already tried that and it is not the problem...
pjehyun
@pjehyun - I tested this code myself, and it has a bug but largely works for one message. Are you running the client side while IDLE is hanging?
Omnifarious
i have two computers set up and run get1() on one then send1() on the other. get1() chokes as soon as it hits that one line. does that answer your question? im not sure
pjehyun
@pjehyun - Have you tested that you have network connectivity between those two computers? Have you tried running that program twice on the same computer to make sure it works when the network can't be the problem? Is there a firewall in between the two computers in question?
Omnifarious
yes, the thing is that it worked fine right before. only minimal changes were made, like time.sleep(2) was added. so the bug should be something very minute...
pjehyun