无法通过TCP客户端C连接到目标计算机#

时间:2016-12-12 04:06:32

标签: c# tcp tcpclient tcplistener

我不确定下面的代码是否有问题。我只是尝试连接到目标IP,但它总是抛出异常“无法建立连接,因为目标计算机主动拒绝它”。

目标客户端计算机未联机,因此无法接受或拒绝来自服务器端的连接。在服务器上,我希望它继续尝试连接到客户端直到连接。不知怎的,它没有按预期工作。

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

谢谢。

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());  }

        }
    }
}

1 个答案:

答案 0 :(得分:0)

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