SocketWrench Control

Communicating

Now that the code to establish the connection has been included, the next step is to actually send and receive data to and from the server. To do this, the Text2 control should have the following code added to it's KeyPress event:

Sub Text2_KeyPress (KeyAscii As Integer)
If KeyAscii = 13 Then
    Socket1.SendLen = Len(Text2.Text)
    Socket1.SendData = Text2.Text
    KeyAscii = 0: Text2.Text = ""
End If
End Sub

If the user presses the Enter key in the Text2 control, then that text is sent down to the echo server. The properties used to send data are as follows:

Property Description
SendLen This property specifies the length of the data being sent to the server. It should always be set before the data is written to the socket. After the data has been sent, the value of the property is adjusted to indicate the actual number of bytes that have been written.
SendData Setting this property causes the data assigned to it to be written to the socket. The number of bytes actually written may be less than the amount specified in the SendLen property if the socket buffers become full.

Once the data has been sent to the server, it immediately sends the data back to the client. This generates a Read event in SocketWrench, which should have the following code:

Sub Socket1_Read (DataLength As Integer, IsUrgent As Integer)
    Socket1.RecvLen = DataLength
    Text3.Text = Socket1.RecvData
End Sub

The properties used to receive the data are as follows:

Property Description
RecvLen This property specifies the maximum number of bytes that should be read from the socket. After the data has been received, the value is changed to reflect the number of bytes actually read.
RecvData Reading this property causes data to be read from the socket, up to the maximum number of bytes specified by the RecvLen property. If the socket is non-blocking and there is no data to be read, an error is generated.

The Read event is passed two parameters, the number of bytes that are available to be read, and a flag that specifies if the data is urgent (also known as "out-of-band" data, the use of urgent data is an advanced topic outside of the scope of this document). For more information about the Read event, please refer to the SocketWrench/VB Technical Reference.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“If Java had true garbage collection, most programs would delete themselves upon execution.” - Robert Sewell