What is a simple C or C++ TCP server and client example? [closed]
Asked Answered
P

5

50

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)
Platter answered 19/3, 2009 at 14:2 Comment(0)
C
52

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.

Cecilycecity answered 19/3, 2009 at 14:9 Comment(5)
Thanks - I found a sample (beej.us/guide/bgnet/output/html/multipage/…) but I'm not sure if it's overkill, do I really need all that code?Platter
@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.Cecilycecity
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.Platter
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.Panlogism
The link used in this answer is outdated, use this one now.Derivation
M
16

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.

Mordy answered 19/3, 2009 at 14:21 Comment(1)
Also, in server.c I had to change the type of clilen to socklen_t, and cast the ouput of sizeof(cli_addr);Platter
K
5

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

Keturahkeung answered 19/3, 2009 at 14:3 Comment(1)
S
1

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

Sayette answered 19/2, 2017 at 18:30 Comment(0)
R
0

Here are some examples for:

1) Simple
2) Fork
3) Threads

based server:

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

Roundworm answered 12/2, 2014 at 11:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.