Library tutorials & articles

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

Comments

  1. 08 Jan 2005 at 15:46

    I need some help. I want to make the server client program work over the internet, not just a LAN. So my friend can use the client, and I can be the server and we can sedn messages back and forth. Can someone help me with this?

  2. 10 Jul 2003 at 01:43

                    we have established connection between server & client. It works ok when we pass queries from just one form. when we want to load many forms at a time with only a single connection some error has occured. only the first form loads from the database without error when the next form loads there is some error in the connection.
                        could you help us?
                                 

  3. 01 Jan 1999 at 00:00

    This thread is for discussions of SocketWrench Control.

Leave a comment

Sign in or Join us (it's free).

Catalyst Development

Related discussion

Related podcasts

  • Listener Feedback 67

    This mailbag episode includes FASM, scripts, sockets, SSL/TLS, HTTPS, Windows 7's XP mode, and more. Security Now wiki shownotes For 16kpbs versions, transcripts, and notes (including fixes), visit Steve's site: grc.com, also the home of the best disk maintenance and recovery utility ever written...

Want to stay in touch with what's going on? Follow us on twitter!