SocketWrench Control

Listening for Connections

By setting the Action property to SOCKET_LISTEN in the form's Load event, the program will start listening for connections as soon as the program starts executing. When a client tries to connect with your server, Socket2's Accept event will fire. The code for this event should look like this:

Sub Socket2_Accept (Index As Integer, SocketId As Integer)

Dim I As Integer
For I = 1 To LastSocket

If Not Socket2(I).Connected Then Exit For

Next I
If I > LastSocket Then

LastSocket = LastSocket + 1: I = LastSocket
Load Socket2(I)

End If

Socket2(I).AddressFamily = AF_INET
Socket2(I).Protocol = IPPROTO_IP
Socket2(I).SocketType = SOCK_STREAM
Socket2(I).Binary = True
Socket2(I).BufferSize = 1024
Socket2(I).Blocking = False
Socket2(I).Accept = SocketId

End Sub

The first statement loads a new instance of the SocketWrench/VB control as part of the Socket2 control array. The next six lines initialize the control's properties, and then the Accept property is set to the value of the SocketId parameter that is passed to the control. After executing this statement, the control is now ready to start communicating with the client program.

Since it's the job of an echo server to echo back whatever is sent to it, we have to add code to the control's Read event, which tells it that the client has sent some data to us. It looks like this:

Sub Socket2_Read (Index As Integer, DataLength As Integer, IsUrgent As Integer)

Socket2(Index).RecvLen = DataLength
Socket2(Index).SendLen = DataLength
Socket2(Index).SendData = Socket2(Index).RecvData

End Sub

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.

“God could create the world in six days because he didn't have to make it compatible with the previous version.”