4

I'm looking for a simplest (and fastest) example of TCP socket programming for windows, c or c++, whichever can get it accomplished faster, sending trival data, for example 1 byte, or several bytes, but in one packet. It's for research purposes. I googled and found several examples, however every single of out them looks a bit different, some are in C, some are in C++, some use ZeroMemory (from windows), some use memset, some of them assign data in different ways, so while I can find examples of winsock in c/c++ and while I'm not an expert in socket programming - I'm not sure what's the absolutely minimalistic c/c++ code to get it accomplish in a fastest way possible.

I know that UDP would be much faster, but it needs to be reliable at the same time, hence I'm looking for TCP.

I guess I could try each of them and try to time them, but was wondering if some socket/winsock expert here would have a super simple server/client in C/C++ with some timing function (high resolution) at the end. I say super simple, because I'm trying to determine how fast (and the fastest way) can socket transmit on my machines, of course it can include turning off Nagle's algorithm, which is what I would like to do anyway. I'm not sure what other tricks people use.

Thanks.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
ra170
  • 3,643
  • 7
  • 38
  • 52
  • What research have you done exactly? It would be beneficial to point out the code that you're intending to use and come back with your conclusions...each and every one of the samples you found in google, sure one uses zeromemory, the other uses memset, at the end of the day, the code you've seen fundamentally uses sockets.... – t0mm13b Oct 20 '10 at 14:35
  • Just a side note: using TCP to dispatch small packets would be overkill. Try to bufferize them as much as you can, otherwise you would wase a lot of time in handling packet headers.. – Jack Oct 20 '10 at 14:37
  • @tommieb75 - not much of a research so far, as stated this the beginning, yes, they all use sockets, but the difference could be microsecond which is a lot of me. Anyway, thank you for comment. – ra170 Oct 20 '10 at 14:56
  • There's also RUDP which is reliable and fast since it omits the connection setup: http://en.wikipedia.org/wiki/Reliable_User_Datagram_Protocol – Peter G. Oct 20 '10 at 15:25
  • see also http://stackoverflow.com/questions/107668/what-do-you-use-when-you-need-reliable-udp – Peter G. Oct 20 '10 at 15:27

8 Answers8

2

Try Len Holgate's socket server framework. I believe he has commercialized this in a packaged version but this should be a good place to start. There is a client implementation tutorial included. This is not the simplest code but if you are interested in maximizing performance, simple code may not meet your needs.

You will have to add your own timing support, but that's likely true for any possible off-the-shelf package.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
  • Thanks, I'll check it out. What I've meant by simplest and fastest was is exactly that, it might be necessary to add/tweak it for performance. – ra170 Oct 20 '10 at 15:00
  • Steve, thanks for the link, probably a better jumping off point is this link: http://www.serverframework.com/products---the-free-framework.html which has the latest free code plus full details of the commercial version. – Len Holgate Oct 20 '10 at 15:18
  • @Len - you are welcome - your online postings were v helpful to me when implementing IOCP server/client code – Steve Townsend Oct 20 '10 at 15:19
1

Boost Asio is probably your best bet. it's a very good library with timing support and everything you should need to get going.

edit: I know that this isn't a pre-built client/server which is exactly what you are looking for, but Asio makes it extremely easy to get what you want out of a few lines of code.

Kyle C
  • 1,627
  • 12
  • 25
  • "Extremely easy" ? No... It has a LOT of internal plumbing. Personnaly I took at least one month before using it - with a sensation of blindness. If you search a very easy example, I advice you to read some C examples for sending an UDP packet etc. – yO_ Nov 28 '18 at 17:04
1

The most minimal examples of which I am aware are in Beej's Guide.

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
  • Thank you, I found that one as well, but wasn't sure if that was in fact the simplest. Thank again. – ra170 Oct 20 '10 at 14:58
1

If you want an off the shelf product, look at any of the messaging products available. They require the least amount of coding to get going, typical examples are:

Open Source:

  1. OpenDDS - based on the DDS protocol (very high performance - used in things like submarine, ship control systems etc.) Their implementation is slightly slower than raw boost::asio, however for ease of use and the bells and whistles, hard to beat.
  2. ZeroMQ - similar to DDS, but based on the MQ protocol, again very fast (millions of messages/sec), MQ is established, but ZeroMQ is not there yet.
  3. AMQP - I believe you'll be able to find something from Red Hat in this space, again very fast, and a new protocol.

Commercial:

  1. Tibco RV: hard to beat, except by hardware vendors
  2. 29West - hardware (and software - thought I've never personally played with it)
  3. Solace - hardware
  4. Tervella - hardware

The last three assumes you've got a few million bucks lying around! ;)

Nim
  • 33,299
  • 2
  • 62
  • 101
0

I've just implemented a network solution using socket++, and it works pretty well. I believe that it's the basis for boost asio, so if you don't want to install all of boost, you can check it out.

The point of the library is that you can use a stream with your socket, sending data as you would to std::cout or std::cerr.

EDIT: if you're using more recent versions of windows, then this library would need some tweaking to compile (it works fine as-is for XP, but apparently some networking code got moved around for win vista and 7).

mmr
  • 14,781
  • 29
  • 95
  • 145
  • yes, I use windows 7 and windows server 2003, but thank you. I will check it out today, sometime soon. – ra170 Oct 20 '10 at 14:58
  • ah, ok. The changes involve removing most of the unix-y code (like forking), linking to ws2_32.lib, getting rid of all the tests, and making sure that the WSAE identifiers are working. So maybe not that simple to get working in win7, but once it's there, it's a case of "myStream << "Here is a string to send on the network! << std::endl; mystream.flush()" – mmr Oct 20 '10 at 15:03
0

Before writing the third comment, I collect them in an answer

Community
  • 1
  • 1
Peter G.
  • 14,786
  • 7
  • 57
  • 75
0

ucspi-tcp

Oldie but goodie, written in C, qmail is widely used mail server is based on it.

https://cr.yp.to/ucspi-tcp.html

Jorge Ramirez
  • 81
  • 3
  • 3
0

You can check Push Framework.

charfeddine.ahmed
  • 526
  • 2
  • 8
  • 16