O'Reilly logo

REALBasic: TDG, 2nd Edition by Matt Neuburg

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Client

To use a Socket for client-side communications, your sequence of actions will run something like this:

  1. Initialize the Socket’s Address property with the IP address of the server, and its Port property with the port number on which the server is listening.

  2. Send the Socket the Connect message. When the connection is established, the Socket will receive a Connected event.

  3. You will be notified of any incoming responses from the server by means of the DataAvailable event. You will typically extract any complete commands from the incoming data with the Read or ReadAll method.

  4. Speak to the server by sending the Socket the Write message.

  5. If any error occurs, you will receive an Error event; you can read the error number as the Socket’s LastErrorCode property.

  6. When finished, send the Socket the Close message to disconnect from the network.

Buffered Read

The chief complication in working with a Socket is that the network is an entity with a life of its own; it supplies or accepts data in quantities and at moments that are of its own choosing. Therefore, a Socket sends and receives data across the network asynchronously, meaning that your code is not in charge of when data arrives or is sent; instead, your code must read and write data by way of the Socket’s buffer, a block of memory that accumulates data temporarily, until its intended recipient has a chance to extract it. Your code may have to do a little extra work in order to interact properly with this buffer.

For example, let’s ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required