To use a Socket for client-side communications, your sequence of actions will run something like this:
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.
Send the Socket the Connect message. When the connection is established, the Socket will receive a Connected event.
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.
Speak to the server by sending the Socket the Write message.
If any error occurs, you will receive an Error event; you can read the error number as the Socket’s LastErrorCode property.
When finished, send the Socket the Close message to disconnect from the network.
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 ...