Using Buffers in TCP Mode

Top  Previous  Next

Once you have allocated memory for the TX and RX buffers you can start sending and receiving data through them. Since TCP is a stream-oriented protocol this is what buffers store- a stream of data being sent and received, without any separation into individual packets. Even for the outgoing data, you have no control over how it will be split into packets for transmission over the network.

Sending Data

Sending data a two-step process. First, you put the data in the TX buffer using the sock.setdata method, and then you perform the actual sending (commit the data) using the sock.send method. For example:

 

 

sock.setdata ("Foo") ' Placed our data in the TX buffer - not being sent out yet.

' ... more code...

sock.setdata ("Bar") ' Added even more data to the TX buffer, waiting to be sent.

sock.send ' Now data will actually start going out. Data sent will be 'FooBar'.

 

 

Since this is a two-step process, you may gradually fill the buffer to capacity, and only then send its contents.

 

note_further-wt

TiOS features non-blocking operation. This means that on sock.send, for example, the program does not halt and wait until the data is completely sent. In fact, execution resumes immediately, even before the first byte goes out. Your program will not freeze just because you ordered it to send a large chunk of data.

 

The data can be stored in the TX buffer at any time but it will only be sent out if and when the network connection is established. Storing the data in the TX buffer won't cause the socket to establish any connection automatically.

Receiving Data

Receiving data is a one-step process. To extract the data from the RX buffer, use the sock.getdata method. Data may only be extracted once from the buffer. Once extracted, it is no longer in the buffer. For example:

 

 

dim whatigot as string

whatigot = sock.getdata(255)

 

 

The string whatigot now contains up to 255 bytes of data which came from the RX buffer of the socket.

You can also preview the data in the RX buffer without actually extracting it. Use sock.peekdata to achieve this.

Discussion of TCP data RXing continues in Receiving Data in TCP mode.