Hi,
I've written two programs.One is for sending data from outside,another is for receiving the message.Both work well.But I also want to get the message value for next program.The problem is I have no idea how to get the value from the receiver. PLZ help me out!Many Thanks!

This is the message sender:

from socket import *
# Set the socket parameters
host = "localhost"
port = 9885
buf = 1024
addr = (host,port)
# Create socket
UDPSock = socket(AF_INET,SOCK_DGRAM)
def_msg = "===Enter message to send to server===";
print "\n",def_msg
# Send messages
while (1):
        data = raw_input('>> ')
	if not data:
		break
	else:
		if(UDPSock.sendto(data,addr)):
			print "Sending message '",data,"'....."

# Close socket
UDPSock.close()

This is the message receiver:

# Server program

from socket import *

# Set the socket parameters
host = "localhost"
port = 9885
buf = 1024
addr = (host,port)

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

# Receive messages

while 1:
    data, addr = UDPSock.recvfrom(buf)
    if not data:
        print "Client has exited!"
        break
    else:
        print "\nReceived message '", data, "'"
        Message = 'data'
        print 'Message is', data
# Close sockets
UDPSock.close()

Recommended Answers

All 2 Replies

Member Avatar for leegeorg07

what is the problem? it works fine in mine, other than a few indentation errors in the receiver.

Thanks for your reply
I was intended to get the real time value of the receiver.
Now, I use the write command to write down the received data into a file which is used to be the input signal of another program.It's working well.
:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.