Establishing a server-client connection for a multiplayer game

Posted on 2009-04-30
Last Modified: 2013-12-14
am trying to create a chess-like two-person multiplayer game using Microsoft Visual C++. I need some guidance on how to create a connection between the two persons(using the internet). One person will be the server (which will be able to change the properties of the game) and the other one the client. What is the easiest and safest way to establish such a connection and what documentation should I look for?

Thanks a lot.
Question by:Chrysaor
    1 Comment
    LVL 1

    Accepted Solution

    Easiest way to do that is sockets, look for connect,send,recv,etc. functions family, they are available both in windows and *nix.

    And example below.
    #include <windows.h>
    #pragma comment(lib,"ws2_32.lib")
    int main(int argc, CHAR* argv[])
    if(argc != 3)
    	return 0;
    if(*argv[1] != '0')
    	ip = inet_addr(argv[1]);
    WORD port = htons(atoi(argv[2])); // should be right order of bits;
    WSADATA wsa;
    	return 0;
    sockaddr_in addra = {0};
    addra.sin_family = AF_INET;
    addra.sin_port = htons(port);
    addra.sin_addr.S_un.S_addr = ip;
    int addra_size = sizeof(addra);
    if(ip == INADDR_ANY) // choose between client and server,
    	// its a server part
    	SOCKET client = 0;
    	// starting a loop for clients
    	while( SOCKET_ERROR != (client = accept(s,(sockaddr*)&addra,&addra_size)) )
    		// got one
    		char buff[513];
    		recv(client,buff,512,0); // get some data from client. it will be waiting until any data arrives or client disconnects;
    		//close client socket and wait for another one;
    	// its a client part
    	if( SOCKET_ERROR != connect(s,(sockaddr*)&addra,addra_size) )
    		// send a string after successfull connection.
    		//close all and exit
    return 0;

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
    Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
    The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
    The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now