Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过IP地址连接计算机_C#_Sockets_Networking - Fatal编程技术网

C# 如何通过IP地址连接计算机

C# 如何通过IP地址连接计算机,c#,sockets,networking,C#,Sockets,Networking,这与c#Sockets有关。 我正在开发一个套接字程序。但是我的软件有一个问题。 我无法连接到通过本地网络连接到internet的计算机。但我可以连接到单独使用互联网的计算机。更具描述性, 考虑到5台计算机通过使用1 IP地址的调制解调器连接因特网。当我试图接触其中一台计算机时,API通过其ip地址连接到调制解调器。但没有回应。因为调制解调器不响应计算机请求。顺便说一下,我的API必须到达计算机,而不仅仅是调制解调器。然后抛出一个SocketException。我能为这个问题做些什么 您遇到的问

这与c#Sockets有关。 我正在开发一个套接字程序。但是我的软件有一个问题。 我无法连接到通过本地网络连接到internet的计算机。但我可以连接到单独使用互联网的计算机。更具描述性,
考虑到5台计算机通过使用1 IP地址的调制解调器连接因特网。当我试图接触其中一台计算机时,API通过其ip地址连接到调制解调器。但没有回应。因为调制解调器不响应计算机请求。顺便说一下,我的API必须到达计算机,而不仅仅是调制解调器。然后抛出一个SocketException。我能为这个问题做些什么

您遇到的问题是由以下原因引起的。这是由允许多个客户端通过一个公共IP地址联机的路由器使用的。路由器后面的所有客户端都不会“知道”或“看到”,但这会限制连接

客户端可以启动到外部的连接,但另一种方式基本上是不可能的。除非使用,否则将一个或多个端口转发到路由器后面的客户端。这样,可以从外部启动连接

但是,这需要在客户端进行配置,因此首选的方法是让客户端连接到服务器,因为这始终是可能的(防火墙被忽略)


*:还有一种变通方法,让客户端连接到服务器,然后将连接信息传递给另一个客户端,以便这些客户端可以相互通信。这被称为“nat打孔”,例如,torrent客户端使用它。

您可以尝试类似的方法

服务器端代码

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"); 
    }
  }
}
客户端代码:

 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"); }
    }
  }
}
名称空间控制台客户端
{
班级计划
{
静态void Main(字符串[]参数)
{
尝试
{
IPHostEntry ipHostInfo=Dns.Resolve(“59.178.131.180”);
IPAddress IPAddress=ipHostInfo.AddressList[0];
IPEndPoint remoteEP=新IPEndPoint(ipAddress,11000);
套接字发送器=新套接字(AddressFamily.InterNetwork,
流,ProtocolType.Tcp);
尝试
{
发送方连接(remoteEP);
WriteLine(“套接字连接到{0}”,
sender.RemoteEndPoint.ToString());
}
捕集物(捕集物)
{
WriteLine(“ArgumentNullException:{0}”,ane.ToString());
Console.Read();
}
捕获(SocketException se)
{
WriteLine(“SocketException:{0}”,se.ToString());
Console.Read();
}
捕获(例外e)
{
WriteLine(“意外异常:{0}”,e.ToString());
Console.Read();
}
}
抓住
{Console.WriteLine(“无法连接”A”);}
}
}
}
我希望它能帮助你