开发者

networking program crashes

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?开发者_JAVA百科 (sorry for long post)


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. :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜