You are here:

C++/Client Server


I have developed a game of falling objects which runs on a single machine in Linux using C++.Now, I have to transform it into client-server in which the there will be multiple clients who are going to interact with server.The server will be responsible for maintaining the connection and also sending data to the client.The clients are not competing against each other, but they are playing on their  own machines.The server will send the data and monitor clients movements in the game.So, to start with I have created a simple client server application.I have followed the simple steps of Socket Programming.But, I am facing the problem that, when I try to run client and server on different terminals, the client runs till the connect() and then gets stuck over there, and also the server runs till accept() and gets stuck over there.

Well if you are using the raw socket C API then I have to point out that it is very error prone and it is _very_ easy to make mistakes. Anything to do with networking is complex as there are so many points a failure could occur.

As you give no details of what any errors are and do not show your code (and if it is long and difficult to understand I am not really interested as it would come under being too complex for AllExperts Q&As).

The basics of the server side are:

   - Open socket using socket
   - Set options using setsockopt
   - Bind socket to address using bind (use sockaddr struct for socket address description)
   - Listen using listen for clients requesting connections
   - Accept connections using accept
   - Use connection from accept to communicate with client (send, recv etc.)
   - Close connection when done with close
   - Close socket when all done with close

Note: this is for vanilla Berkeley sockets Winsock API may differ slightly.

For the client the basic sequence is:

   - Open socket using socket
   - Connect to server using connect
   - Use socket handle to communicate with client (send, recv etc.)
   - Close socket when all done with close

In all cases _check_ the returned value from each call and handle any errors.

I repeat: Make sure you have checked for errors returned from each and every socket API call.

Also make sure you have the documentation for each function available (e.g. the man pages) and know both how to check for errors (usually a specific return value and errno will give an error specific code - which codes errno can have and their meaning should be documented for each function).

Make sure that both ends are using the same address - including port - to connect to / listen on.

Make sure that both ends can use the address/port - e.g. are there firewalls in the way?

Make sure that multibyte values are in network order (e.g. by using htons, inet_ptons ) - including address and port values when setting up sockaddr structures.

Maybe use some network diagnostic tools such as Wireshark (

You can download and review basic TCP client and server socket C code examples and compare them with the usage and call sequences in your code. One such site that has code for download in various programming languages is:

However we are using C++ not C and therefore have many options that can make writing networking applications much easier. There are various 3rd party libraries available that can assist with making such application programming less of a chore. Some that come to mind that are freely available are:

   - the ACE library (
     Been around for many years.
     Works with many OSes and compilers.
     C++ style is somewhat dated.
     See also Douglas C. Schmidt and Stephen D. Huston's books "C++ Network Programming" volumes 1 & 2
     and "the ACE Programmer's Guide" by Stephen D. Huston, James CE Johnson and Umar Syyid.

   - The Boost libraries ( Asio library
     Newer C++ style library for networking and asynchronous I/O

   - The POCO Libraries (
     Also a newer style set of libraries specifically aimed at network and Internet based applications

In C there are a few libraries that might be of use:

   cURL - The C URL library (
   Has bindings to many other languages including C++, although the C++ binding library seem not to have been maintained since 2009.

   0MQ - A lightweight but efficient messaging library (
   Core API is a C API but there are bindings for various languages.
Note that writing good, responsive, efficient, reliable and resilient networking code is not an easy thing to do so you might like to look at one or more of the libraries mentioned and also read the "C++ Network Programming" book I mention as they point out many complexities, problems, solutions and application patterns. As you require networking specifically for games you might like to look for network programming libraries specifically aimed at the networking needs of game applications. For example I tried:

   games programming  networking libraries

Which lead to:

which might or might not be of interest (also it seems not to be very active, last update seems to be in 2009!).

A bit of time spent with a search engine might reveal more options in this area - who knows!

Hope this answer might give you some hints and pointers at least.


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2016 All rights reserved.