tags:

views:

72

answers:

4
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

Why do I get this error message?

A: 

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!

try{
    TcpClient client = new TcpClient();
    client.Connect("www.python.org",80);
        ....
}catch(SocketException sockEx){
    /* Handle the socket exception.... */
}
tommieb75
The URL isn't the issue: both python.org and www.python.org resolve to the same IP.
Steven Sudit
And it's not the code change, either, since passing those two fields to the constructor is the same as calling Connect with them.
Steven Sudit
Ganesh R.
@Ganesh: Wrap it up in a try/catch as suggested - you will get an answer as to why it is not working...perhaps, the DNS is screwed and cannot resolve the domain name or that the firewall is blocking it...
tommieb75
+2  A: 

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.

To verify that "python.org" is resolvable:

open cmd prompt & type "ping 82.94.164.162". If it responds back, it is reachable.

Then try ping -a 82.94.164.162. If it resolves to python.org than name is resolvable.

Also check if Windows Firewall or third party firewall is blocking the communication.

Also you can try connecting via IP. i.e.

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

EDIT: As per tommieb75's suggestion,

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);
        }
    }
}
Ganesh R.
CSharp could also try connecting to port 80 with telnet.
Steven Sudit
A: 

python.org doesn't like you, or you're not really hitting python.org. An active refusal means the address is good, there is no firewall blocking, but the remote server responded with 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. I'd guess that a web browser would also not work for you to browse python.org.

FWIW, a firewall would swallow the initial SYN packet and never generate a RST. So, you'd end up with about a 15 second delay (as your client retries) and then a time out error.

You may want to verify that your python.org resolves to the same as everyone else's. If it doesn't, then your DNS is hosed. 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.

Mark Brackett
A: 

Can you telnet to the port form your machine, if you can then there is something wrong with the code, else you are behind a firewall.

anijhaw