[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: 2 Player mode



Hi,

well, the SDL_net seems to make things quite easy. Assume we have a way to let the user decide if he will be the server or the client. Also he needs to add the server-IP or hostname, when he is client. Then it goes like this:

for both sites:
---------------

somewhere quite early:

if(SDL_Init(0)==-1) {
    printf("SDL_Init: %s\n", SDL_GetError());
    exit(1);
}
if(SDLNet_Init()==-1) {
    printf("SDLNet_Init: %s\n", SDLNet_GetError());
    exit(2);
}

when we are done with all:

SDLNet_Quit();


Server site -----------

// create a listening TCP socket on port 9999 (server)
IPaddress ip;
TCPsocket tcpsock;

if(SDLNet_ResolveHost(&ip,NULL,9999)==-1) {
    printf("SDLNet_ResolveHost: %s\n", SDLNet_GetError());
    exit(1);
}

// now we bind the socket to a local port
tcpsock=SDLNet_TCP_Open(&ip);
if(!tcpsock) {
    printf("SDLNet_TCP_Open: %s\n", SDLNet_GetError());
    exit(2);
}

// accept a connection coming in on server_tcpsock
TCPsocket new_tcpsock;

new_tcpsock=SDLNet_TCP_Accept(server_tcpsock);
if(!new_tcpsock) {
    printf("SDLNet_TCP_Accept: %s\n", SDLNet_GetError());
}
else {
    // communicate over new_tcpsock
}



Client site
-----------

// connect to hostname at port 9999 using TCP (client)
IPaddress ip;
TCPsocket tcpsock;

if(SDLNet_ResolveHost(&ip, hostname, 9999)==-1) {
    printf("SDLNet_ResolveHost: %s\n", SDLNet_GetError());
    exit(1);
}

tcpsock=SDLNet_TCP_Open(&ip);
if(!tcpsock) {
    printf("SDLNet_TCP_Open: %s\n", SDLNet_GetError());
    exit(2);
}



Send
----

unsigned long send(void *data, unsigned long count)
{
	// send a hello over sock
	//TCPsocket sock; // global var
	int result;

	result = SDLNet_TCP_Send(sock, data, count);
	if (result < count)
	{
		printf("SDLNet_TCP_Send: %s\n", SDLNet_GetError());
		/* It may be good to disconnect sock because
		   it is likely invalid now. */
	}

	return result;
}


Receive -------

unsigned long receive(void *data, unsigned long count)
{
	// receive some text from sock
	//TCPsocket sock; // global var
	int result;

	result = SDLNet_TCP_Recv(sock, data, count);
	if (result <= 0)
	{
		/* An error may have occured, but sometimes you can
		   just ignore it
		   It may be good to disconnect sock because it
		   is likely invalid now. */
	}
	printf("Received: \"%s\"\n",msg);

	return result;
}




Jens Granseuer wrote:

On 04.10.2006 00:53, Henk Jonas wrote:

The week before I've promised to look into the 2 Player mode and actually I did. I have something which works one, two rounds between 2 Palm devices over Bluetooth, then it crashes.

[...]

I've made a diff to the 0.4.9:

what I did:

added a new game flag: GI_2PLAY
added a flag to the player: local or not
added functions to send and receive data over a given transport

[...]

Please check the diff and ask me in the list. Sending the history was a better thing as the player can see what the other site did, if he like.

I would like to know what happens when you implement it for 2 desktop players over TCP/IP.


Great (except I no longer have any good excuses for not taking a closer
look at SDL_net...) ;-)

I'll try to get my head around the networking stuff and see whether I can
provide a TCP/IP implementation. Any helping hands welcome, of course.
(And just in case anybody else wants to look at Henk's patch, note that
it's in reverse, so apply with -r. Of course, there's also lots of
Palm-specific stuff in there which makes it a bit messy, plus it
doesn't apply cleanly against current CVS...)

Thanks!

Jens


-- ------------------------------------------------------------------------- Henk Jonas Palm OS ® certified developer

  metaview@xxxxxx                                   www.metaviewsoft.de
-------------------------------------------------------------------------