Solved

tcp client

Posted on 2008-10-09
2
1,136 Views
Last Modified: 2008-10-10
Hi, I'm working on a C# tcp client that will make a simple http request. Here's my code so far:



            string http;
            http = "GET /2008/10/02/android-the-next-wave/ HTTP/1.1\r\n";
            http += "Host: www.solutionbot.com\r\n";
            http += "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3 (.NET CLR 3.5.30729)\r\n";
            http += "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Language: en-us,en;q=0.5\r\n";
            http += "Accept-Encoding: gzip,deflate\r\n";
            http += "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\nKeep-Alive: 300\r\nConnection: keep-alive\r\nCache-Control: max-age=0\r\n\r\n";



            Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            client.Connect("www.google.com", 80);

            NetworkStream ns = new NetworkStream(client);

            StreamWriter sw = new StreamWriter(ns);

            sw.Write(http);
            sw.Flush();

            string buffer = "";
            int k = client.ReceiveBufferSize;
            Byte[] bb = new Byte[k];

            int i = ns.Read(bb, 0, k);

            buffer = Encoding.UTF8.GetString(bb);

            while (true)
            {
                int j = ns.ReadByte();

                if (j == -1) break;

                char c = (char)j;
                buffer += c.ToString();
            }

 
           
            client.Close();

            string headers = buffer.Substring(0, buffer.IndexOf("\r\n\r\n"));
            string content = buffer.Substring(buffer.IndexOf("\r\n\r\n"));

            textBox1.Text = buffer;


the problem with this code is the fact that it is really slow (for obvious reasons). It reads one byte at a time. The reason for this is because the buffer size isn't the entire amount of data that gets sent over. I can't seem to find any other method though. I don't want to use the httpwebrequest as i really like the flexability of the sockets class. Can anyone help?
0
Comment
Question by:hobeau
2 Comments
 
LVL 7

Accepted Solution

by:
Zippit earned 500 total points
ID: 22683755
actually...take a look at MSDN:

http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx

you can use the following instead:

int bytes = 0;
do {
            bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
            page = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes);
        }
        while (bytes > 0);

If you still find it slow, you may want at making it asynchronous:

http://www.codeguru.com/csharp/csharp/cs_network/sockets/article.php/c7695
0
 

Author Comment

by:hobeau
ID: 22691834
Awesome! so simple. Thanks Zippit.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now