views:

334

answers:

2

Hello,

I know it is a general question but I need your recommodations about a TCP server/client application.

The server is only supposed to handle one connection at a time. Server is sending live images (one frame is approx. 50K and 20 frames per second) to connected client.

Actually at the startup of the server and client applications connection is established and this connection is supposed to be keep alive forever,in theory.

My point is that since server is sending live images, delay must be minimum, so what is best practice for writing such a (simple) tcp server and client, and how to serialize/deserialize images so that "minumum delay" aim is achieved?

Thanks in advance,

Regards

+1  A: 

One way is to send the data using UDP instead of TCP.

If you do, it's possible that some of the UDP packets will be lost (dropped by the network), so your code will need a method (e.g. sequence number in the packet headers) to detect lost packets.

If a TCP packet is lost, then TCP will retransmit it, which results in a delay. It's possible that for your application, when a packet is lost you might just want to do without that lost packet, not retransmit it, don't display this video frame (or display only the partial frame), and go on to display the next frame.

It depends on the application:

  • Are you streaming canned/prerecorded/non-real-time video, where you want to receive/display every frame even if some of them cause a delay?

  • Are you streaming live video, where you want to display the current frame in near-real-time (and even if some previous frames were lost, you don't want to delay while they're retransmitted)?


In terms of winsock architecture, the TransmitFile or TransmitPackets APIs are quite efficient: they're executing in the kernel, instead of causing round trips between your user mode code and O/S kernel mode code as each buffer is transmitted.


"minumum delay" aim is achieved

You may want some delay, to avoid jitter: better to have a small (e.g. 150 msec) fixed delay, than a delay which varies from 2 to 120 msec. See http://www.google.ca/search?hl=en&q=jitter+network

ChrisW
thank you for reply, at first I was planning to use UDP but I got a feedback that states when number of connections on the same PC is over 5 than CPU usage is approximating to 100%. Anyway, can you please a little elaborate this transmitPackets API and how to read at the client side?
AFgone
Is that more then 5 connections sending, or receiving?
ChrisW
According to the Remarks, the behaviour (i.e. the way in which it is optimized) of TransmitPackets varies depending on whether you're using a server-style O/S or a client-style O/S: http://msdn.microsoft.com/en-us/library/ms740566(VS.85).aspx
ChrisW
The client will receive a sequence of packets. The packets are probably/usually received in sequence: they're sent an sequence, but may (depending on the type of network) become out-of-sequence on the network. I've forgotten what you can do to optimize performance of receiving a sequence of packets. However it may be the server (transmitter) that's more in need of optimization than the client, since the client typically only has one concurrent connection to one server, whereas a server may have multiple connections to multiple concurrent clients.
ChrisW
I am using one conecction for one server but in the same machine there can be more than 5 servers running
AFgone
A: 

I'm not super-authoritative, but...

Let's do some math. 50K per image (that's bytes, right?) at 20 frames per second is about 1 megabyte per second. In communications terms, that's 10 megabits per second. That's a fair-ish bit, but very do-able. Be sure you've got 100 megabit equipment throughout.

The fastest APIs for this are the boring old blocking "send" and "recv" calls (*1). There are other calls which do the Windows IO type completion ports and whatnot; don't use them for this application (because (a) they are overkill and (b) they make an app be more scalable)

Consider turning off the Nagle (the NODELAY option).

(*1) probably. The actual literature on the actual fastest Winsock calls is severely lacking.