473,320 Members | 2,094 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Re: Receive data from socket stream

On Mon, 28 Apr 2008 08:30:03 -0500, Nick Craig-Wood <ni**@craig-wood.comwrote:
>Hrvoje Niksic <hn*****@xemacs.orgwrote:
> Nick Craig-Wood <ni**@craig-wood.comwrites:
What you are missing is that if the recv ever returns no bytes at all
then the other end has closed the connection. So something like this
is the correct thing to write :-

data = ""
while True:
new = client.recv(256)
if not new:
break
data += new

This is a good case for the iter() function:

buf = cStringIO.StringIO()
for new in iter(partial(client.recv, 256), ''):
buf.write(new)
data = buf.getvalue()

Note that appending to a string is almost never a good idea, since it
can result in quadratic allocation.

My aim was clear exposition rather than the ultimate performance!

Anyway str += was optimised in python 2.4 or 2.5 (forget which) wasn't
it? I'm not convinced it will be any worse performing than
cStringIO.StringIO.write() which effectively appends to a string in
exactly the same way.

This test agrees with me!

$ python -m timeit -s 's = ""' 'for i in xrange(100000): s+="x"'
10 loops, best of 3: 23.8 msec per loop

$ python -m timeit -s 'from cStringIO import StringIO; s=StringIO()' 'for i in xrange(100000): s.write("x")'
10 loops, best of 3: 56 msec per loop
It's a bit nice that using cStringIO doesn't rely on a very esoteric
optimization in the CPython eval loop, as well as on what references
happen to exist to what objects. For example, one might want to use a
debugger without falling back to the quadratic behavior of repeated
string concatenation. cStringIO wins out in this case.

Jean-Paul
Jun 27 '08 #1
0 1054

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kitchen Bin | last post by:
Hi. I am trying to use Sockets to do multiple Send and Receives via HTTP (not simultaneously). A first pair of Send/Receives works fine and sure enough I receive HTML back, but the next...
0
by: ?lafur Helgi R?gnvaldsson | last post by:
I'm building a server application which accepts socket connections and I ran into some problems. The socket is asynchronous and therefore uses the BeginXXX and EndXXX methods in the Socket class...
2
by: Craig | last post by:
Hi I listen on a port, when data is received I raise an event (OnMessageReceived) in the while loop as follows: private void WaitForConnection() { TcpListener listener = new...
0
by: whetham63 | last post by:
When I run this on my network at home and on my network at work it runs fine. When this is run at a clients network I am receiving a after the first packet is received(There are 2 packets...
1
by: Saso Zagoranski | last post by:
Hi! I have simple client/server game that uses sockets to transfer different messages. The server and the client are running on the same machine.
2
by: Qindong Zhang | last post by:
My socket application blocked at socket.receiver() after received all information from sender. Should socket.Receive() return 0 after no more data available? Note: My socket object was not close on...
6
by: Simon Says | last post by:
Hi all, I've a message object that is serialized to a xml string and communicating between a client and server with a connection-oriented (TCP) socket. Unlike a UDP, in a TCP socket, the...
3
by: Joe Blauth | last post by:
Hi all, I am currently working on a small application that sends messages from a client to a server and receives messages in return. Basically the functionality is made with sockets which is...
16
by: s0suk3 | last post by:
I wanted to ask for standard ways to receive data from a socket stream (with socket.socket.recv()). It's simple when you know the amount of data that you're going to receive, or when you'll receive...
4
by: danomano | last post by:
The code below works fine. As of now I just set the buffer length real big and all is good. However, I hate the impreciseness of that. So my question in general is how do I know what length to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.