Thread: help with netcode!

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    2

    help with netcode!

    hey guys, my netcode for this game is extremely terrible, can someone help me improve it? my code is very messy and complicated, sorry.. =[ but any suggestions would be very appriciated!
    Code:
    #include "SDL.h"
    #include <winsock.h>
    #include <fstream.h>
    #include <conio.h>
    
    
    
    int servmain(SDL_Surface* pSurface);
    int climain(SDL_Surface* pSurface, char addy);
    
    int main( int argc, char* argv[] )
    {
        SDL_Init ( SDL_INIT_EVERYTHING );
      atexit ( SDL_Quit ) ;
      
      SDL_Surface* pSurface = SDL_SetVideoMode ( 640 , 480 , 8, SDL_HWSURFACE | SDL_DOUBLEBUF );
      SDL_Surface* intro = SDL_LoadBMP("bmp3.bmp");
      SDL_BlitSurface(intro, NULL, pSurface, NULL);
      SDL_Flip(pSurface);
    
      char addy;
      ifstream getconfig("config.txt");
      int cliserv;
      getconfig>>cliserv;
      if(cliserv == 0)
    	  servmain(pSurface);
      else
      {
    	 // getconfig.getline(&addy,15,'\n');
    	  char decoy = 'z';
    	  climain(pSurface, decoy);
      }
    	getch();
      return 0;
    }
    
    //************************ SERVER ************************************************
    
    int servmain(SDL_Surface* pSurface)
    {
    	WORD version = MAKEWORD(1,1);
    	WSADATA wsaData;
    	int nRet;
    
    
    	//
    	// First, we start up Winsock
    	//
    	WSAStartup(version, &wsaData);
    
    
    	//
    	// Next, create the socket itself
    	//
    	SOCKET listeningSocket;
    
    	listeningSocket = socket(AF_INET,		// Go over TCP/IP
    				 SOCK_STREAM,		// Socket type
    				 IPPROTO_IP);		// Protocol
    	if (listeningSocket == INVALID_SOCKET) {
    		printf("Error at socket()");
    		WSACleanup();
    		return 0;
    	}
    
    	
    	//
    	// Use SOCKADDR_IN to fill in address information
    	//
    	SOCKADDR_IN saServer;
    	
    	saServer.sin_family = AF_INET;
    	saServer.sin_addr.s_addr = INADDR_ANY;		// Since this is a server, any address will do
    	saServer.sin_port = htons(8888);		// Convert int 8888 to a value for the port field
    
    
    	//
    	// Bind the socket to our local server address
    	//
            nRet = bind(listeningSocket, (LPSOCKADDR)&saServer, sizeof(struct sockaddr));
    	if (nRet == SOCKET_ERROR) {
    		printf("Error at bind()");
    		WSACleanup();
    		return 0;
    	}
    
    	
    	//
    	// Make the socket listen
    	//
    	nRet = listen(listeningSocket, 10);		// 10 is the number of clients that can be queued
    	if (nRet == SOCKET_ERROR) {
    		printf("Error at listen()");
    		WSACleanup();
    		return 0;
    	}
    	
    	
    	//
    	// Wait for a client
    	//
    	SOCKET theClient;
    
    	theClient = accept(listeningSocket,
    			   NULL,			// Address of a sockaddr structure (see below)
    			   NULL);			// Address of a variable containing the size of sockaddr
    	if (theClient == INVALID_SOCKET) {
    		printf("Error at accept()");
    		WSACleanup();
    		return 0;
    	}
    
      //******** SERVER VIDEO
    //SDL_Surface* pSurface = SDL_LoadBMP("bmp2.bmp");
      SDL_Event event ;
      
      SDL_Surface* bitmap = SDL_LoadBMP("bmp.bmp");
      SDL_Rect box;
      SDL_Rect box2;
      box.x = 20;
      box.y = 0;
      box2.x = 510;
      box2.y = 0;
      SDL_Rect *pbox = &box;
      SDL_Rect *pbox2 = &box2;
      SDL_Surface* erase = SDL_LoadBMP("bmp2.bmp");
      int otheryrel;
      char recieved;
      char tosend;
      while(1)
      {
        SDL_BlitSurface(erase, NULL, pSurface, NULL);
    	SDL_BlitSurface(bitmap, NULL, pSurface, pbox);
    	SDL_BlitSurface(bitmap, NULL, pSurface, pbox2);
    	SDL_Flip(pSurface);
    
        if( SDL_PollEvent ( &event ) )
        {
          
          if ( event.type == SDL_QUIT ) break;   // **** ADD SEND QUIT TO PEER
    
    	  if( event.type == SDL_MOUSEMOTION) 
    	  {
    		  SDL_MouseMotionEvent motion = event.motion;
    			  //box.x += motion.xrel;
    		      box.y = motion.y;
    			  
    	  }
    	  nRet = recv(theClient,		// Pretend this is connected
    	  &recieved,		// Our string buffer
    	  1, 	// Length of the data in the buffer
    	  0);			// Most often is 0, but see end of tutorial for options
    	  otheryrel=int(recieved);
    	  	  
    	  SDL_MouseMotionEvent motion = event.motion;
    	  tosend = char(motion.y);
    	  nRet = send(theClient,		// Pretend this is connected
    	  &tosend,		// Our string buffer
    	  1, 	// Length of the data in the buffer
    	  0);
          
    	  box2.y =otheryrel;
    	  
    	  
    
    
    	}
      }
    
      	closesocket(theClient);
    	
    	closesocket(listeningSocket);
    
    
    	//
    	// Shutdown Winsock
    	//
    	WSACleanup();
    	return 0;
    }
    
    
    
    //**************** CLIENT **********************************************************************
    
    
    int climain(SDL_Surface* pSurface, char addy)
    {
    		WORD version = MAKEWORD(1,1);
    	WSADATA wsaData;
    	int nRet;
    
    
    	//
    	// Start up Winsock as before
    	//
    	WSAStartup(version, &wsaData);
    
    	
    	//
    	// Store information about the server
    	//
            LPHOSTENT lpHostEntry;
     
            //lpHostEntry = gethostbyaddr(&addy,4,AF_INET);		// Specifying server by its name
    		        unsigned int addr;
    		addr=inet_addr("65.33.251.51");
    		lpHostEntry = gethostbyaddr((char*)&addr,sizeof(addr),AF_INET);
            if (lpHostEntry == NULL) {
    	   printf("Error at gethostbyname()");
    	   WSACleanup();
    	   return 0;
            }
    
    
    	//
    	// Create the socket
    	//
    	SOCKET theSocket;
    
    	theSocket = socket(AF_INET,		// Go over TCP/IP
    			   SOCK_STREAM,		// Socket type
    			   IPPROTO_TCP);	// Protocol
    	if (theSocket == INVALID_SOCKET) {
    		printf("Error at socket()");
    		WSACleanup();
    		return 0;
    	}
    
    	
    	//
    	// Use SOCKADDR_IN to fill in address information
    	//
    	SOCKADDR_IN saServer;
    	
    	saServer.sin_family = AF_INET;
    	saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
    			  // ^ Address of the server being inserted into the address field
    	saServer.sin_port = htons(8888);
    
    
    	//
    	// Connect to the server
    	//
    	nRet = connect(theSocket,
    		       (LPSOCKADDR)&saServer,		// Server address
    		       sizeof(struct sockaddr));	// Length of address structure
            if (nRet == SOCKET_ERROR) {
    	   printf("Error at connect()");
    	   WSACleanup();
    	   return 0;
    	}
    
      //********** CLIENT VIDEO
    
      SDL_Event event ;
      SDL_Surface* bitmap = SDL_LoadBMP("bmp.bmp");
      SDL_Rect box;
      SDL_Rect box2;
      box.x = 20;
      box.y = 0;
      box2.x = 510;
      box2.y = 0;
      SDL_Rect *pbox = &box;
      SDL_Rect *pbox2 = &box2;
      SDL_Surface* erase = SDL_LoadBMP("bmp2.bmp");
      char tosend;
      char recieved;
      int otheryrel;
      while(1)
      {
        SDL_BlitSurface(erase, NULL, pSurface, NULL);
    	SDL_BlitSurface(bitmap, NULL, pSurface, pbox2);
    	  SDL_BlitSurface(bitmap, NULL, pSurface, pbox);
    	SDL_Flip(pSurface);
    
        if( SDL_PollEvent ( &event ) )
        {
          
          if ( event.type == SDL_QUIT ) break;
    
    	  if( event.type == SDL_MOUSEMOTION) 
    	  {
    		  SDL_MouseMotionEvent motion = event.motion;
    		      box2.y += motion.yrel;
    			  
    	  }
    	  SDL_MouseMotionEvent motion = event.motion;
    	  tosend = char(motion.y);
    	  nRet = send(theSocket,		// Pretend this is connected
    	  &tosend,		// Our string buffer
    	  1, 	// Length of the data in the buffer
    	  0);
    	  nRet = recv(theSocket,		// Pretend this is connected
    	  &recieved,		// Our string buffer
    	  1, 	// Length of the data in the buffer
    	  0);			// Most often is 0, but see end of tutorial for options
    	  otheryrel=int(recieved);
          
    	  box.y =otheryrel;
    	  
    	}
      }
    
      closesocket(theSocket);
    
    
    	//
    	// Shutdown Winsock
    	//
    	WSACleanup();
    
    
      return ( 0 );
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You'll do better by posting a snippet and asking a specific question, rather than posting the whole lot and asking someone to revise it in an unspecified way.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed