Issue
I'm using socket programming for sending a UDP text message and it's working fine. Here is the code:
send:
import socket
UDP_IP = "10.0.0.2"
UDP_PORT = 5005
MESSAGE = "Hello"
print "UDP target IP:", UDP_IP
print "UDP target port:", UDP_PORT
print "message:", MESSAGE
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
recv:
import socket
UDP_IP = "10.0.0.2"
UDP_PORT = 5005
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024)
print "received message:", data
Now I want to change the message I'm sending to a list. I tried to do this by using pickle
. Here is the code:
send:
import socket
import pickle
UDP_IP = "10.0.0.2"
UDP_PORT = 5005
a = []
a.append('H')
a.append('G')
MESSAGE = pickle.dumps(a)
print "UDP target IP:", UDP_IP
print "UDP target port:", UDP_PORT
print "message:", MESSAGE
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
recv:
import socket
import pickle
UDP_IP = "10.0.0.2"
UDP_PORT = 5005
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024)
print "received message:", data
pickle.loads(data)
print(data)
But I'm not getting the correct form of the list in the receiver side. Here is the output:
(1p0
S'H'
p1
aS'G'
p2
a.
(1p0
S'H'
p1
aS'G'
p2
a.
what's wrong?
Solution
The problem is solved. The code is working fine, there was a problem because I was printing the data itself not the output of pickle.loads(data)
, So this code is working fine now:
recv:
import socket
import pickle
UDP_IP = "10.0.0.2"
UDP_PORT = 5005
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024)
print "received message:", data
print(pickle.loads(data))
Answered By - helen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.