2016-12-12 122 views
0

我不确定下面的代码是否有问题。我只是尝试连接到一个目标IP,但它总是会抛出一个异常“因为目标机器主动拒绝它而无法建立连接”。无法通过TCP客户端C连接到目标机器#

目标客户机不在线,因此它无法接受或拒绝来自服务器端的连接。在服务器上,我希望它一直尝试连接到客户端直到连接。不知何故,它不像预期的那样工作。

我认为原因是防病毒或防火墙,所以我确实禁用了这些,但问题仍然存在。有没有人可以测试代码,并让我知道为什么它总是抛出异常?

谢谢。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Net; 
using System.Net.Sockets; 
using System.Threading; 
using System.Windows.Forms; 

namespace Server 
{ 
    class Program 
    { 
     public static TcpClient client = new TcpClient(); 
     public static int port = 6446; 
     public static string connectTo = "192.168.0.11"; 

     static void Main(string[] args) 
     { 
      try 
      { 
       do 
       { 
        Console.WriteLine("Connecting..."); 
        IPAddress ipAddress = IPAddress.Parse(connectTo); 
        client.Connect(ipAddress, port); 

       } while (client.Connected != true); 
       //dataStream = client.GetStream(); 
       //getMessages.Start(); 
      } 
      catch (Exception ex) { MessageBox.Show(ex.ToString()); } 

     } 
    } 
} 

回答

0

这是一个内部IP,只有你或他们网段上的其他人可以接触到它。你总是可以尝试ping或telnet到特定的端口。 Telnet将连接,超时或拒绝连接。

相关问题