4 Replies - 623 Views - Last Post: 03 December 2012 - 09:36 AM Rate Topic: -----

#1 Kernel303  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-November 12

TCP Connection over network with port fowarded and allowed in firewall

Posted 30 November 2012 - 04:29 PM

Good evening

I have a card game developed in c#. I wanted to make it work over network, however, I managed to make it work over LAN only. I browsed lots of pages, and didn't find an answer for my problem.

I am using System.Net.Socket object. The application has asynchronous connections and all. One side is client, other is server. In order to connect two computers (my computer is behind router), i should make some server (theoretically),that is publicly visible, and players will connect to it. Or set a static (visible, unique) IP adress for my PC. I do not want to do these, so I tried to forward ports.

I forwarded the port 906, and also allowed it in firewall for incoming requests. I set it all to both TCP and UDP. I hosted a connection with server.

public static int PORT = 906;
        public static Socket HostSocket;


 HostSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress IP = IPAddress.Any;// Functions.GetThisMachineIPv4();// IPAddress.Any;

            HostSocket.Bind(new IPEndPoint(IP, PORT));
            HostSocket.Listen(3);
            HostSocket.Blocking = false;
            HostSocket.BeginAccept(new AsyncCallback(BeginAcceptConnection), HostSocket); 



To make it easier to understand: IPAdress is IP.Any (0.0.0.0), and port is the one...906

The client:

 public static IPAddress IP = System.Net.IPAddress.Parse("XXX");
        public static int PORT = 906;

 client_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            client_socket.Blocking = false;
            client_socket.BeginConnect(IP, PORT, new AsyncCallback(OnConnect), client_socket);



Please note, that instead of the "XXX" string as IP, I put there my default gateway, witch i got from command prompt.
Aditional info, maybe useless, but I have Internet Protocol ver. 4 :D
However, despite days of studying pages and debugging, I have no idea, where else could be the problem, or what I did wrong.

Thank you for your time, and possible suggestions, or even solutions.

Is This A Good Question/Topic? 0
  • +

Replies To: TCP Connection over network with port fowarded and allowed in firewall

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 996
  • View blog
  • Posts: 2,404
  • Joined: 04-October 09

Re: TCP Connection over network with port fowarded and allowed in firewall

Posted 30 November 2012 - 05:58 PM

Using your gateway IP means you want to connect to the gateway. If you want to connect to another computer, use that computers IP address. If it isn't on your local network, you'll need to use the exposed IP address of that network.
Was This Post Helpful? 0
  • +
  • -

#3 Kernel303  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-November 12

Re: TCP Connection over network with port fowarded and allowed in firewall

Posted 02 December 2012 - 12:11 PM

Well, by the "exposed IP address of that network" you mean my CPU's external IP? that one you get for ex. here... http://www.whatismyip.com/ . I am quite confused, could you, please, guide me, where do i get the IP?
Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 996
  • View blog
  • Posts: 2,404
  • Joined: 04-October 09

Re: TCP Connection over network with port fowarded and allowed in firewall

Posted 02 December 2012 - 12:54 PM

The IP address from whatismyip.com is what you need. It's the address the rest of the word sees as your network.
Was This Post Helpful? 1
  • +
  • -

#5 Kernel303  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-November 12

Re: TCP Connection over network with port fowarded and allowed in firewall

Posted 03 December 2012 - 09:36 AM

Thank you very much indeed, my friend. The connection could be made now. However, it is giving me an error. On the client side, the connecting void and the initialization voids are here. I know, how hard, and anoying it is to read someone else's source code, so i will try to explain it briefly:

A CreateHost void is called. It creates a new instance of the HostSocket object, and begins accepting connections (if there are any)

 public static void CreateHost()
        {
            HostSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress IP = IPAddress.Any;// Functions.GetThisMachineIPv4();// IPAddress.Any;

            HostSocket.Bind(new IPEndPoint(IP, PORT));
            HostSocket.Listen(3);
            HostSocket.Blocking = false;
            HostSocket.BeginAccept(new AsyncCallback(BeginAcceptConnection), HostSocket); 
        }       
private static void BeginAcceptConnection(IAsyncResult ar)
        {
            if (HostSocket == null)
                return;
                       
            Socket HostTMP = (Socket)ar.AsyncState;
            HostSocket = HostTMP.EndAccept(ar);

            if (!HostSocket.Connected)
            {
                //Connection failed
                return;
            }
                       
            HostSocket.BeginReceive(myRecieveStruct.RecievedBytes, 0, myRecieveStruct.RecievedBytes.Length,
                SocketFlags.None, new AsyncCallback(OnRecieve), HostSocket);
            Core.Mainwindow.Invoke(new Core.Form1_delegate(Mainwindow.Successfully_Connected), new object[] { Core.MainWindow });
}


The server side is right here
The
CreateServer()
void basically just starts the client, wich tries to connect to the server. Please notice, that the
IP
is now the public IP, you told me to use (thank you once again). So the client is created, and tries to connect to the server. (
OnConnect()
). Then you can see there a try formula, what generally happens there, that if the connecting action fails, cliend shuts down, restarts, and tries to connect again. (this was to increase the reliability)

public static void CreateServer()
        {
            client_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            client_socket.Blocking = false;
            client_socket.BeginConnect(IP, PORT, new AsyncCallback(OnConnect), client_socket);
           // client_socket.BeginConnect(new IPEndPoint(IP, PORT), new AsyncCallback(OnConnect), client_socket);
        }
        private static void OnConnect(IAsyncResult ar)
        {
            try
            {
                Socket sock = (Socket)ar.AsyncState;
                sock.EndConnect(ar);

                if (!client_socket.Connected)
                {
                    //connection failed
                    ShutDown(true);                  
                    return;
                }
                //here, the connection is estabilished     
                Connected();                
                client_socket.BeginReceive(myRecieveStruct.RecievedBytes, 0, myRecieveStruct.RecievedBytes.Length, SocketFlags.None, new AsyncCallback(OnRecieve), client_socket);
            }
            catch (SocketException)
            {
                System.Threading.Thread.Sleep(5000);
                MessageBox.Show("That machine is not hosting at the moment. Conecting failed");
                ShutDown(true);                
            }
        }



Now, to the error.
1) I started the server first, and created a brake to the
BeginAcceptConnection(IAsyncResult ar)
.
2) I started the client and created a brake on
OnConnect(IAsyncResult ar)

3) Start the client
4) I caught the brake in server,
BeginAcceptConnection(IAsyncResult ar)

5) On Server, i debugged (F10) just to the
if (!HostSocket.Connected)
            {
                //Connection failed
                return;
            }


passage. I checked the connected value, and it is (was)
True


6) I debugged the client (F10) in the
OnConnect(IAsyncResult ar)
void
7) An exception is caught at line
 sock.EndConnect(ar);


It is SocketException, saying, that target PC have not responded in an interval, or the connection failed because the host hadn't responded (IP:PORT)

The
Aviable
property of the Client Socket is
0
.
I thought about it, and when the server accepts the connection (event is raised), i wait and do not bebug it further. After like 30 seconds, an event of connecting is raised at client side. Possibly, the error could be in time mismatch? Is the code above done the right, smooth way?

Thank you for your time, if you have red it to this point, and, of course, for possible responces.

Have a nice day!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1