C# Socket Communication Behind a router

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cnixuser
    New Member
    • Dec 2006
    • 80

    C# Socket Communication Behind a router

    Hi,

    I was wondering if someone could give me some general pointers about creating client server applications that would be able to communicate with each other over not just the LAN which I am able to do, but would be able to communicate with each other and exchange filestreams over the internet, meaning in most cases communication with PC's behind a router and / or a gateway server. I have applications that can send messages to each other and send filestreams to each other via socket ;however, these applications only take into account the IP address of the client and server applications and the port the socket is on, so I assume that I need to use the external IP of the router the application is behind and a port number on that connection perhaps? Can someone please point me in the right direction and tell me what I need to be trying to do (provide terms usable for a good google search, sample code, your own explanation) so that I can communicate with applications over the internet behind a router?

    Thanks in advance!
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    If a router is programmed to allow traffic through a port (i.e. opened up a port to a specfic computer insde it's network) you should be able to communicate through it.

    Example:

    I have a server behind a firewall. My software listens on port 9088. I tell my router that any incomming traffic for port 9088 should be directed to my server.
    Suppose the outside IP address for my router is 64.64.64.20

    You are behind a firewall, and have a copy of my client software. You would put in to connect to 64.64.64.20 on port 9088 and even though you are behind a firewall (this is only true on MOST routers for home use) your connection will go out and connect to me.

    Firewalls generally only stop traffic from getting in, not traffic going out.

    Comment

    • cnixuser
      New Member
      • Dec 2006
      • 80

      #3
      Originally posted by Plater
      If a router is programmed to allow traffic through a port (i.e. opened up a port to a specfic computer insde it's network) you should be able to communicate through it.

      Example:

      I have a server behind a firewall. My software listens on port 9088. I tell my router that any incomming traffic for port 9088 should be directed to my server.
      Suppose the outside IP address for my router is 64.64.64.20

      You are behind a firewall, and have a copy of my client software. You would put in to connect to 64.64.64.20 on port 9088 and even though you are behind a firewall (this is only true on MOST routers for home use) your connection will go out and connect to me.

      Firewalls generally only stop traffic from getting in, not traffic going out.

      Thanks for your reply,

      Since I am able to use instant messenger clients and have web pages displayed on the PC I am currently using which is behind my company's router / gateway server, would it be safe to say that port forwarding is already enabled, and that all I would have to ask IT to do is enable port forwarding for a new port that a specify for my application?

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        All of those program you named are all "client" programs, they send requests OUT through the firewall. None are indicitive of port forwarding.
        You could request a port be opened up for you, but they are going to want a good reason for doing so.

        Comment

        • cnixuser
          New Member
          • Dec 2006
          • 80

          #5
          Originally posted by Plater
          All of those program you named are all "client" programs, they send requests OUT through the firewall. None are indicitive of port forwarding.
          You could request a port be opened up for you, but they are going to want a good reason for doing so.
          But don't the client applications need to be able to listen on a specific port for messages incoming from the server?

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Originally posted by cnixuser
            But don't the client applications need to be able to listen on a specific port for messages incoming from the server?
            No. The server listens for connections, the client sends out a request to connect to it. A connection is established and communication occurs back and forth on that one connection.

            Comment

            • cnixuser
              New Member
              • Dec 2006
              • 80

              #7
              Originally posted by Plater
              No. The server listens for connections, the client sends out a request to connect to it. A connection is established and communication occurs back and forth on that one connection.
              Ok, let me see if I can articulate properly what you are trying to convey here to verify that I am understanding you correctly:

              Server Application -- Behind a router / gateway server with say port 12,000 opened on the external IP

              Client application -- Uses any open port on the router / gateway server it is behind, gets a connection by sending to the external IP address of the router the Server Application's PC is behind at port 12,000 (11.22.1.1:1200 0).

              The connection is established and all communication and filestream exchange between the 2 applications occurs and the server sends to the port on the external IP of the router the client pc is behind.

              Is this the basic gist of what you mean? Thanks again for the continued assistance :)

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                that sounds about right.

                Comment

                • cnixuser
                  New Member
                  • Dec 2006
                  • 80

                  #9
                  Originally posted by Plater
                  that sounds about right.

                  I believe this conversation will prove to be very fruitful, thank you for your assistance.

                  Comment

                  • Svinja
                    New Member
                    • Nov 2007
                    • 25

                    #10
                    You need to forward ports only for the server(incoming connection).
                    Client does not need to forward port because of the way NAT in your router works.

                    When client(outcomin g connection) start the connection, it sends some packets over the router, NAT(in the router) remembers clients IP and port and puts it in temporary table. That way when packets(incomin g) come for client, the router knows on which IP to send it because of the temporary tables.

                    Hope you got it. If you need some better explanation, google NAT.

                    Comment

                    Working...