Solved

tcp client

Posted on 2008-10-09
2
1,140 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

763 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