Binary data exchange

R

RasikaSrinivasan

friends

I have a pair of simple python programs as follows:

#!/usr/bin/python
# broadcast.py
import socket
from ctypes import *
import random

class PurgeData(Structure):
_fields_ = [("press",c_int), ("ticks",c_int), ("volume",c_float)]

myPort = 10756

sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
addr = ('localhost',myPort)
#sock.sendto(data,addr)

presdata = PurgeData()
presdata.press = 0
presdata.ticks = 100

for msg in range(1,20):
presdata.press = presdata.press+1
presdata.ticks = presdata.ticks+1
presdata.volume = random.random()
sock.sendto(presdata,addr)

#--------------------

#!/usr/bin/python
# Receiver
import socket

from ctypes import *

class PurgeData(Structure):
_fields_ = [("press",c_int), ("ticks",c_int), ("volume",c_float)]

myPort = 10756

sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
addr = ('localhost',myPort)
sock.bind(addr)
presdata=PurgeData()

while True:
data,addr = sock.recvfrom(1024)
memmove(addressof(presdata),data.strip(),len(data.strip()))
print presdata.press, presdata.ticks, presdata.volume

---------------------

When I tried to run this I get some bizarre results:


1 101 0.343009024858
2 102 0.36397305131
3 103 0.495895296335
4 104 0.372055351734
5 105 0.933839201927
6 106 0.931187808514
7 107 0.876732826233
8 108 0.298638045788
1828716544 -754974720 0.183644190431
1845493760 1660944384 0.186560109258
1862270976 1056964608 0.18631502986
1879048192 1728053248 0.186902835965
1895825408 2097152000 0.18658298254
14 114 0.407857120037
15 115 0.833854913712
16 116 0.00646247947589
17 117 0.297783941031
18 118 0.58082228899
19 119 0.61717569828

the received data for the messages 9 thru 13 are not as expected.

I wonder if anyone can see what I am doing wrong?

Appreciate any hints. thanks, srini
 
R

RasikaSrinivasan

BTW - My environment is:

H:\>python
Enthought Canopy Python 2.7.6 | 64-bit | (default, Apr 11 2014, 20:31:44) [MSC v
..1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
 
M

MRAB

friends

I have a pair of simple python programs as follows:

#!/usr/bin/python
# broadcast.py
import socket
from ctypes import *
import random

class PurgeData(Structure):
_fields_ = [("press",c_int), ("ticks",c_int), ("volume",c_float)]

myPort = 10756

sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
addr = ('localhost',myPort)
#sock.sendto(data,addr)

presdata = PurgeData()
presdata.press = 0
presdata.ticks = 100

for msg in range(1,20):
presdata.press = presdata.press+1
presdata.ticks = presdata.ticks+1
presdata.volume = random.random()
sock.sendto(presdata,addr)

#--------------------

#!/usr/bin/python
# Receiver
import socket

from ctypes import *

class PurgeData(Structure):
_fields_ = [("press",c_int), ("ticks",c_int), ("volume",c_float)]

myPort = 10756

sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
addr = ('localhost',myPort)
sock.bind(addr)
presdata=PurgeData()

while True:
data,addr = sock.recvfrom(1024)
memmove(addressof(presdata),data.strip(),len(data.strip()))
print presdata.press, presdata.ticks, presdata.volume

---------------------

When I tried to run this I get some bizarre results:


1 101 0.343009024858
2 102 0.36397305131
3 103 0.495895296335
4 104 0.372055351734
5 105 0.933839201927
6 106 0.931187808514
7 107 0.876732826233
8 108 0.298638045788
1828716544 -754974720 0.183644190431
1845493760 1660944384 0.186560109258
1862270976 1056964608 0.18631502986
1879048192 1728053248 0.186902835965
1895825408 2097152000 0.18658298254
14 114 0.407857120037
15 115 0.833854913712
16 116 0.00646247947589
17 117 0.297783941031
18 118 0.58082228899
19 119 0.61717569828

the received data for the messages 9 thru 13 are not as expected.

I wonder if anyone can see what I am doing wrong?

Appreciate any hints. thanks, srini
I don't understand why you're using the .strip method. That's for
stripping whitespace from text, but you're not sending and receiving
text, you're sending and receiving binary data.

Personally, I'd use the struct module.
 
R

RasikaSrinivasan

Of course!!!! Cut and paste issue. Anyhow, i will look at the struct module. cheers, srini

friends

I have a pair of simple python programs as follows:


# broadcast.py
import socket
from ctypes import *
import random

class PurgeData(Structure):
_fields_ = [("press",c_int), ("ticks",c_int), ("volume",c_float)]
myPort = 10756
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

addr = ('localhost',myPort)
#sock.sendto(data,addr)

presdata = PurgeData()
presdata.press = 0
presdata.ticks = 100

for msg in range(1,20):
presdata.press = presdata.press+1
presdata.ticks = presdata.ticks+1
presdata.volume = random.random()





# Receiver
import socket

from ctypes import *

class PurgeData(Structure):
_fields_ = [("press",c_int), ("ticks",c_int), ("volume",c_float)]
myPort = 10756
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

addr = ('localhost',myPort)
sock.bind(addr)
presdata=PurgeData()

while True:
data,addr = sock.recvfrom(1024)

print presdata.press, presdata.ticks, presdata.volume



When I tried to run this I get some bizarre results:


1 101 0.343009024858
2 102 0.36397305131
3 103 0.495895296335
4 104 0.372055351734
5 105 0.933839201927
6 106 0.931187808514
7 107 0.876732826233
8 108 0.298638045788
1828716544 -754974720 0.183644190431
1845493760 1660944384 0.186560109258
1862270976 1056964608 0.18631502986
1879048192 1728053248 0.186902835965
1895825408 2097152000 0.18658298254
14 114 0.407857120037
15 115 0.833854913712
16 116 0.00646247947589
17 117 0.297783941031
18 118 0.58082228899
19 119 0.61717569828

the received data for the messages 9 thru 13 are not as expected.

I wonder if anyone can see what I am doing wrong?

Appreciate any hints. thanks, srini

I don't understand why you're using the .strip method. That's for

stripping whitespace from text, but you're not sending and receiving

text, you're sending and receiving binary data.



Personally, I'd use the struct module.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,044
Messages
2,570,388
Members
47,052
Latest member
ketan

Latest Threads

Top