Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'algorithm': 0.03; 'exception': 0.03; 'value,': 0.03; 'subject:Python': 0.05; 'binary': 0.05; 'bytes.': 0.07; 'try:': 0.07; 'python': 0.09; '16)': 0.09; 'byte,': 0.09; 'length.': 0.09; 'valueerror': 0.09; 'binary.': 0.16; 'digits.': 0.16; 'format?': 0.16; 'hex': 0.16; 'length,': 0.16; 'true:': 0.16; 'uint32': 0.16; 'string': 0.17; 'wrote:': 0.17; 'fix': 0.17; 'byte': 0.17; 'bytes': 0.17; 'certainly': 0.17; 'implementing': 0.17; 'specify': 0.17; '(in': 0.18; 'translate': 0.20; 'permitted': 0.22; "i'd": 0.22; "i've": 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; '(which': 0.26; 'values': 0.26; 'implemented': 0.27; 'i.e.': 0.27; 'coded': 0.29; 'decimal': 0.29; 'description,': 0.29; 'protocols': 0.29; 'str': 0.29; 'starts': 0.29; 'probably': 0.29; 'this.': 0.29; "i'm": 0.29; 'server.': 0.32; 'print': 0.32; 'to:addr:python-list': 0.33; 'hi,': 0.33; "can't": 0.34; 'server': 0.35; 'pm,': 0.35; 'except': 0.36; 'skip:{ 10': 0.36; "wasn't": 0.36; 'client': 0.36; 'being': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'skip:" 10': 0.40; 'received:192.168': 0.40; 'help': 0.40; 'range': 0.60; 'first': 0.61; 'side': 0.61; 'received:': 0.62; 'stuck': 0.65; 'receive': 0.71; 'received:74.208': 0.71; 'payload': 0.84; 'received:74.208.4.194': 0.84; 'concluded': 0.91; 'imagine': 0.96 Date: Sun, 10 Feb 2013 20:22:15 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Python recv loop References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:ScW1Gi7737UYD/4UEZxxd8Nvmb7sDrjMuBYElk5I3YN 4vZbRtbnvlZZjzoQKw4VSZWDNrW5FLN0GrOzkxsKo62oRtJWu9 PR5dhDXPpplnDbX7nPtMzSw5NcKQlbuzeXd83VTgdh7BpqJHBV FCuwkTxsygLqHD69qRMvclk278kIxMoPvgojmSVGmcjbGveXIL PysFW7XwvxaQ6Jf+y6CJIu5UdumgpKdTgjgAJb7uMfx+6Ty54C 5r/JdETZT5vfC+o/oUHSv5z/oB33m+ovWJWkOddScauPSscQgb NYJZi+HtLlHFtYlMh4VABQ8jZhm1Dv9OaDytiTOXi/TBq3lHg= = 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: 57 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360545756 news.xs4all.nl 6899 [2001:888:2000:d::a6]:58093 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38627 On 02/10/2013 07:48 PM, Ihsan Junaidi Ibrahim wrote: > Hi, > > I'm implementing a python client connecting to a C-backend server and am currently stuck to as to how to proceed with receiving variable-length byte stream coming in from the server. > > I have coded the first 4 bytes (in hexadecimal) of message coming in from the server to specify the length of the message payload i.e. 0xad{...} Exactly how are you sending "hexadecimal" ? If that 0xad (which is only one byte, what about the other 3 ?) is intended to be a C description, then it's certainly not hex, it's binary. And probably little-endian, to boot. That's a mistake, as network protocols almost always use big-endian. So what is the range of values for the length, and how are they actually encoded? Are they uint32 in native binary format? And are you permitted to change the encoding, to fix it? (If it were my choice, I'd make it a printable decimal value, first choice, or printable hex, second choice.) > > I've managed to receive and translate the message length until I reach my second recv which I readjusted the buffer size to include the new message length. > > However that failed and recv received 0 bytes. I implemented the same algorithm on the server side using C and it work so appreciate if you can help me on this. > > # receive message length > print 'receiving data' > mlen = sock.recv(4) > try: > nbuf = int(mlen, 16) That supposes the count is being sent as a printable string of hex digits. That's not what I concluded above. > except ValueError as e: If the count starts as 0xad, I can't imagine why this exception wasn't thrown. > print 'invalid length type' > return -1 > > while True: > buf = sock.recv(nbuf) > > if not buf: > break > > slen = len(buf) > str = "{0} bytes received: {1}".format(slen, buf) > print str > You might need to play with hexlify, if you persist in sending the count in binary. -- DaveA