Things like IP numbers and port numbers should always be backed up within an *.INI type of file so that a person does not ALWAYS have to enter in values if they do not happen to change from one game play to the next game play.
I like to use an extension of *.SET rather than *.INI because Vista does nor mess up the settings in this extension, where it does play with INI files.
Here is my network setup function from what you may pull out a few ideas if you are interested.
int InitialiseNetwork( void )
{
char TempStr[128];
// set up the networking configuration
// NETWORK Set timeout value which is the length of time
// in seconds that the command will wait for the
// connection to complete until giving up
int iTimeout = 20;
// NETWORK Set the error mode which decides whether or not
// to display message boxes when an error occurs
int iErrorMode = ERRORMODE_DISABLE_MESSAGE_BOX;
// Set the UDP mode which decides how UDP packets
// are dealt with; on a per client basis or on a
// per client/per operation basis
int iUdpMode = UDPMODE_PER_CLIENT_PER_OPERATION;
// NETWORK Set the UDP receive buffer size which determines
// the maximum size of incoming UDP packets
int iUdpRecvSize = 1024;
// NETWORK Set the TCP receive buffer size which determines
// the maximum amount of data we can receive in one
// receive operation
int iTcpRecvSize = 1024;
// Set the TCP receive store size which determines
// the maximum amount of unreceived data that can
// be in waiting at any one time.
int iTcpStoreSize = 1024;
// NETWORK Set the number of threads that will be created
// to deal with incoming data. In most cases this
// should be set to the number of processors on your
// computer. MikeNet can determine this for you
// automatically if you set it to 0
int iNoThreads = 0;
dbPrint( );
dbPrint("Select CLIENT or HOST mode (C/H) ");
dbSync ( );dbSync ( );//must be done twice because of double buffering
do {
switch ( dbScanCode ( ) ){
case DIK_C:
iGameMode = ClientMode;
dbPrint("****** CLIENT mode selected ****** ");
dbPrint( );
dbSync ( );dbSync ( );//must be done twice because of double buffering
break;
case DIK_H:
iGameMode = HostMode;
dbPrint("****** HOST mode selected ****** ");
dbPrint( );
dbSync ( );dbSync ( );//must be done twice because of double buffering
break;
default:
iGameMode = NotSelected;
}
dbSleep ( 1 );
}while (iGameMode == NotSelected);
dbSleep ( 1000 );
if ( iGameMode == ClientMode)
{
//Get the client IP number
//If we set this to an empty string
//then MikeNet will find an IP for us
char LocalIP[128];
GetPrivateProfileStringA("ClientSetup","UseIP","127.0.0.1",LocalIP,128,".\\Client.set");
dbPrintC("Current selected IP this machine will try to use = "); dbPrint(LocalIP);
dbPrintC("Enter the IP that you would like this client to use = ");
strcpy(TempStr, dbInput());
dbPrint();
if (*TempStr != 0)
strcpy(LocalIP, TempStr);
WritePrivateProfileStringA("ClientSetup","UseIP",LocalIP,".\\Client.set");//update info
// Get Port Number for the client to use
// If the local port of the client is set
// to NULL, then MikeNet will find an available
// port for us automatically
unsigned short LocalPort = NULL;
LocalPort = GetPrivateProfileIntA("ClientSetup","UsePort",6790,".\\Client.set");
itoa(LocalPort,TempStr,10);
dbPrintC("Current selected Port number this game instance will use = "); dbPrint(TempStr);
dbPrintC("Enter Port number (1024 - 65535) for client to use = ");
strcpy(TempStr, dbInput());
dbPrint();
if (*TempStr != 0)
LocalPort = atoi(TempStr);
WritePrivateProfileStringA("ClientSetup","UsePort",dbStr(LocalPort),".\\Client.set");
//Get the IP number for the server
char ConnectIP[128];
GetPrivateProfileStringA("ClientSetup","ServerIP","127.0.0.1",ConnectIP,128,".\\Client.set");
dbPrintC("Current selected game server IP number = "); dbPrint(ConnectIP);
dbPrintC("Enter the IP the game server is using = ");
strcpy(TempStr, dbInput());
dbPrint();
if (*TempStr != 0)
strcpy(ConnectIP, TempStr);
WritePrivateProfileStringA("ClientSetup","ServerIP",ConnectIP,".\\Client.set");//update info
// Get Port Number for the server
unsigned short ConnectPort;
ConnectPort = GetPrivateProfileIntA("ClientSetup","ServerPort",6789,".\\Client.set");
itoa(ConnectPort,TempStr,10);
dbPrintC("Current selected game server Port number = "); dbPrint(TempStr);
dbPrintC("Enter Port number (1024 - 65535) the game server is using = ");
strcpy(TempStr, dbInput());
dbPrint();
if (*TempStr != 0)
ConnectPort = atoi(TempStr);
WritePrivateProfileStringA("ClientSetup","ServerPort",dbStr(ConnectPort),".\\Client.set");
// Try to connect
dbPrint(" *** TRYING TO CONNECT ***");
dbPrint( );
dbSync ( );dbSync ( );//must be done twice because of double buffering
mnSetBufferSizes(2048,2048,2048,2); // Client mode with some decent buffer sizes
mnSetLocal(LocalIP,LocalPort,LocalIP,LocalPort);// Setup local IP and Port
mnDisableMessageBoxes( ); // We dont want an error message box if an error occures
int iConnect = mnConnect(ConnectIP, ConnectPort, ConnectIP, ConnectPort, iTimeout, iNoThreads);
//int iConnect = mnConnect(ConnectIP, ConnectPort, iTimeout, LocalIP, LocalPort, iErrorMode, iUdpRecvSize, iTcpRecvSize, iNoThreads);
// If the connection was successful
if(iConnect == 1)
{
dbPrint("Connection was successful!");
dbPrint( );
dbSync ( );dbSync ( );//must be done twice because of double buffering
// Find maximum number of clients and operations
// necassary for udp receiving
iMaxPlayers = mnGetMaxClients();
iMaximumOperations = mnGetMaxOperations();
// Find our Network ID
iThisMachineNetworkID = mnGetClientID();
//Synchronise machine IDs with network IDs
iThisMachinePlayerID = iThisMachineNetworkID;
// Print our Network ID
dbPrintC("Our Network ID is: ");
dbPrint(dbStr(iThisMachineNetworkID));
dbSync ( );dbSync ( );//must be done twice because of double buffering
// Print our local IP, port and udp mode
dbPrintC("The local IP is: ");
dbPrint(mnGetLocalIPUDP());
dbSync ( );dbSync ( );//must be done twice because of double buffering
dbPrintC("The local port is: ");
dbPrint(dbStr(mnGetLocalPortUDP()));
dbSync ( );dbSync ( );//must be done twice because of double buffering
dbPrintC("UDP Mode: ");
dbPrint(dbStr(mnGetUDPMode()));
dbPrint( );
dbSync ( );dbSync ( );//must be done twice because of double buffering
dbPrint("Press any key to continue");
dbSync ( );dbSync ( );//must be done twice because of double buffering
dbWaitKey();
}
// If the connection timed out
if(iConnect == 0)
{
dbPrint("Connection timed out! Check that the server is switched on");
dbPrint("and that the ABOVE IP/Port setup is correct");
dbPrint("Press any key to exit");
dbSync ( );dbSync ( );//must be done twice because of double buffering
dbWaitKey();
return 1; //fail
}
// If an error occurred during connection
if(iConnect == -1)
{
dbPrint("An error occurred whilst trying to connect");
dbPrint("Press any key to exit");
dbSync ( );dbSync ( );//must be done twice because of double buffering
dbWaitKey();
return 1; //fail
}
}
else
{
//Get the server IP number
char LocalIP[128];
GetPrivateProfileString("HostSetup","UseIP","127.0.0.1",LocalIP,16,".\\Client.set");
dbPrintC("Current selected IP number = "); dbPrint(LocalIP);
dbPrintC("Enter the IP that you would like this host to use = ");
strcpy(TempStr, dbInput());
dbPrint();
if (*TempStr != 0)
strcpy(LocalIP, TempStr);
WritePrivateProfileString("HostSetup","UseIP",LocalIP,".\\Client.set");//update info
// Get Port Number for the server to use
unsigned short LocalPort = NULL;
LocalPort = GetPrivateProfileInt("HostSetup","UsePort",6789,".\\Client.set");
itoa(LocalPort,TempStr,10);
dbPrintC("Current selected Port number = "); dbPrint(TempStr);
dbPrintC("Enter Port number (1024 - 65535) this host to use = ");
strcpy(TempStr, dbInput());
dbPrint();
if (*TempStr != 0)
LocalPort = atoi(TempStr);
WritePrivateProfileString("HostSetup","UsePort",dbStr(LocalPort),".\\Client.set");
//Get maximum to allow on server
iMaxPlayers = GetPrivateProfileInt("HostSetup","MaxPlayers",32,".\\Client.set");
if ( iMaxPlayers > 255)
iMaxPlayers = 255;
itoa(iMaxPlayers,TempStr,10);
dbPrintC("Current Maximum players = "); dbPrint(TempStr);
dbPrintC("Enter MAXIMUM players in this game = ");
strcpy(TempStr, dbInput());
dbPrint();
if (*TempStr != 0)
iMaxPlayers = atoi(TempStr);
if ( iMaxPlayers > 255)
iMaxPlayers = 255;
WritePrivateProfileString("HostSetup","MaxPlayers",dbStr(iMaxPlayers),".\\Client.set");
// Attempt to start the HOST server
// The host is not counted in the MaxPlayers for the server code
mnSetLocal(LocalIP,LocalPort,LocalIP,LocalPort);
mnSetBufferSizes(2048,2048,2048,1); //server mode with some decent buffer sizes
mnDisableMessageBoxes( );
int iResult = mnStartServer((iMaxPlayers - 1 ),iMaximumOperations,iUdpMode,iNoThreads);
//int iResult = mnStartServer(LocalIP,LocalPort,(iMaxPlayers - 1),iMaximumOperations,iErrorMode,iUdpMode,iUdpRecvSize,iTcpRecvSize,iTcpStoreSize,iNoThreads);
// If started successfully
if(iResult == 0)
{
dbPrint(" *** HOST started ***");
dbPrint();
// Find our Network ID
iThisMachineNetworkID = 0;
// Synchronise machine IDs with network IDs
iThisMachinePlayerID = iThisMachineNetworkID;
// Print our Network ID
dbPrintC("Our Network ID is: ");
dbPrint(dbStr(iThisMachineNetworkID));
dbPrint("Press any key to continue");
dbSync ( );dbSync ( );//must be done twice because of double buffering
dbWaitKey();
return 0; // Sucess
}
// If failed to start
else
{
dbPrint(" *** HOST failed to start ***");
dbWaitKey();
dbSync ( );dbSync ( );//must be done twice because of double buffering
return 1; // Fail
}
}
return 0; // Sucess
}