Jump to content
  • Advertisement
Sign in to follow this  
Ruddie

Raknet Problems

This topic is 2860 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I have been trying out raknet but I stumbled onto a problem and I can't seem to get why it isn't working correctly.

Here is the complete code:


#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <iostream>
#include "RakPeerInterface.h"
#include "MessageIdentifiers.h"
#include "BitStream.h"
#include "RakNetTypes.h"

using namespace std;

#define MAX_CLIENTS 10
#define SERVER_PORT 61337

enum GameMessages
{
ID_GAME_MESSAGE_1=ID_USER_PACKET_ENUM+1
};

int main(void)
{
char username[500];
char str[512];
char message[200];
RakNet::RakPeerInterface *peer = RakNet::RakPeerInterface::GetInstance();
bool isServer;
RakNet::Packet *packet;

printf("(C) or (S)erver?\n");
gets(str);
if ((str[0]=='c')||(str[0]=='C'))
{
peer->Startup(1,&RakNet::SocketDescriptor(), 1);
isServer = false;
}
else
{
peer->Startup(MAX_CLIENTS, &RakNet::SocketDescriptor(SERVER_PORT,0), 1);
isServer = true;
}

if ( isServer )
{
printf("Server is starting up...\n");
// Accept incoming connections (up to a maximum amount)
peer->SetMaximumIncomingConnections(MAX_CLIENTS);
}
else
{
printf("Enter server IP or press enter for default (ip = 127.0.0.1)\n");
gets(str);
if ( str[0] == 0 ){
strcpy(str, "127.0.0.1");
}
printf("Starting up client..\n");
peer->Connect(str, SERVER_PORT, 0,0);

printf("Please enter a username (20 characters max.).\n");
cin.getline(username, 20);
}

while ( true )
{
for ( packet=peer->Receive(); packet; peer->DeallocatePacket(packet), packet=peer->Receive())
{

switch ( packet->data[0] )
{
case ID_REMOTE_DISCONNECTION_NOTIFICATION:
printf("Another client has disconnected.\n");
break;
case ID_REMOTE_CONNECTION_LOST:
printf("Another client has lost connection.\n");
break;
case ID_REMOTE_NEW_INCOMING_CONNECTION:
printf("A new client has established connection.\n");
break;
case ID_CONNECTION_REQUEST_ACCEPTED:
{
printf("Your connect request has been accepted by the server.\n");

// Use bitstream to write a custom user message
RakNet::BitStream bsOut;
bsOut.Write((RakNet::MessageID)ID_GAME_MESSAGE_1);
bsOut.Write(username);
bsOut.Write(" has joined the server\n");
// Send it all to the server
peer->Send(&bsOut, HIGH_PRIORITY, RELIABLE_ORDERED,0,packet->systemAddress,false);
}
break;
case ID_NEW_INCOMING_CONNECTION:
printf("A connection is now incoming.\n");
break;
case ID_NO_FREE_INCOMING_CONNECTIONS:
printf("Server is currently at it's maximum limit.\n");
break;
case ID_DISCONNECTION_NOTIFICATION:
if (isServer)
{
printf("A client has disconnected.\n");
}
else
{
printf("We have been disconnected.\n");
}
break;
case ID_CONNECTION_LOST:
if (isServer)
{
printf("A client has lost connection.\n");
}
else
{
printf("Connection lost.\n");
}
break;
case ID_GAME_MESSAGE_1:
{
// Display any messages send to the client/server
RakNet::RakString rsUN;
RakNet::RakString rsMsg;
RakNet::BitStream bsIn(packet->data,packet->length,false);

bsIn.IgnoreBytes(sizeof(RakNet::MessageID));
bsIn.Read(rsUN);
bsIn.Read(rsMsg);
printf(rsUN.C_String(), "%s");
printf(rsMsg.C_String(), "%s\n");
if ( isServer )
{
// And send these further if the current execute file is a server
RakNet::BitStream bsOut;
bsOut.Write((RakNet::MessageID)ID_GAME_MESSAGE_1);
bsOut.Write(rsUN);
bsOut.Write(rsMsg);
peer->Send(&bsOut, HIGH_PRIORITY, RELIABLE_ORDERED,0,packet->systemAddress,true);
}
}
default:
if ( isServer )
printf("Packet arrived with indentifier: %i\n ", packet->data[0]);
break;
}
}
if ( !isServer && kbhit() ) // If a client is currently executed, and the keyboard is used.
{
// Get a message to send
gets(message);
// If the message isn't empty
if ( message[0] != NULL )
{
int testtest = 5;
// Send the message
printf("Sending this message: ");
printf(message);
RakNet::BitStream bsSend;
bsSend.Write((RakNet::MessageID)ID_GAME_MESSAGE_1);
bsSend.Write(username);
bsSend.Write(message);
peer->Send(&bsSend, HIGH_PRIORITY, RELIABLE_ORDERED,0,packet->systemAddress,false);
printf("I sended the message!\n");
}
}
}

RakNet::RakPeerInterface::DestroyInstance(peer);

return 0;
}



And in this part of the code it seems I am crashing on my client:


if ( !isServer && kbhit() ) // If a client is currently executed, and the keyboard is used.
{
// Get a message to send
gets(message);
// If the message isn't empty
if ( message[0] != NULL )
{
int testtest = 5;
// Send the message
printf("Sending this message: ");
printf(message);
RakNet::BitStream bsSend;
bsSend.Write((RakNet::MessageID)ID_GAME_MESSAGE_1);
bsSend.Write(username);
bsSend.Write(message);
peer->Send(&bsSend, HIGH_PRIORITY, RELIABLE_ORDERED,0,packet->systemAddress,false);
printf("I sended the message!\n");
}
}



I narrowed it down, and found that the line causing the crash is the peer->Send function. However, I can't seem to understand why it is crashing.

Is their someone who has any ideas?

Share this post


Link to post
Share on other sites
Advertisement
Seems I was being rather stupid here, since the packet got destroyed in the for loop, and of course, I tried to get the packet to tell it's systemAddress, which should have pointed to NULL at the time, causing my crash.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!