简体   繁体   English

如何通过IPAddress连接计算机

[英]How to connect computers via IPAddress

This is related to c# Sockets. 这与c#套接字有关。 I'm developping a socket program. 我正在开发一个套接字程序。 But there is one problem in my software. 但是我的软件存在一个问题。 I cannot connect to computers which are connected to internet via theirs local network. 我无法连接通过本地网络连接到Internet的计算机。 But I can connect to computers that are uses internet alone. 但是我可以连接到仅使用互联网的计算机。 Be more descriptive, Consider that 5 computers connects internet via a modem that uses 1 ip address. 更具描述性,请考虑5台计算机通过使用1个IP地址的调制解调器连接Internet。 When I try to reach one of these computers, API connects to modem via its ip address. 当我尝试访问其中一台计算机时,A​​PI通过其IP地址连接到调制解调器。 But there is no response. 但是没有回应。 Because a modem don't respond a computer request. 因为调制解调器不响应计算机请求。 By the mean, my API must reach a computer not only modem. 意思是说,我的API必须不仅要访问调制解调器,还要访问计算机。 And and an SocketException is thrown. 并且,抛出SocketException。 What can I do about this problem? 我该怎么办?

The problem you're encountering is caused by NAT . 您遇到的问题是由NAT引起的。 This is used by routers who allow multiple clients to go online through one public IP address. 路由器允许多个客户端通过一个公用IP地址联机。 None of the clients behind the router will 'know' or 'see' this, but it limits connectivity. 路由器后面的任何客户端都不会“知道”或“看到”此消息,但是它限制了连接性。

Clients can initiate a connection to the outside, but the other way around is basically* impossible. 客户可以启动与外部的连接,但是基本上*是不可能的。 Unless you use port forwarding , where one or more ports are forwarded to a client behind the router. 除非使用端口转发 ,否则一个或多个端口将转发到路由器后面的客户端。 This way, connections can be initiated from the outside. 这样,可以从外部启动连接。

This requires configuration on the client side however, so the preferred way would be to let your clients connect to your server, since that will always be possible (firewalls neglected). 但是,这需要在客户端进行配置,因此首选方法是让您的客户端连接到服务器,因为这总是可能的(忽略防火墙)。

*: There also is a workaround, where you let a client connect to your server, and then pass the connection information to another client, so those clients can communicate with each other. *:还有一种解决方法,您可以让一个客户端连接到服务器,然后将连接信息传递给另一个客户端,以便这些客户端可以相互通信。 This is called 'nat punching' and is used by torrent clients, for example. 这称为“ natpunching”,例如,torrent用户使用。

you can try something like this..... 您可以尝试这样的事情.....

server Side Code 服务器端代码

namespace Consoleserver
{
class Program
{
    static void Main(string[] args)
    {

        IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
    IPAddress ipAddress = ipHostInfo.AddressList[0];
    IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);


    Socket listener = new Socket(AddressFamily.InterNetwork,
        SocketType.Stream, ProtocolType.Tcp );


    try
    {
        listener.Bind(localEndPoint);
        listener.Listen(10);


        while (true)
        {
            Console.WriteLine("Waiting for a connection...");

            Socket handler = listener.Accept();
            Console.WriteLine("connected");
        }

    }
    catch { Console.WriteLine("error"); 
    }
  }
}

Client side code : 客户端代码:

 namespace consoleclient
 {
   class Program
   {
    static void Main(string[] args)
    {
        try
        {

            IPHostEntry ipHostInfo = Dns.Resolve("59.178.131.180");
            IPAddress ipAddress = ipHostInfo.AddressList[0];
            IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);


            Socket sender = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);


            try
            {
                sender.Connect(remoteEP);

                Console.WriteLine("Socket connected <strong class="highlight">to</strong> {0}",
                    sender.RemoteEndPoint.ToString());
            }
            catch (ArgumentNullException ane)
            {
                Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
                Console.Read();
            }
            catch (SocketException se)
            {
                Console.WriteLine("SocketException : {0}", se.ToString());
                Console.Read();
            }
            catch (Exception e)
            {
                Console.WriteLine("Unexpected exception : {0}", e.ToString());
                Console.Read();
            }

        }
        catch
        { Console.WriteLine("could not <strong class="highlight">connect</strong> A"); }
    }
  }
}

I hope it will helps you 希望对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM