?
Solved

tcp client

Posted on 2008-10-09
2
Medium Priority
?
1,143 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

752 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