Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 34,259 times

Contents

Related Categories

SocketWrench Control - Listening for Connections

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

  • BLAH

    Posted by millffire on 08 Jan 2005

    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...

  • help

    Posted by fms on 10 Jul 2003

    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 so...