简体   繁体   English

C#中的TCPClient(错误)

[英]TCPClient in C# (Error)

using System;
using System.Text;
using System.IO;
using System.Net.Sockets;

namespace ConsoleApp01
{
    class Program
    {
        public static void Main(string[] args)
        {
                 TcpClient client = new TcpClient("python.org",80);
                 NetworkStream ns = client.GetStream();
                 StreamWriter sw = new StreamWriter(ns);
                 sw.Write("HEAD / HTTP/1.1\r\n"
                        + "User-Agent: Test\r\n"
                        + "Host: www.python.org\r\n"
                        + "Connection: Close\r\n");
                  sw.Flush();

                  Console.ReadKey(true);
        }
    }
}

System.Net.Sockets.SocketException: Unable to make a connection because the target machine actively refused it at System.Net.Sockets.TcpClient..ctor at ConsoleApp01.Program.Main :line 12 System.Net.Sockets.SocketException:无法建立连接,因为目标计算机在ConsoleApp01.Program.Main的System.Net.Sockets.TcpClient..ctor上主动拒绝了该连接:第12行

Why do I get this error message? 为什么会收到此错误消息?

I tried the code you have posted. 我尝试了您发布的代码。 It works just fine. 它工作正常。

I think, either the domain name Python.org is not resolvable (possible if you are behind a firewall or are using a proxy server) or a firewall is blocking the connection or just that the request is going to the wrong computer. 我认为,域名Python.org不可解析(如果您位于防火墙后面或正在使用代理服务器,则可能是这样),或者防火墙正在阻止连接,或者仅仅是请求将发送到错误的计算机。

To verify that "python.org" is resolvable: 验证“ python.org”是否可解析:

open cmd prompt & type "ping 82.94.164.162". 打开cmd提示符并键入“ ping 82.94.164.162”。 If it responds back, it is reachable. 如果响应,则可以访问。

Then try ping -a 82.94.164.162. 然后尝试ping -a 82.94.164.162。 If it resolves to python.org than name is resolvable. 如果解析为python.org,则名称是可解析的。

Also check if Windows Firewall or third party firewall is blocking the communication. 还要检查Windows防火墙或第三方防火墙是否阻止了通信。

Also you can try connecting via IP. 您也可以尝试通过IP连接。 ie

//TcpClient client = new TcpClient("python.org", 80);
TcpClient client = new TcpClient("82.94.164.162", 80);

EDIT: As per tommieb75's suggestion, 编辑:根据tommieb75的建议,

using System;
using System.IO;
using System.Net.Sockets;

namespace ConsoleApp01
{
    class Program
    {
        public static void Main(string[] args)
        {
            try
            {
                //TcpClient client = new TcpClient("python.org", 80);
                TcpClient client = new TcpClient("82.94.164.162", 80);
                NetworkStream ns = client.GetStream();
                StreamWriter sw = new StreamWriter(ns);
                sw.Write("HEAD / HTTP/1.1\r\n"
                       + "User-Agent: Test\r\n"
                       + "Host: www.python.org\r\n"
                       + "Connection: Close\r\n");
                sw.Flush();
            }
            catch (SocketException ex)
            {
                Console.WriteLine(ex);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            Console.ReadKey(true);
        }
    }
}

It looks like you have the url incorrect for ' TcpClient ', and you are not calling the ' Connect ' method of the 'TcpClient' instance... here's the corrected version..see this here on MSDN ....If I were you, I would wrap this up in a try / catch clause to see the exact error... nitpicky aside, if you are using disposable objects, wrap them up in a using clause also! 看来您的网址不正确,不能用于“ TcpClient ”,并且您没有在调用“ TcpClient”实例的“ Connect ”方法...这是正确的版本。请在MSDN上查看此内容。您,我将其包装在try / catch子句中以查看确切的错误... nitpicky放在一边,如果您使用的是一次性对象,也将它们包装在using子句中!

try{
    TcpClient client = new TcpClient();
    client.Connect("www.python.org",80);
        ....
}catch(SocketException sockEx){
    /* Handle the socket exception.... */
}

python.org doesn't like you, or you're not really hitting python.org. python.org不喜欢您,或者您并不是真的很喜欢python.org。 An active refusal means the address is good, there is no firewall blocking, but the remote server responded with RST (TCP RESET). 主动拒绝表示地址正确,没有防火墙阻止,但远程服务器以RST(TCP RESET)响应。 Usually that means nothing is listening on the port - but since we all know that python.org does listen on port 80 that can't be it. 通常,这意味着没有任何内容在端口上侦听-但由于我们都知道python.org 确实在端口80 侦听,所以不能。 I'd guess that a web browser would also not work for you to browse python.org. 我猜想Web浏览器也不适合您浏览python.org。

FWIW, a firewall would swallow the initial SYN packet and never generate a RST. FWIW,防火墙将吞下初始SYN数据包,并且从不生成RST。 So, you'd end up with about a 15 second delay (as your client retries) and then a time out error. 因此,最终会导致15秒的延迟(客户端重试),然后出现超时错误。

You may want to verify that your python.org resolves to the same as everyone else's. 您可能需要验证python.org解析为与其他所有人相同。 If it doesn't, then your DNS is hosed. 如果没有,则说明您的DNS已中断。 If it does, then I'd suspect a transparent proxy between you and the web....Either way, it's more of a ServerFault question than a StackOverflow one at that point. 如果是这样,那么我怀疑您和网络之间存在一个透明的代理...。无论哪种方式,这都更是一个ServerFault问题,而不是此时的StackOverflow问题。

您能否通过telnet到计算机的端口,如果可以,则代码有问题,否则您在防火墙后面。

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

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