49

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)
Nick Bolton
  • 38,276
  • 70
  • 174
  • 242

5 Answers5

50

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.

Andy Sukowski-Bang
  • 1,402
  • 7
  • 20
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • 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? – Nick Bolton Mar 19 '09 at 14:16
  • @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 Mar 19 '09 at 14:33
  • 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. – Nick Bolton Mar 19 '09 at 16:17
  • 1
    God bless Beej. His tutorial was my first exposer to socket programming on *NIX when I was little. I don't use c/c++ enough to fully retain everything so when ever I need a refresher I always look to Beej for guidance. – William Mar 22 '12 at 22:25
  • 1
    The link used in this answer is outdated, use [this](https://beej.us/guide/bgnet/html//index.html#client-server-background) one now. – divinelemon Mar 07 '21 at 20:46
15

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>
    
  2. 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.

Andre Kampling
  • 5,476
  • 2
  • 20
  • 47
5

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

bayda
  • 13,365
  • 8
  • 39
  • 48
  • 1
    Are these samples appropriate? http://www.boost.org/doc/libs/1_36_0/doc/html/boost_asio/example/iostreams/daytime_server.cpp http://www.boost.org/doc/libs/1_36_0/doc/html/boost_asio/example/iostreams/daytime_client.cpp – Nick Bolton Mar 19 '09 at 14:10
1

Although many year ago, clsocket seems a really nice small cross-platform (Windows, Linux, Mac OSX): https://github.com/DFHack/clsocket

Erwin Coumans
  • 1,766
  • 14
  • 22
0

Here are some examples for:

1) Simple
2) Fork
3) Threads

based server:

http://www.martinbroadhurst.com/server-examples.html

Community
  • 1
  • 1
Guy L
  • 2,824
  • 2
  • 27
  • 37