hey sorry i took so long. here is the code. its reaaaally simple..
//multisync example by cetobasilius
#include <DarkGDK.h>
#include "multisync.h"
#define MAX_CONNECTED 15
char str[MAX_PATH];
int mode=0; //0= client mode, 1=server mode
char ErrorNoConnection[MAX_PATH] = "Could not establish connection to Server.";
char ErrorNoConnectMessage[MAX_PATH] = "Connected, no message";
char serverip[MAX_PATH] = "127.0.0.1";
bool connectedclient = false;
int number=0;
char MultiSyncServerName[MAX_PATH] = "Basi Test Server";
bool connectedserver = false;
int connection = 0;
int connections = 0;
int playerleft = 0;
bool TryMultiSyncServerConnect(char* ipaddress);
int response[16];
int whoresponse;
int tempresponse;
int numberassigned;
char welcome[MAX_PATH];
bool TryMultiSyncServerConnect(char *ipaddress)
{
DWORD start;
sprintf(str, "BUFFER CONTENT UNCHANGED.");
if (NetConnect(ipaddress))//trata de conectarte a la direccion IP del servidor//tries to connect to server with the ip adress declared up there
{
start = GetTickCount();// this is some sort of a pause, to make sure the message the server sends (welcome message) gets to the client
while (GetTickCount() - start < 1000);
if (NetGetMessage()) NetGetString(str, MAX_PATH);//recibe cualquier mensaje de bienvenida //recieve the message
else sprintf(str, ErrorNoConnectMessage);// si no hay mensaje de bienvenida print esto//if theres no message print this other message
if(strcmp(str,"Basi Test Server")==0){sprintf(welcome, "succesfully connected to Basi Test Server");}//check server name
else{sprintf(welcome, "succesfully connected to incorrect server");}//es obio lo que dice. lol
return true;//regresa 1, osease que si se conecto//returns 1 which means succesfully connected
}
else//no se pudo conectar
{
sprintf(welcome, "could not detect server");
return false;
}
}
//////////////////////////////////////////////////////////////////////
/////////////////////////////// MAIN /////////////////////////////////
void DarkGDK (void)
{
dbSyncOn();
dbSyncRate(60);
dbPositionCamera(10, 10, -20);
dbWait(400);
if (strcmp(dbInKey(), "l") ==0){mode=1;}//mode selector// if you quickly press l at start the program will start in mode 1, which is server mode.
//else it will go to mode 0 which is client. you can connect up to i dont know how much players but in this sample i made it for 16 i think?
///////////////////////////////////////////////////////////////////////
if(mode==0)//client mode
{
NetSetPort(23);
connectedserver = TryMultiSyncServerConnect(serverip);
}
///////////////////////////////////////////////////////////////////////
if(mode==1)//server mode
{
NetSetPort(23);
if (!NetHost(MAX_CONNECTED))
{
NetGetError(str, MAX_PATH);
MessageBox(NULL, str, MultiSyncServerName, MB_ICONEXCLAMATION);
}
}
///////////////////////////////////////////////////////////////////////
while (LoopGDK())
{
if(mode==0)//client mode
{
dbClear(0, 0, 0);
dbText(0,0,"Client Mode: ");
dbText(100,0,welcome);
if (strcmp(dbInKey(), "k") ==0){connectedserver = TryMultiSyncServerConnect(serverip);}//reset/search for server
if(NetConnected()==1)
{
dbText(0,20,"connected");
if(NetGetMessage()){number=NetGetInt();}
sprintf(str, "%i is the number being sent by the server", number);dbText(0, 40, str);
}
else{dbText(0,20,"not connected");}
if (strcmp(dbInKey(), "l")==0)
{
number=number+1;
if(number>=500){number=0;}
NetPutInt(number);
NetSend(0);
}
}
if(mode==1)//server mode
{
dbClear(0, 0, 0);
dbText(0, 0,"Server Name: ");
dbText(110, 0,MultiSyncServerName);
connection = NetPlayerJoined();
if (connection != 0)
{
NetPutString(MultiSyncServerName);// este mensaje se manda cuando un cliente nuevo se conecta al servidor,este se puede usar para checar la compatibilidad entre el servidor y el cliente
NetSend(connection);
connections++;
}
playerleft = NetPlayerLeft();
if (playerleft)
{
connections--;
}
sprintf(str, "%i connected.", connections);dbText(0, 20, str);
if (strcmp(dbInKey(), "l")==0)
{
number=number+1;
if(number>=500){number=0;}
NetPutInt(number);
NetSendAll();
}
for(int i=0;i<=15;i++)
{
if(NetGetMessage())
{
whoresponse=NetMessageFrom();//who no whore XD
tempresponse=NetGetInt();
response[whoresponse]=tempresponse;
}
}
sprintf(str, "%i is the number sent", number);dbText(0, 40, str);
sprintf(str, " Server itself: %i", /*response[0]*/number);dbText(0, 80, str);
sprintf(str, " P2 Sends: %i", response[1]);dbText(0, 100, str);
sprintf(str, " P3 Sends: %i", response[2]);dbText(0, 120, str);
sprintf(str, " P4 Sends: %i", response[3]);dbText(0, 140, str);
sprintf(str, " P5 Sends: %i", response[4]);dbText(0, 160, str);
sprintf(str, " P6 Sends: %i", response[5]);dbText(0, 180, str);
sprintf(str, " P7 Sends: %i", response[6]);dbText(0, 200, str);
sprintf(str, " P8 Sends: %i", response[7]);dbText(0, 220, str);
sprintf(str, " P9 Sends: %i", response[8]);dbText(0, 240, str);
sprintf(str, " P10 Sends: %i", response[9]);dbText(0, 260, str);
sprintf(str, " P11 Sends: %i", response[10]);dbText(0, 280, str);
sprintf(str, " P12 Sends: %i", response[11]);dbText(0, 300, str);
sprintf(str, " P13 Sends: %i", response[12]);dbText(0, 320, str);
sprintf(str, " P14 Sends: %i", response[13]);dbText(0, 340, str);
sprintf(str, " P15 Sends: %i", response[14]);dbText(0, 360, str);
sprintf(str, " P16 Sends: %i", response[15]);dbText(0, 380, str);
}
dbSync();
}
if(mode==0)
{
if (NetConnected()) NetDisconnect();
}
return;
}
What this does is just a little example of how sending packets functions. but you will have to change it a little bit to get it to work with your game as you want it to function. for example, to send different data on one message you must send it as a string, like"p1,x:12,y:5" where you can separate that string in your other program and take that as"player one is on position(12,5)" or something like that... use your imagination there... :p
if you press "l" before the program starts it will launch in server mode, if you dont press anything it will launch in client mode, you can launch all the clients you wish, but the server only accepts 15. in server mode press "L" so you send a number to all clients, and in client mode press "L" so you send a number to the server. the code is not enhanced and is just for demostration purposes... hope it does help!
and i think 999 is the limit with multisync... dont really remember
hi