Hello,
I have a problem.
I will send 4 Packets.
2 are from the CubeWorld example, and 2, I have created yourself.
My problem is, that the float not the sendet float from the client include, but coordinates from my Player.
My codes are the follow:
Server:
// Cube World Server
// This demonstrates how to set up a simple server
// that uses UDP and TCP to relay data to clients
#include <MikeNet.h>
#include <DarkGDK.h>
// Operations, used to identify the purpose of each packet
// TCP
#define OP_NEWPLAYER 0
#define OP_LEFTPLAYER 1
// Main code
void DarkGDK ()
{
unsigned short LocalPort = 6789;
char * LocalIP = "";
int MaxClients = 50;
int UdpMode = UM_PER_CLIENT;
int NoThreads = 0;
int NoInstances = 1;
int Result;
// Setup packets
long long int RecvPacket = mnCreatePacket();
long long int SendPacket = mnCreatePacket();
mnSetMemorySize(SendPacket,1024);
long long int RPacket = mnCreatePacket();
long long int SPacket = mnCreatePacket();
mnSetMemorySize(SPacket,1);
// Start MikeNet
mnStart(NoInstances,NoThreads);
// Set the local IP and port of the server
// Note: if we did not use this command then MikeNet
// would find a local IP and port to use automatically
mnSetLocal(0,LocalIP,LocalPort,LocalIP,LocalPort);
// Attempt to start the server
Result = mnStartServer(0,MaxClients,0,UdpMode);
// If started successfully
if(Result == 0)
{
dbPrint("Server started");
dbPrint();
dbPrint("Server local TCP port: ");
dbPrint(dbStr(mnGetLocalPortTCP(0)));
dbPrint("Server local UDP port: ");
dbPrint(dbStr(mnGetLocalPortUDP(0)));
}
// If failed to start
else
{
dbPrint("Server failed to start");
dbWaitKey();
return;
}
// Main loop
while( LoopGDK() == true )
{
// Use less CPU
Sleep(1);
dbWait(1);
// Check to see if a new client has joined
int Joined = mnClientJoined(0);
// If a new client has joined
if(Joined > 0)
{
// Display the new client's information
dbPrintC("A new client has joined with a client ID of ");
dbPrint(dbStr(Joined));
dbPrintC(" Client TCP IP is: ");
dbPrint(mnGetClientIPTCP(0,Joined));
dbPrintC(" Client TCP port is: ");
dbPrint(dbStr(mnGetClientPortTCP(0,Joined)));
dbPrintC(" Client UDP IP is: ");
dbPrint(mnGetClientIPUDP(0,Joined));
dbPrintC(" Client UDP port is: ");
dbPrint(dbStr(mnGetClientPortUDP(0,Joined)));
// Tell the new client what clients are currently connected
for(int Client = 1;Client<=MaxClients;Client++)
{
if(mnClientConnected(0, Client) == 1)
{
if(Client != Joined)
{
mnAddInt(SendPacket,OP_NEWPLAYER);
mnAddInt(SendPacket,Client);
mnSendTCP(0,SendPacket,Joined,false,false);
}
}
}
// Tell clients that a new client has joined
mnAddInt(SendPacket,OP_NEWPLAYER);
mnAddInt(SendPacket,Joined);
mnSendTCPAll(0,SendPacket,false,false,Joined);
}
// Check to see if any clients have left recently
int Left = mnClientLeft(0);
// If a client has left recently
if(Left > 0)
{
// Print the client id of the client who left
dbPrintC("Client ");
dbPrintC(dbStr(Left));
dbPrint(" has disconnected");
// Tell clients that a client has left
mnAddInt(SendPacket,OP_LEFTPLAYER);
mnAddInt(SendPacket,Left);
mnSendTCPAll(0,SendPacket,false,false,Left);
}
// Deal with new packets from all clients
for(int Client = 1; Client<=MaxClients; Client++)
{
// Check to see if any new TCP packets have been received
int TcpPackets = mnRecvTCP(0,RecvPacket,Client);
// If any have been received then do nothing
// since we don't want tcp packets from clients
if(TcpPackets > 0)
{
}
int UPackets = mnRecvUDP(0,RPacket,1,0);
if(UPackets > 0)
{
// Get data from packet
float test1 = mnGetFloat(RPacket);
dbPrint(dbStr(test1));
}
// Check to see if any new UDP packets have been received
int UdpPackets = mnRecvUDP(0,RecvPacket,Client,0);
// If any have been received
if(UdpPackets > 0)
{
// Get data from packet
float PosX = mnGetFloat(RecvPacket);
float PosY = mnGetFloat(RecvPacket);
float PosZ = mnGetFloat(RecvPacket);
float RotX = mnGetFloat(RecvPacket);
float RotY = mnGetFloat(RecvPacket);
float RotZ = mnGetFloat(RecvPacket);
// Relay data to clients
// Formulate packet
mnAddInt(SendPacket,Client);
mnAddFloat(SendPacket,PosX);
mnAddFloat(SendPacket,PosY);
mnAddFloat(SendPacket,PosZ);
mnAddFloat(SendPacket,RotX);
mnAddFloat(SendPacket,RotY);
mnAddFloat(SendPacket,RotZ);
// Send packet to all clients
mnSendUDPAll(0,SendPacket,false,false,Client);
}
}
}
// Shutdown MikeNet and exit
mnFinish(-1);
return;
}
Client:
// Cube World Client
// This demonstrates how to set up a simple client
// that uses UDP and TCP to communicate with the server
#include <MikeNet.h>
#include <DarkGDK.h>
// Operations, used to identify the purpose of each packet
// TCP
#define OP_NEWPLAYER 0
#define OP_LEFTPLAYER 1
float tt = 10;
// Variables
// Set velocity and turn speed for player movement
const float Velocity = 5, TurnSpeed = 5;
// To be filled during connection
int MaximumClients;
void DarkGDK(void)
{
// Set UDP and TCP port of the server, which is 6789
unsigned short ConnectPort = 6789;
// 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 Timeout = 4;
// 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 NoThreads = 0;
// Set the number of MikeNet instances to create.
int NoInstances = 1;
// Setup packets
long long int RecvPacket = mnCreatePacket();
long long int SendPacket = mnCreatePacket();
mnSetMemorySize(SendPacket,1024);
long long int RPacket = mnCreatePacket();
long long int SPacket = mnCreatePacket();
mnSetMemorySize(SPacket,1);
// Start MikeNet
mnStart(NoInstances,NoThreads);
// Get IP address to connect to via UDP and TCP
char * ConnectIP = "192.168.178.64";
// Try to connect
int Connect = mnConnect(0,ConnectIP,ConnectPort,ConnectIP,ConnectPort,Timeout,true);
// Delete memory allocated by dbInput
delete ConnectIP;
dbCLS();
// If the connection was successful
switch(Connect)
{
case(1):
dbPrint("Connection was successful!");
// Find maximum number of clients
// necassary for UDP receiving
MaximumClients = mnGetMaxClients(0);
// Display information
dbPrintC(" The local TCP IP is: ");
dbPrint(mnGetLocalIPTCP(0));
dbPrintC(" The local TCP port is: ");
dbPrint(dbStr(mnGetLocalPortTCP(0)));
dbPrintC(" The local UDP IP is: ");
dbPrint(mnGetLocalIPUDP(0));
dbPrintC(" The local UDP port is: ");
dbPrint(dbStr(mnGetLocalPortUDP(0)));
dbPrintC(" Your client ID is: ");
dbPrint(dbStr(mnGetClientID(0)));
dbPrint("");
dbPrint("Press any key to continue");
dbWaitKey();
break;
// If the connection timed out
case(0):
dbPrint("Connection timed out. The server may not be available.");
dbWaitKey();
return;
break;
// If an unknown error occurred during connection
case(-1):
dbPrint("An unknown error occurred whilst trying to connect.");
dbWaitKey();
return;
break;
// If the connection request was rejected
case(-2):
dbPrint("The connection request was rejected because the server is full.");
dbWaitKey();
return;
break;
}
// Setup the world for us to run around in
dbSyncOn();
dbSyncRate(40);
dbAutoCamOff();
dbMakeMatrix(1,5000,5000,50,50);
dbPositionMatrix(1,dbCameraPositionX(),dbCameraPositionY()-20,dbCameraPositionZ());
// Once connected, we loop until we become disconnected
while( (mnClientConnected(0,0) == 1) && (LoopGDK() == true) )
{
// Refresh screen
dbSync();
dbText(15,25,dbStr(tt));
if(dbKeyState(4))
{
dbText(1,10,"sending packet");
mnAddFloat(SPacket,tt);
mnSendUDP(0,SPacket,NULL,false,true);
}
// Player movement
if(dbUpKey() == 1){dbMoveCamera(Velocity);}
if(dbDownKey() == 1){dbMoveCamera(-Velocity);}
if(dbLeftKey() == 1){dbTurnCameraLeft(TurnSpeed);}
if(dbRightKey() == 1){dbTurnCameraRight(TurnSpeed);}
// Deal with new messages from the server
// Check for new TCP messages
int iTCPPackets = mnRecvTCP(0,RecvPacket,NULL);
// If there is a new TCP message
if(iTCPPackets > 0)
{
// Get operation of new message
// and player that it applies to
int Operation = mnGetInt(RecvPacket);
int Player = mnGetInt(RecvPacket);
// If the server is telling us that a new player has joined
// then create a cube for that player
if(Operation == OP_NEWPLAYER)
{
if(dbObjectExist(Player) == 0)
{
dbMakeObjectCube(Player,50);
}
}
// If the server is telling us that a player has left then
// delete the cube of that player
if(Operation == OP_LEFTPLAYER)
{
if(dbObjectExist(Player) == 1)
{
dbDeleteObject(Player);
}
}
}
// Check for new UDP messages on a per client basis
// Check each client
for(int Player = 1;Player<=MaximumClients;Player++)
{
// Check for new UDP messages
int UDPPackets = mnRecvUDP(0,RecvPacket,Player,0);
if(dbObjectExist(Player) == 1)
{
// If there is a new UDP message
if(UDPPackets == 1)
{
// Get the player's position and angle
// and apply them to his/her cube
float PosX = mnGetFloat(RecvPacket);
float PosY = mnGetFloat(RecvPacket);
float PosZ = mnGetFloat(RecvPacket);
float RotX = mnGetFloat(RecvPacket);
float RotY = mnGetFloat(RecvPacket);
float RotZ = mnGetFloat(RecvPacket);
dbPositionObject(Player,PosX,PosY,PosZ);
dbRotateObject(Player,RotX,RotY,RotZ);
}
}
}
// Send our position/angle to the server via UDP
// Formulate packet
mnAddFloat(SendPacket,dbCameraPositionX());
mnAddFloat(SendPacket,dbCameraPositionY());
mnAddFloat(SendPacket,dbCameraPositionZ());
mnAddFloat(SendPacket,dbCameraAngleX());
mnAddFloat(SendPacket,dbCameraAngleY());
mnAddFloat(SendPacket,dbCameraAngleZ());
// Send packet
mnSendUDP(0,SendPacket,NULL,false,true);
}
// If we have become disconnected from the server
while(dbScanCode() != 0){dbWait(1);}
while( (dbScanCode() == 0) && (LoopGDK() == true) )
{
dbText(0,0,"Lost connection to server, press any key to exit");
dbSync();
dbWait(1);
}
// Shutdown MikeNet and exit
mnFinish(-1);
return;
}