Solved

tcp client

Posted on 2008-10-09
2
1,135 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
Comment Utility
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
Comment Utility
Awesome! so simple. Thanks Zippit.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

11 Experts available now in Live!

Get 1:1 Help Now