C++/Client Server
Expert: Ralph McArdell - 2/10/2012
QuestionHi,
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.
AnswerWell 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 (
http://www.wireshark.org/)
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:
http://www.prasannatech.net/2008/07/socket-programming-tutorial.html
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 (
http://www.cs.wustl.edu/~schmidt/ACE.html).
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 (
http://www.boost.org/) Asio library
(
http://www.boost.org/doc/libs/1_48_0/doc/html/boost_asio.html)
Newer C++ style library for networking and asynchronous I/O
- The POCO Libraries (
http://pocoproject.org/)
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 (
http://curl.haxx.se/)
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 (
http://www.zeromq.org/).
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:
http://www.gillius.org/gne/
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.