Glossary Item Box
As described in the topic Tcp Component: Basic Sending And Receiving Of Data, there are three basic techniques for sending and receiving data: Receiving all data, receiving data until a delimiter is reached, and receiving fixed-size "chunks" of data. This example demonstrates receiving data until a delimiter is reached.
It can often be useful to receive data until a delimiter is reached. For example, if you are implementing the HTTP protocol and just want to read the headers, you could simply read until CRLFCRLF, signifying the end of the headers. As another example, both the FTP and NNTP protocols return a listing delimited by CRLF. If you wish to read the listing one line at a time (so you can parse the line and handle it within your application), read until you find the CRLF. The following example demonstrates reading an HTTP response, throwing out the headers, then reading the actual page content.
To read until a delimiter is reached.
[Visual Basic, C#]
[C#] tcp1.Connect("www.dart.com", 80); [Visual Basic] Tcp1.Connect("www.dart.com", 80)
[Visual Basic, C#]
[C#] // Send GET command to get a page. (This demonstrates writing data to the socket using the stream interface). tcp1.Stream.Write("GET / HTTP/1.0\r\n\r\n"); [Visual Basic] ' Send GET command to get a page. (This demonstrates writing data to the socket using the stream interface). Tcp1.Stream.Write("GET / HTTP/1.0" + vbCrLf + vbCrLf)
[Visual Basic, C#]
[C#] // Read until the headers are reached. bool found = false; // This method reads data from the server until a double CRLF is found // marking the end of the headers, or 1024 bytes are read. tcp1.Stream.Read("\r\n\r\n", 1024, ref found); [Visual Basic] ' Read until the headers are reached Dim found As Boolean = False ' This method reads data from the server until a double CRLF is found ' marking the end of the headers, or 1024 bytes are read. Tcp1.Stream.Read(vbCRLf + vbCrLf, 1024, found);
[Visual Basic, C#]
[C#] // Now read the rest of the page string pagedata = ""; while(tcp1.Connected) { // Now that the headers have been read, continue reading the rest of the response from the server. pagedata += tcp1.Stream.Read(); } // Display page data Debug.WriteLine(pagedata); [Visual Basic] ' Now read the rest of the page Dim pagedata As String = "" While Tcp1.Connected ' Now that the headers have been read, continue reading the rest of the response from the server. pagedata += Tcp1.Stream.Read() Loop ' Display page data Debug.WriteLine(pagedata)
Send comments on this topic.
Documentation version 1.1.2.0.
© 2008 Dart Communications. All rights reserved.