Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.datemas.de!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'binary': 0.07; 'method.': 0.07; 'socket': 0.07; '101': 0.09; '103': 0.09; 'expected.': 0.09; 'msg': 0.09; 'python': 0.11; 'random': 0.14; '107': 0.16; 'addr': 0.16; 'data,addr': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'personally,': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'true:': 0.16; 'skip:# 20': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'text,': 0.24; 'header:In-Reply- To:1': 0.27; 'tried': 0.27; 'wonder': 0.29; 'skip:( 20': 0.30; 'ctypes': 0.31; 'struct': 0.31; 'anyone': 0.31; 'class': 0.32; 'run': 0.32; 'skip:# 10': 0.33; "i'd": 0.34; 'received:84': 0.35; 'but': 0.35; 'module.': 0.36; 'subject:data': 0.36; 'doing': 0.36; 'skip:- 20': 0.37; 'follows:': 0.38; 'skip:[ 10': 0.38; 'to:addr :python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'simple': 0.61; "you're": 0.61; 'email addr:gmail.com': 0.63; 'skip:m 50': 0.68; '100': 0.79; 'friends': 0.81; '102': 0.84; 'receiver': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=R5mNGLhX c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=1oDRhzKAKukA:10 a=ihvODaAuJD4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=pGLkceISAAAA:8 a=aY1KXlFn2SAc_M2M0OYA:9 a=QEXdDO2ut3YA:10 a=MSl-tDqOz04A:10 X-AUTH: mrabarnett:2500 Date: Fri, 30 May 2014 00:09:21 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Binary data exchange References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 90 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1401405151 news.xs4all.nl 2925 [2001:888:2000:d::a6]:52809 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:72259 On 2014-05-29 23:08, RasikaSrinivasan@gmail.com wrote: > 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.