[Tux4kids-commits] r1001 - tuxmath/branches/lan/src

akash gangil gangil-guest at alioth.debian.org
Fri May 29 09:06:49 UTC 2009


Author: gangil-guest
Date: 2009-05-29 09:06:49 +0000 (Fri, 29 May 2009)
New Revision: 1001

Modified:
   tuxmath/branches/lan/src/network.c
   tuxmath/branches/lan/src/titlescreen.c
Log:
trying to get the connection right

Modified: tuxmath/branches/lan/src/network.c
===================================================================
--- tuxmath/branches/lan/src/network.c	2009-05-28 22:05:08 UTC (rev 1000)
+++ tuxmath/branches/lan/src/network.c	2009-05-29 09:06:49 UTC (rev 1001)
@@ -8,18 +8,21 @@
 #include <unistd.h>
 #include "SDL.h"
 #include "SDL_net.h"
-
+#include <game.h>
 //*** ipaddress of the server and the port would be taken by the user @ the time he selects "LAN multiplayer" from the options menu..
 
 //***also should I fix the port beforehand or ask it from the user...
 
-int lan_client_connect(char *host,int port)            //here "host" is either the hostname or the ipaddress(of the server) in string
+
+int lan_client_connect(char *host,char *port)            //here "host" is either the hostname or the ipaddress(of the server) in string
 {
+char message[]="Client got connected";
+int len;
 IPaddress ip;
 TCPsocket sock;
 Uint16 portnum;
 
-portnum=(Uint16) strtol(&port,NULL,0);
+portnum=(Uint16) strtol(port,NULL,0);
 
 // initialize SDL
 if(SDL_Init(0)==-1)
@@ -50,23 +53,50 @@
 printf("SDLNet_TCP_Open: %s\n",SDLNet_GetError());
 exit(4);
 }
+	len=strlen(message);
 
+	// strip the newline
+	message[len]='\0';
+	
+	if(len)
+	{
+		int result;
+		
+		// print out the message
+		printf("Sending: %.*s\n",len,message);
 
+		result=SDLNet_TCP_Send(sock,message,len); // add 1 for the NULL
+		if(result<len)
+			printf("SDLNet_TCP_Send: %s\n",SDLNet_GetError());
+	}
 
-return(0);
+
+	SDLNet_TCP_Close(sock);
+	
+	// shutdown SDL_net
+	SDLNet_Quit();
+
+	// shutdown SDL
+	SDL_Quit();
+
+	return(0);
 }
 
 
+
 /***      server connection function    ****/
 
-int lan_server_connect(int port)
+
+int lan_server_connect(char *port)
 {
 IPaddress ip; //int *remoteip;
 TCPsocket server,client;
 //Uint32 ipaddr;
 Uint16 portnum;
+int len;
+char message[1024];
+char waiting[]="WAITING FOR OTHER PLAYER(minimum 2 players required)";
 
-
 // initialize SDL
 if(SDL_Init(0)==-1)
 {
@@ -82,7 +112,7 @@
 }
 
 
-portnum=(Uint16)strtol(&port,NULL,0);
+portnum=(Uint16)strtol(port,NULL,0);
 
 // Resolve the argument into an IPaddress type
 if(SDLNet_ResolveHost(&ip,NULL,portnum)==-1)
@@ -99,38 +129,49 @@
 exit(4);
 }
 
+game_set_start_message(waiting, "", "", "");
+printf("%s\n",waiting);
 while(1)
 {
+
 // try to accept a connection
-client=SDLNet_TCP_Accept(server);
-if(!client)
-{ // no connection accepted
-//printf("SDLNet_TCP_Accept: %s\n",SDLNet_GetError());
-SDL_Delay(100); //sleep 1/10th of a second
-continue;
+ client=SDLNet_TCP_Accept(server);
+ if(!client)
+  {    // no connection accepted
+      //printf("SDLNet_TCP_Accept: %s\n",SDLNet_GetError());
+    SDL_Delay(100); //sleep 1/10th of a second
+    continue;
+  }
+  
+
+              // read the buffer from client
+		len=SDLNet_TCP_Recv(client,message,1024);
+		if(!len)
+		{
+			printf("SDLNet_TCP_Recv: %s\n",SDLNet_GetError());
+			continue;
+		}
+
+		// print out the message
+		printf("Received: %.*s\n",len,message);
+
+		if(message[0]=='Q')
+		{
+			printf("Quitting on a Q received\n");
+			break;
+		}
+                break;
 }
+	SDLNet_TCP_Close(client);	
+	SDLNet_TCP_Close(server);
+        // shutdown SDL_net
+	SDLNet_Quit();
 
-/// get the clients IP and port number
-//remoteip=SDLNet_TCP_GetPeerAddress(client);
-//if(!remoteip)
-//{
-//printf("SDLNet_TCP_GetPeerAddress: %s\n",SDLNet_GetError());
-//continue;
-//}
+	// shutdown SDL
+	SDL_Quit();
 
-/*for testing purpose to check if it is connected to the desired client
-// print out the clients IP and port number
-ipaddr=SDL_SwapBE32(remoteip->host);
-printf("Accepted a connection from %d.%d.%d.%d port %hu\n",
-ipaddr>>24,
-(ipaddr>>16)&0xff,
-(ipaddr>>8)&0xff,
-ipaddr&0xff,
-remoteip->port);
 
-*/
+return 0;
 }
-return(0);
-}
 
 

Modified: tuxmath/branches/lan/src/titlescreen.c
===================================================================
--- tuxmath/branches/lan/src/titlescreen.c	2009-05-28 22:05:08 UTC (rev 1000)
+++ tuxmath/branches/lan/src/titlescreen.c	2009-05-29 09:06:49 UTC (rev 1001)
@@ -853,7 +853,7 @@
 
 int run_lan_menu(void)
 {
-  int mode = -1;
+  int mode = -1,l;
   char host[HIGH_SCORE_NAME_LENGTH * 3];
   char port[HIGH_SCORE_NAME_LENGTH * 3];
   
@@ -885,7 +885,7 @@
     
    // lan_client_set_parameter(HOST, host);
    // lan_client_set_parameter(PORT, port);
-   // lan_client_connect(host,port);
+  //  if((lan_client_connect(host,port))==0)
     game();
    }   
 
@@ -933,7 +933,6 @@
      else
      {NameEntry(port, _("Enter the PORT"),
                        _(""));
-
    // lan_server_connect(port);
        game();}
     break;




More information about the Tux4kids-commits mailing list