Alright so I've been struggling with Multisync with DarkGDK for a while now and I just can't seem to get it working... I have no issues with making the connection through either LAN or the internet BUT I'm having a big issue with transferring data back and forth(Kind of important).. The server can receive the new players name but I'm having issues with more than one client, Any additional clients start overwriting the previous clients data. I don't know if it's just me being dumb, puttin things in the wrong place or what.. I hope someone can help me.
Server.h
#pragma once
// PACKET TYPES
//
#define PLAYER_JOIN 1
#define PLAYER_QUIT 2
#define PLAYER_DATA 3
#define CHAT_ENTRY 4
#define JOIN_SUCCESS 5
#define LEVEL_EVENT 6
#define NOOBIE 7
#define MAKE_OBJECT 8
#define SERVER_SHUTDOWN 9
#define MAX_CONNECTIONS 16
void HandleMessages ( );
void HandleNewPlayers ( );
void HandleQuitters ( );
void HostGame ( );
void SendToAllExcluding ( int Excluded );
void UpdateServer ( );
void QuitServer ( );
Server.cpp
#include "Classes.h"
#include "Client.h"
#include "DarkGDK.h"
#include "GUI.h"
#include "Multisync.h"
#include "Server.h"
// Server
//
char str_ServName[] = "Server's Name";
char str_ServTempStr[256] = "";
int num_NewConnection;
int num_Connections = 0;
int num_Quitter;
int num_PlayerLeft;
int MaxPlayers = 0;
// From Client
//
char str_MessageFrmClient[256];
extern char str_ClientName[256];
int num_MessageFrm;
int num_PacketType;
int num_PacketSize;
Player Plyr[MAX_CONNECTIONS];
void HandleMessages ( )
{
num_MessageFrm = NetMessageFrom ( ) + 1;
num_PacketType = NetGetByte ( );
switch ( num_PacketType )
{
case PLAYER_JOIN:
// Accept New Player
NetGetString ( str_ClientName, 16 );
if ( Plyr[num_MessageFrm].str_Name = "" ) Plyr[num_MessageFrm].str_Name = str_ClientName;
NetPutByte ( JOIN_SUCCESS );
NetPutString ( str_ServName );
NetSend ( num_MessageFrm );
// Tell everyone else about New Player
NetPutByte ( NOOBIE );
sprintf ( str_ServTempStr, "%s has joined.", Plyr[num_MessageFrm].str_Name );
NetPutString ( str_ServTempStr );
SendToAllExcluding ( num_MessageFrm );
break;
case PLAYER_DATA:
break;
case CHAT_ENTRY:
break;
default:
break;
}
}
void HandleNewPlayers ( )
{
dbText ( 0, 0, str_ServTempStr );
num_NewConnection = NetPlayerJoined ( );
if ( num_NewConnection > 0 )
{
num_Connections++;
}
}
void HandleQuitters ( )
{
num_Quitter = NetPlayerLeft ( );
if ( num_Quitter != 0 )
{
NetPutByte ( PLAYER_QUIT );
NetSendAll ( );
Plyr[num_Quitter].str_Name = "";
num_Connections--;
}
}
void HostGame ( )
{
if ( !NetHost ( MAX_CONNECTIONS ) )
{
NetGetError ( str_ServTempStr, 256 );
MessageBox ( NULL, str_ServTempStr, str_ServName, MB_ICONEXCLAMATION );
}
for ( int Nm = 2; Nm <= MAX_CONNECTIONS; Nm++ )
Plyr[Nm].str_Name = "";
}
void SendToAllExcluding ( int Excluded )
{
for ( int x = 1; x <= MAX_CONNECTIONS; x++ )
{
if ( x != Excluded )
NetSend ( x );
}
}
void UpdateServer ()
{
sprintf ( str_ServTempStr, "");
Plyr[1].str_Name = ExportPlayerName ( );
HandleNewPlayers ( );
HandleQuitters ( );
while ( NetGetMessage ( ) )
{
HandleMessages ( );
}
dbCenterText ( dbScreenWidth ( ) / 2, 0, "Hosting" );
sprintf ( str_ServTempStr, "Players: %i", num_Connections + 1 );
dbText ( 0, 50, str_ServTempStr );
MaxPlayers = MAX_CONNECTIONS;
for ( int Xn = 1; Xn <= 16; Xn++ )
{
sprintf ( str_ServTempStr, "%i:%s", Xn, Plyr[Xn].str_Name );
dbText ( 10, Xn * 12 + 52, str_ServTempStr );
}
}
Client.cpp
#include "Classes.h"
#include "Client.h"
#include "DarkGDK.h"
#include "GUI.h"
#include "Multisync.h"
#include "Server.h"
// Client
//
char str_ClientTempStr[256];
extern Player Plyr[MAX_CONNECTIONS];
// From Server
//
bool bl_ConnectionEstablished = false;
bool bl_IdObtained = false;
bool bl_JoinedYay = false;
char str_InputBuff[] = "";
int num_ID;
extern int num_PacketType;
char str_ServName[];
// Error
//
char str_ErrNoConnection[] = "Could not establish connection.";
char str_ErrNoConnectionMsg[] = "Connection Message";
char str_ClientName[] = "Client Name";
bool TryToConnect ( char *ip )
{
DWORD start;
sprintf ( str_ClientTempStr, "BUFFER CONTENT UNCHANGED!" );
if ( NetConnect ( ip ) )
{
start = GetTickCount ( );
while ( GetTickCount ( ) - start < 1000 );/*
if ( NetGetMessage ( ) )
{
NetGetString ( str_ClientTempStr, 256 );
sprintf ( str_ServName, str_ClientTempStr );
dbSetWindowTitle ( str_ServName );
}
else sprintf ( str_ClientTempStr, str_ErrNoConnectionMsg );*/
return true;
}
else
{
MessageBox ( NULL, str_ErrNoConnection, str_ClientName, MB_ICONEXCLAMATION );
return false;
}
}
void HandleMessageFromServer ( )
{
num_PacketType = NetGetByte ( );
switch ( num_PacketType )
{
case CHAT_ENTRY:
break;
}
}
void JoinGame ( char *ip )
{
bl_ConnectionEstablished = TryToConnect ( ip );
if ( bl_ConnectionEstablished )
{
if ( !bl_JoinedYay )
{
NetPutByte ( PLAYER_JOIN );
NetPutString ( ExportPlayerName ( ) );
NetSend ( 0 );
}
}
}
void UpdateClient ( )
{
if ( bl_JoinedYay ) dbCenterText ( dbScreenWidth ( ) / 2, 0, "Joined" );
if ( bl_ConnectionEstablished )
{
while ( NetGetMessage ( ) )
{
num_PacketType = NetGetByte ( );
switch ( num_PacketType )
{
case JOIN_SUCCESS:
NetGetString ( str_InputBuff, 256 );
dbSetWindowTitle ( str_InputBuff );
bl_JoinedYay = true;
break;
default:
break;
}
}
sprintf ( str_InputBuff, "%s", ExportPlayerName ( ) );
dbText ( 0, 0, str_InputBuff );
}
}
This code is everything to do with the Networking but I've also included the Project folder as well..