Navigation:  Programming Cookbook > Sockets Connectivity > Monitoring Blocking Calls >

Talking Bytes across a Socket Connection

Previous pageReturn to chapter overviewNext page

The lowest level of Socket communication is the sending and receiving of bytes. You can use the #sendByte: method to send single bytes (integers between zero and 255 inclusive). For sending larger quantities of data the #sendByteArray: method is more efficient. These calls will block until the data has actually been sent, although not necessarily until it has been received at far end of the connection.

Examples

In the Client workspace send some bytes to socketB.

socketB sendByte: 255.

socketB sendByteArray: #[1 2 3 4].

socketB sendByteArray: 'hello'.

 

Use #receiveByte to read a single byte from a Socket and to answer its integer value. If no data is available to be read, the call will block until the first byte is received. If you are expecting a number of bytes, then you can use #receiveByteArray: to receive these into a ByteArray in a single operation. Note, though, that you must provide #receiveByteArray: with the size of the array to allocate and return. The call will block until at least this number of bytes has been received by the Socket.

You may want to find out if a Socket has data available to be read before issuing one of these blocking calls. You can test this using the #hasInput method, which will answer true if one or more bytes are queued up waiting to be received.

Examples

In the Server workspace let's receive the bytes from socketA that we sent earlier through socketB. You should display the results of evaluating each of the following.

In the Server workspace:

socketA hasInput.

socketA receiveByte.

socketA receiveByteArray: 4.

(socketA receiveByteArray: 5) asString.

socket hasInput.

 

The last test for input should return false to indicate that there are no more bytes waiting for collection from the Socket.