views:

36

answers:

3

Hey guys, I need to connect to the IP Address "127.0.0.1:4000", the problem is that I can't find a way to do that in C#, WebRequest only supports URIs (to my knowledge), and I couldn't find a socket function to do it either. Any Help with this would be great, Thanks, Max

A: 

You could use a TcpClient or raw Socket:

using (var client = new TcpClient("127.0.0.1", 4000))
using (var stream = client.GetStream())
{
    using (var writer = new StreamWriter(stream))
    {
        // Write something to the socket
        writer.Write("HELLO");
    }

    using (var reader = new StreamReader(stream))
    {
        // Read the response until a \r\n
        string response = reader.ReadLine();
    }
}

Remark: If it is a binary protocol you should directly write/read to the socket without using StreamWriter and StreamReader.

Darin Dimitrov
+1  A: 

You can set the Proxy property of the HttpWebRequest, that should do the trick.

Pretty nice simple example here (In VB though, but not hard to translate)..

Rob Cooper
I tried the code you gave, but it returned the same error I have been experiencing, on the line:HttpWebRequest webReq = ((HttpWebRequest)(WebRequest.Create(url)));(where url = 127.0.0.1:4000)
mazzzzz
A: 

Thanks for the help guys, I found what I was Looking for!

<code>
    /// <summary>
    /// Gets the contents of a page, using IP address not host name
    /// </summary>
    /// <param name="host">The IP of the host</param>
    /// <param name="port">The Port to connect to</param>
    /// <param name="path">the path to the file request (with leading /)</param>
    /// <returns>Page Contents in string</returns>
    private string GetWebPage(string host, int port,string path)
    {
        string getString = "GET "+path+" HTTP/1.1\r\nHost: www.Me.mobi\r\nConnection: Close\r\n\r\n";
        Encoding ASCII = Encoding.ASCII;
        Byte[] byteGetString = ASCII.GetBytes(getString);
        Byte[] receiveByte = new Byte[256];
        Socket socket = null;
        String strPage = null;
        try
        {
            IPEndPoint ip = new IPEndPoint(IPAddress.Parse(host), port);
            socket = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            socket.Connect(ip);
        }
        catch (SocketException ex)
        {
            Console.WriteLine("Source:" + ex.Source);
            Console.WriteLine("Message:" + ex.Message);
            MessageBox.Show("Message:" + ex.Message);
        }
        socket.Send(byteGetString, byteGetString.Length, 0);
        Int32 bytes = socket.Receive(receiveByte, receiveByte.Length, 0);
        strPage = strPage + ASCII.GetString(receiveByte, 0, bytes);

        while (bytes > 0)
        {
            bytes = socket.Receive(receiveByte, receiveByte.Length, 0);
            strPage = strPage + ASCII.GetString(receiveByte, 0, bytes);
        }
        socket.Close();
        return strPage;
    }

Thanks again for the help, couldn't have found it any other way.

mazzzzz