Solved

tcp client

Posted on 2008-10-09
2
1,141 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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