Author Topic: Help, can't connect from remote PC or through external IP  (Read 9031 times)

Sehyo

  • Not-a-newbie
  • *
  • Posts: 2
  • Karma: 0
    • View Profile
Help, can't connect from remote PC or through external IP
« on: January 21, 2013, 11:38:50 AM »
Hi, I'm trying to connect to my RakNet server on port 60000 but I seem to be unable to connect.
The port is portforwarded and I have also tried to run it on other ports that I have ran other servers successfully on before.
I can connect to the server from the same PC to IP 127.0.0.1 but not through the external :(

Here is my server code:
Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RakNet;
namespace WTServer
{
        public class Client : RakNetGUID
        {
            public int id, x, y;
            public RakNetGUID guid;
            public Client(int passed_id)
            {
                id = passed_id;
            }
        }
    class Program
    {
        static uint MAX_CLIENTS = 10;
        static ushort port = 60000;
        enum GameMessages
        {
            SPAWN_POSITION = DefaultMessageIDTypes.ID_USER_PACKET_ENUM+100,
            POSITION_UPDATE = DefaultMessageIDTypes.ID_USER_PACKET_ENUM+101,
            YOUR_TURN = DefaultMessageIDTypes.ID_USER_PACKET_ENUM+102,
            WELCOME = DefaultMessageIDTypes.ID_USER_PACKET_ENUM+103,
            NEW_CLIENT = DefaultMessageIDTypes.ID_USER_PACKET_ENUM+104
        }
        static int Main(string[] args)
        {
            RakPeerInterface peer = RakPeerInterface.GetInstance();
            Packet packet = new Packet();
            peer.Startup(MAX_CLIENTS,  new SocketDescriptor(port, "0"), 1);
            Console.WriteLine("Starting the server.");
            peer.SetMaximumIncomingConnections((ushort)MAX_CLIENTS);
            BitStream bsOut = new BitStream();
            RakString rs = new RakString(); // In C#, we need to initialize it to use bsIn.Read on it :S
            // List, replaces C++ std::vector template, I miss vector :'( T_T
            List<Client> clients = new List<Client>();
                                                 // ID, X, Y

            int current_client = 0;
            int counter = 0;
            // Start Message loop
            while(true)
            {         
                for(packet = peer.Receive(); packet != null ; peer.DeallocatePacket(packet), packet = peer.Receive())
                {
                    BitStream bsIn = new BitStream(packet.data, packet.length, false);
                    bsIn.IgnoreBytes(sizeof(DefaultMessageIDTypes));
                    bsIn.Read(rs);
                    // Handle messages
                    int x, y, client_id;
                    switch(packet.data[0])
                    {
                            // I hope casting it to a byte doesn't corrupt it...
                        case (byte)DefaultMessageIDTypes.ID_REMOTE_DISCONNECTION_NOTIFICATION:
                            Console.WriteLine("A client has disconnected.");
                            break;

                        case (byte)DefaultMessageIDTypes.ID_REMOTE_CONNECTION_LOST:
                            Console.WriteLine("A client lost their connection.");
                            break;

                        case (byte)DefaultMessageIDTypes.ID_REMOTE_NEW_INCOMING_CONNECTION:
                            Console.WriteLine("A client has connected.");
                            break;

                        case (byte)DefaultMessageIDTypes.ID_NEW_INCOMING_CONNECTION:
                            Console.WriteLine("A connection is incoming.");
                            // Add client to the List
                            client_id = (int)clients.Count();
                            if((int)clients.Count() > 0)
                            {
                                // Send new client notification to existing players
                                Console.WriteLine("Sending new spawn position to each client");
                                bsOut.Reset();
                                bsOut.Write((byte)GameMessages.NEW_CLIENT);
                                bsOut.Write(client_id);
                                bsOut.Write(64); // X spawn location
                                bsOut.Write(64); // Y spawn location
                                for(int i = 0; i < (int)clients.Count(); ++i)
                                {
                                    Console.WriteLine("To: " + i + " - " + clients[i].guid.g);
                                    peer.Send(bsOut, PacketPriority.HIGH_PRIORITY, PacketReliability.RELIABLE_ORDERED, (char)0, peer.GetSystemAddressFromGuid(clients[i].guid), false);
                                }
                                bsOut.Reset();
                               
                                Console.WriteLine("Sending each client's pos to new client");
                                for(int i = 0; i < (int)clients.Count(); ++i)
                                {
                                    Console.WriteLine("Sending to: " + i);
                                    bsOut.Reset();
                                    bsOut.Write((byte)GameMessages.NEW_CLIENT);
                                    bsOut.Write(i); // ID
                                    bsOut.Write(clients[i].x); // X pos
                                    bsOut.Write(clients[i].y); // Y pos
                                    peer.Send(bsOut,PacketPriority.HIGH_PRIORITY, PacketReliability.RELIABLE_ORDERED, (char)0, packet.systemAddress, false);
                                }
                            }
                            else
                                Console.WriteLine("No clients yet :( no spawn locations sent nor existing locations");
                            // Add client
                           clients.Add(new Client(client_id));
                           clients[client_id].guid = packet.guid;
                           clients[client_id].x = 64; // Set the new clients spawn location
                           clients[client_id].y = 64; // Set the new clients spawn location
                           // BitStream to write a custom user message
                           bsOut.Reset();
                           bsOut.Write((byte)GameMessages.SPAWN_POSITION);
                           bsOut.Write(client_id);
                           bsOut.Write(64); // Tell other clients about the spawn location
                           bsOut.Write(64); // Tell other clients about the spawn location
                           peer.Send(bsOut,PacketPriority.HIGH_PRIORITY, PacketReliability.RELIABLE_ORDERED, (char)0, packet.systemAddress, false);
                           bsOut.Reset();
                           break;

                        case (byte)DefaultMessageIDTypes.ID_DISCONNECTION_NOTIFICATION:
                            Console.WriteLine("A client lost their connection :(.");
                            break;

                        case (byte)GameMessages.POSITION_UPDATE:
                            // Received new position from client.
                            bsIn.Read(out client_id); // Store this data
                            bsIn.Read(out x); // Store this data
                            bsIn.Read(out y); // Store this data
                            Console.WriteLine("Client {0} sent new position: {1}, {2}", client_id, x, y);
                            // Save to list
                            clients[client_id].x = x;
                            clients[client_id].y = y;
                            Console.WriteLine("Sending new position value to each client");
                            bsOut.Reset();
                            bsOut.Write((byte)GameMessages.POSITION_UPDATE);
                            bsOut.Write(client_id);
                            bsOut.Write(x);
                            bsOut.Write(y);
                            for(int i = 0; i < (int)clients.Count(); ++i) // Send data to all clients (bsOut.Write)
                            {
                            if(client_id != i)
                            {
                                Console.WriteLine(" To: " + i+1 + " - " + clients[i].g);
                                // Use GetSystemAddresFromGUID to convert the stored GUIDS into addresses
                                peer.Send(bsOut, PacketPriority.HIGH_PRIORITY, PacketReliability.RELIABLE_ORDERED,(char)0,peer.GetSystemAddressFromGuid(clients[i]), false);
                            }
                            else
                                Console.WriteLine("Not sending to own client: " + client_id);
                            }
                            bsOut.Reset();
                            // Sleep for 3 sec :P
                           /* System.Threading.Thread.Sleep(3000);
                            // Send a YOUR_TURN to the next client
                            current_client = (++current_client) % (int)clients.Count();
                            bsOut.Reset();
                            bsOut.Write((byte)GameMessages.YOUR_TURN);
                            peer.Send(bsOut, PacketPriority.HIGH_PRIORITY, PacketReliability.RELIABLE_ORDERED, (char)0, peer.GetSystemAddressFromGuid(clients[current_client]), false); */
                            break;

                        default:
                            Console.WriteLine("Message with identifier: " + packet.data[0] + " has arrived.");
                            break;
                    }
                 //   bsOut.Reset(); // Gives error atm, will be gone when its been assigned inside switch
                }
            }
            RakPeerInterface.DestroyInstance(peer);
            return 0;
        }
    }
}

And also if wanted here is the client code
Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RakNet;
namespace WTClientCode {
    class Program {
        static ushort port = 0;
        enum GameMessages
        {
            SPAWN_POSITION = DefaultMessageIDTypes.ID_USER_PACKET_ENUM+100,
            POSITION_UPDATE = DefaultMessageIDTypes.ID_USER_PACKET_ENUM+101,
            YOUR_TURN = DefaultMessageIDTypes.ID_USER_PACKET_ENUM+102,
            WELCOME = DefaultMessageIDTypes.ID_USER_PACKET_ENUM+103,
            NEW_CLIENT = DefaultMessageIDTypes.ID_USER_PACKET_ENUM+104
        }
        static void Main(string[] args)
        {
            string strPort;
            RakPeerInterface peer = RakPeerInterface.GetInstance();
            Packet packet = new Packet();
            Console.WriteLine("Enter Client port (NOT SERVER PORT) or hit enter for automatic port");
            strPort = Console.ReadLine();
            if(strPort == "")
                strPort = "0";
            port = ushort.Parse(strPort);
            peer.Startup(1, new SocketDescriptor(port, "127.0.0.1"), 1);
            string str = "127.0.0.1";
            bool connected = false;
            RakString rs;
            int int_message, client_id = 0; // Or VS gives us an error that its not initialized in the if statement after message loop :S
            int posX = 0, posY = 0; // We need to initialize it or visual gives error later :S
            SystemAddress server_address;
            BitStream bsOut = new BitStream();
            Console.WriteLine("Enter Server IP or hit enter for 127.0.0.1");
            str = Console.ReadLine();
            if(str == "")
                str = "127.0.0.1";
            Console.WriteLine("Starting the client.");
            peer.Connect(str, 60000,"0",0);
           
            while(true)
            {
                for(packet = peer.Receive(); packet != null; peer.DeallocatePacket(packet), packet = peer.Receive())
                {
                    BitStream bsIn = new BitStream(packet.data, packet.length, false);
                  //  bsIn.IgnoreBytes(sizeof(DefaultMessageIDTypes));
                    bsIn.IgnoreBytes(1); // fix
                    bsIn.Read(out int_message);
                    switch(packet.data[0])
                    {
                        case (byte)GameMessages.SPAWN_POSITION:
                        bsIn.Read(out client_id);
                        bsIn.Read(out posX); // Receive spawn location
                        bsIn.Read(out posY); // Receive spawn
                        // Repos of camera might be needed here aswell.
                        server_address = packet.systemAddress;
                        Console.WriteLine("We are Client Number: {0} at location: {1},{2}", int_message,posX,posY);
                        break;
                        case (byte)GameMessages.WELCOME:
                        Console.WriteLine("Server said we are client number: " + int_message);
                        break;
                        case (byte)DefaultMessageIDTypes.ID_CONNECTION_REQUEST_ACCEPTED:
                        connected = true;
                        break;
                        case (byte)GameMessages.POSITION_UPDATE: // Receive positions
                        // Report the servers new counter value
                        Console.WriteLine("Server said we are now at " + int_message);
                        break;
                        case (byte)GameMessages.YOUR_TURN:
                        Console.WriteLine("My turn, sending message.");
                        bsOut.Write((byte)GameMessages.POSITION_UPDATE);  // Send position
                        bsOut.Write(20); // Our X position
                        bsOut.Write(20); // Our Y position
                        peer.Send(bsOut, PacketPriority.HIGH_PRIORITY, PacketReliability.RELIABLE_ORDERED, (char)0, packet.systemAddress, false);
                        break;
                        default:
                            Console.WriteLine("Message with identifier " + packet.data[0] + " has arrived.");
                            break;
                    }
                    bsOut.Reset();
                }
             //
               /* if(true)
                {
                    Console.WriteLine("We (Client {0}) sent our position: {1},{2}", client_id,posX,posY);
                    bsOut.Reset();
                    bsOut.Write((byte)GameMessages.POSITION_UPDATE);
                    bsOut.Write(client_id);
                    bsOut.Write(posX);
                    bsOut.Write(posY);
                    peer.Send(bsOut, PacketPriority.HIGH_PRIORITY, PacketReliability.RELIABLE_ORDERED, (char)0, packet.systemAddress, false);   
                } */
            }
            RakPeerInterface.DestroyInstance(peer);
        }
    }
}

Help please :P
Thanks :)

Rak'kar

  • Administrator
  • Hero Member
  • *****
  • Posts: 6895
  • Karma: 291
    • View Profile
    • RakNet
Re: Help, can't connect from remote PC or through external IP
« Reply #1 on: January 21, 2013, 11:42:20 AM »
For socketDescriptor on the client, do not bind to 127.0.0.1. Leave it blank (the default).

Sehyo

  • Not-a-newbie
  • *
  • Posts: 2
  • Karma: 0
    • View Profile
Re: Help, can't connect from remote PC or through external IP
« Reply #2 on: January 21, 2013, 11:53:37 AM »
Thanks a lot Rakkar :) <3
« Last Edit: January 21, 2013, 03:41:19 PM by Sehyo »