-2

For example, if the computer's IP is under the Lan, it's 192.168.10.10 and Internet IP 10.10.10.10.

How to set up the Socket in C# to connect to the computer?

Some programs like "Ammyy Admin" do this.

public void Connect(string server)
{
    if (IsConnected)
        return;
    try
    {
        _server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        IPAddress[] allIp = Dns.GetHostAddresses(server);
        foreach (IPAddress ipa in allIp)
        {
            if (ipa.AddressFamily != AddressFamily.InterNetwork) continue;
            try
            {
                IPEndPoint remoteEndPoint = new IPEndPoint(ipa, DefaultPort);
                _server.Connect(remoteEndPoint);
                _server.SendBufferSize = SendReceveDataSize;
                _server.ReceiveBufferSize = SendReceveDataSize;
                DataHolder data = new DataHolder(_server, SendReceveDataSize);
                _server.BeginReceive(data.Buffer, 0, data.BufferSize, 0, ReceiveData, data);
                break;
            }
            catch { /* ignore */ }
        }
    }
    catch { /* ignore */ }
}

This code only works under Lan But I have to be able to connect to the user on the Internet

M.R.T
  • 746
  • 8
  • 20
  • Google "NAT" :-) – ProgrammingLlama Dec 26 '17 at 13:31
  • Where are you connecting from? Another local PC on an intranet or from another computer on the internet? Router IP addresses are fixed IP and must be set to the same subnet as the computers. If the router is set to a different subnet you will never be able to connect. – jdweng Dec 26 '17 at 13:34
  • 2
    are you asking "how can a custom server use UPnP to make itself available via port forwarding without having to configure the router manually?" ? – Marc Gravell Dec 26 '17 at 13:36

1 Answers1

4

You have a custom server on a private network. If a device in a different network wants to connect but there is no routable address, then it cannot do so unless the gateway/router (the device on the public routable address) has port forwarding configured that routes requests to a specific port through to the custom server, and has the port open in terms of firewall rules. This could be done manually at the gateway/router, or: if the gateway/router supports UPnP the custom server could attempt to configure the port forwarding automatically through code via the UPnP API.

However, it is usually more convenient to have the deployed nodes make outbound connections to a central server (typically over a port that is likely to be wide open such as 80/443).

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • It is assumed that I know the destination IP and I'm looking for a way to do this without manipulating the router's settings. – M.R.T Dec 26 '17 at 14:03
  • @M.R.T2017 if it isn't routable, knowing the IP isn't much help unless the protocol itself supports unwrapping and forwarding (like http proxies often do) – Marc Gravell Dec 26 '17 at 14:45
  • @M.R.T2017 if your end goal is something like peer to peer communication (where several clients can connect to each other) - you might want to look at tcp\udp hole punching. – Evk Dec 26 '17 at 15:12
  • 1
    @M.R.T2017 My network has 100 computers. Each one has a different LAN address, but they all share the same public address, which is set in my router. If you try to use my public address, which of the 100 computers are you trying to connect to? That is why you can't do it this way without configuring the router to tell it where to route the request. +1 for making an outbound connection from the client - you can prompt it to do so by using the type of message bus that is used for push notifications. – Dave Smash Dec 26 '17 at 16:00