3

I am new to C++ network programming but have experience with Java sockets etc.

I have been trying to write a simple TCP echo server in C++ but cannot really make any progress. I've tried looking at some code like at http://cs.baylor.edu/~donahoo/practical/CSockets/practical/ but cannot get anything to work.

Can anyone give me some simple C++ code to get started with for something like a TCP echo server? I do not really understand how to even get started.

Thanks in advance.

tree-hacker
  • 5,351
  • 9
  • 38
  • 39
  • There is one here - http://www.paulgriffiths.net/program/c/srcs/echoservsrc.html – Steve Townsend Dec 06 '10 at 19:32
  • When I tried compiling that I got errors for: Readline and Writeline for some reason – tree-hacker Dec 06 '10 at 19:33
  • @Tree-hacker It doesn't get more simple than what you linked to. http://cs.baylor.edu/~donahoo/practical/CSockets/practical/TCPEchoServer.cpp Do you need a homework tag? – Byron Whitlock Dec 06 '10 at 19:33
  • @tree 1) what are the errors 2) if you want to code in c++ you need to know how to deal with compilation errors and fix them. – Byron Whitlock Dec 06 '10 at 19:35
  • the errors were: Undefined reference to 'Readline' and Undefined reference to 'Writeline' – tree-hacker Dec 06 '10 at 19:37
  • Those are defined in helper.c on that same link. Are you awake? – Byron Whitlock Dec 06 '10 at 19:43
  • 2
    @tree-hacker: @Steve's example shows three files. "echoserv.c", "helper.c" are both source files that need to be compiled. "helper.h" is a *header* file, which will need to be available, in the same directory, as "echoserv.c" is compiled. In short, you need to put all three files in the same directory, then pass both ".c" files to the compiler at the same time -- something like `gcc -o echoserv echoserv.c helper.c` – Lee Dec 06 '10 at 19:49
  • @tree-hacker: if you're struggling with compilation issues, it might be too early to tackle network programming in c/c++. You might consider starting smaller... and working your way up to the tuff stuff. (no intent to be offensive here - just a friendly suggestion) – Lee Dec 06 '10 at 19:54

3 Answers3

3

The terms "simple" and "C++ TCP Echo Server" don't belong together in the same sentence. There is no such thing.

The sample that you are looking at is probably as close to "simple" as you're going to get (if you want to get into the nitty-gritty). Using a library that handles all the heavy lifting for you would make things easier (but far less educational). I would probably check out Boost.Asio (and the example Blocking TCP Echo Server example).

If things aren't making sense, you should probably go back and brush up on your C++ network programming until you get to the point that things start clicking.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • 1
    I disagree, an echo server is simple, and C++ makes it even simpler than doing it in C. See e.g. the old C source code from Steven's Unix Network Programming and adapt it for C++. (google to find it). – Erik Alapää Mar 10 '15 at 12:37
1

I would recommend that you look into the boost framework -- it provides the same kind of "indispensable utility classes" that the JDK provides to Java programmers.

There are many tutorials available for various aspects of boost. Here's one on getting started with the asynchronous i/o components.

If you want to jump straight into the (very simple) server socket example, here it is.

halfer
  • 19,824
  • 17
  • 99
  • 186
Lee
  • 13,462
  • 1
  • 32
  • 45
1

To learn network programming, I highly recommend you see if you can buy, beg, borrow, or steal (stay away from my copy) a copy of Richard W Stevens book Unix Network Programming (note that after the first edition, the subsequent editions are split into volumes, so make sure you get the right volume for TCP/IP).

I found it to be a detailed resource for learning TCP programming, primarily on Unix/POSIX systems. If memory servers, it has some code for a TCP echo client and server written in C that it uses for some of its examples. You can find the source code for the book here - dig around the Makefile and source code in the tcpcliserv dir:

http://www.kohala.com/start/unpv12e.html

Edit: I realize its not a C++ version you asked for, but if I'm right that your end goal is learning network programming, learning it in C should be a fine stepping stone to C++ ....

B

Bert F
  • 85,407
  • 12
  • 106
  • 123