Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed3.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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'algorithm': 0.03; 'subject:Python': 0.05; 'bytes.': 0.07; 'try:': 0.07; 'python': 0.09; '16)': 0.09; 'length.': 0.09; 'valueerror': 0.09; 'received:124.108': 0.16; 'true:': 0.16; 'byte': 0.17; 'bytes': 0.17; 'implementing': 0.17; 'specify': 0.17; '(in': 0.18; 'translate': 0.20; "i've": 0.23; 'second': 0.24; 'implemented': 0.27; 'i.e.': 0.27; 'coded': 0.29; 'str': 0.29; 'this.': 0.29; "i'm": 0.29; 'server.': 0.32; 'print': 0.32; 'to:addr:python- list': 0.33; 'hi,': 0.33; 'received:172.16': 0.34; 'server': 0.35; 'except': 0.36; 'skip:{ 10': 0.36; 'client': 0.36; 'charset:us- ascii': 0.36; 'to:addr:python.org': 0.39; 'help': 0.40; 'first': 0.61; 'side': 0.61; 'header:Message-Id:1': 0.62; 'received:': 0.62; 'stuck': 0.65; 'receive': 0.71; 'payload': 0.84; 'received:my': 0.93 From: Ihsan Junaidi Ibrahim Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Subject: Python recv loop Date: Mon, 11 Feb 2013 08:48:24 +0800 To: python-list@python.org Mime-Version: 1.0 (Mac OS X Mail 6.2 \(1499\)) X-Mailer: Apple Mail (2.1499) 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360544258 news.xs4all.nl 6852 [2001:888:2000:d::a6]:50573 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38625 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{...} 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 =3D sock.recv(4) try: nbuf =3D int(mlen, 16) except ValueError as e: print 'invalid length type' return -1 while True: buf =3D sock.recv(nbuf) if not buf: break slen =3D len(buf) str =3D "{0} bytes received: {1}".format(slen, buf) print str=