views:

4060

answers:

4

I need to quickly implement a very small C or C++ TCP server/client solution. This is simply to transfer literally an array of bytes from one computer to another - doesn't need to be scalable / over-complicated. The simpler the better. Quick and dirty if you can.

I tried to use the code from this tutorial, but I couldn't get it to build using g++ in Linux: http://www.linuxhowtos.org/C_C++/socket.htm

If possible, I'd like to avoid 3rd party libraries, as the system I'm running this on is quite restricted. This must be C or C++ as the existing application is already implemented.

Thanks to emg-2's answer, I managed to make the above mentioned code sample compatible with C++ using the following steps:

Add these headers to both client and server:

#include <cstdlib>
#include <cstring>
#include <unistd.h>

In server.c, change the type of clilen to socklen_t.

int sockfd, newsockfd, portno/*, clilen*/;
socklen_t clilen;

In client.c, change the following line:

if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0) { ... }

To:

if (connect(sockfd,(const sockaddr*)&serv_addr,sizeof(serv_addr)) < 0)
+5  A: 

try boost::asio lib (http://www.boost.org/doc/libs/1_36_0/doc/html/boost_asio.html) it have lot examples.

bb
Are these samples appropriate?http://www.boost.org/doc/libs/1_36_0/doc/html/boost_asio/example/iostreams/daytime_server.cpphttp://www.boost.org/doc/libs/1_36_0/doc/html/boost_asio/example/iostreams/daytime_client.cpp
nbolton
+8  A: 

I've used Beej's Guide to Network Programming in the past. It's in C, not C++, but the examples are good. Go directly to section 6 for the simple client and server example programs.

Bill the Lizard
+1 for beej's. Good stuff.
Kyle Walsh
Thanks - I found a sample (http://beej.us/guide/bgnet/output/html/multipage/clientserver.html#simpleserver) but I'm not sure if it's overkill, do I really need all that code?
nbolton
@nickbolton2705: Probably, yes. Beej's code has error handling included, which is part of the reason I like it so much. Also note that the server supports multiple concurrent connections, and IPv4 and IPv6.
Bill the Lizard
Yeah, this would be great for an elegant solution - I noticed one of the features is serialization, which could be useful to me should I need to implement this properly at some point.
nbolton
A: 

You requested c++, but the link is c. Which do you want? If you want c and can't get that to compile, you probably won't get anything to compile. Maybe post your errors (and the what sort of system you're trying to compile on - a cursory look at that source indicates that *nix systems should eat that up like nothing)

KevinDTimm
This sample was the closest thing I could find, I would prefer C++ sample which is the equivalent level of simplicity as that sample.
nbolton
my response to that would be that you should use c instead of c++ as all you want is simple (though I see you've found a way to modify the code to work). just a note to all, c is not a subset of c++ (see other discussions) so the conversion may not be trivial (even when the example is)
KevinDTimm
great, thanks, changed question to suit this
nbolton
+4  A: 

If the code should be simple, then you probably asking for C example based on traditional BSD sockets. Solutions like boost:asio are IMHO quite complicated when it comes to short and simple "hello world" example.

To compile examples you mentioned you must make simple fixes, because you are compiling under C++ compiler. I'm referring to following files: http://www.linuxhowtos.org/data/6/server.c http://www.linuxhowtos.org/data/6/client.c from: http://www.linuxhowtos.org/C_C++/socket.htm

  1. Add following includes to both files:
    #include <cstdlib>
    #include <cstring>
    #include <unistd.h>
  1. In client.c, change the line:

    if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0) { ... }

to:

if (connect(sockfd,(const sockaddr*)&serv_addr,sizeof(serv_addr)) < 0)
{ ... }

As you can see in C++ an explicit cast is needed.

Also, in server.c I had to change the type of clilen to socklen_t, and cast the ouput of sizeof(cli_addr);
nbolton
nice concise answer.
Paperino