无法建立连接,因为目标计算机主动拒绝它127.0.0.1:8082

时间:2013-11-28 13:28:22

标签: c# sockets localhost tcplistener

我已经制作了一个小的Windows服务,我需要从我自己的localhost系统连接,但在运行应用程序时,我收到了错误...

No connection could be made because the target machine actively refused it 127.0.0.1:8082

虽然它完全适用于Live ip和端口号,但我需要在localhost上测试它,因为我无法每次都访问实时运行的ip ..

这是我的代码......

byte[] data = new byte[1024];
         string input, stringData;
         String ip = "127.0.0.1";
         Int32 port = 8082;
         string path = "D:\\";

            if (File.Exists("ipsettings.txt"))
            {
                File.Delete("ipsettings.txt");
            }
            IPAddress ipad = IPAddress.Parse(ip);
            IPEndPoint ipend = new IPEndPoint(ipad, port);
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                sock.Connect(ipend);

            }
            catch (Exception ex)
            {
                throw ex;
            }

此时我收到错误..

 try
            {
                sock.Connect(ipend);

            }
            catch (Exception ex)
            {
                throw ex;
            }

请帮我解决问题.. 提前谢谢..

1 个答案:

答案 0 :(得分:0)

  

虽然它在Live ip和端口号中完美地工作

你怎么知道的?错误说恰恰相反。没人在听IP:端口。从sysinternals套件运行tcpview以了解更多信息。注意,监听机器自己的IP地址,如192.168.55.55,监听127.0.0.1,监听*(每个地址)是不同的事情。当您连接到127.0.0.1时,它将成功连接到后两个,但不是第一个。如果另一端正在侦听UDP而不是TCP,那么它也可能出错。

相关问题