Glossary Item Box
When communicating with a server using TCP, there are three basic techniques for receiving data:
Most communication with the server involves receiving all data. In most circumstances, the data to be received will be less than the internal buffer size for receiving data (returned by ReceiveBufferSize) so a single Receive operation should receive all available data. The data could be greater than the internal buffer for receiving data however, in which case multiple Receive operations will be required (See example below). Typically, the server will close the connection after sending all data, so you must keep receiving in a loop until Connected = false.
To receive all data from the server.
[Visual Basic, C#]
[C#] tcp1.Connect("myserver", 79); [Visual Basic] Tcp1.Connect("myserver", 79)
[Visual Basic, C#]
[C#] // Get information for account "test" tcp1.Send("test\r\n"); [Visual Basic] ' Get information for account "test" tcp1.Send("test" + vbCrLF)
The server should send a response at this point, hopefully providing information about the user name account. Receive the response from the server using Receive. The Receive method returns a SegmentEventArgs.Segment object which encapsulates the data returned. The returned data is likely to be less than the internal receive buffer and, as a result, a single Receive operation would suffice. For demonstration purposes, however, it will be assumed that the data may be larger than internal receive buffer, requiring multiple Receive operations.
To test this, simply set ReceiveBufferSize to a small number (like 100) to change the size of the internal receive buffer. This will almost guarantee that you will need to call multiple Receive methods to receive all data.
[Visual Basic, C#]
[C#] string s = ""; // Keep receiving until the connection is closed while(tcp1.Connected) { Segment seg = tcp1.Receive(); s += seg.ToString(); } [Visual Basic] Dim s as string = "" ' Keep receiving until the connection is closed while(Tcp1.Connected) Dim seg as Segment = Tcp1.Receive() s += seg.ToString() Loop
[Visual Basic, C#]
[C#] Debug.WriteLine("Result from Finger query: " + s); [Visual Basic] Debug.WriteLine("Result from Finger query: " + s)
Send comments on this topic.
Documentation version 1.1.2.0.
© 2008 Dart Communications. All rights reserved.