eelpark
asked on
VB Winsock server communications using UDTs
Does anyone have a GOOD example of using the Winsock control in a server to communicate with multiple clients using any type of messaging/UDTs? Especialy if the message is longer than a single TCP/IP packet.
An example could be a server that processes application request for multiple clients and returns results or just other messages. Another example would be a game server controlling several players.
An example could be a server that processes application request for multiple clients and returns results or just other messages. Another example would be a game server controlling several players.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I am just looking for a good example. I know the concepts but I just want to see it working in VB. ;-)
If you can send me the code I'll go ahead and close this question.
BTW, does it handle multiple clients?
Thanks
If you can send me the code I'll go ahead and close this question.
BTW, does it handle multiple clients?
Thanks
Does it have to use the winsock layer?
I have a component that uses mailslots and handles multiple clients...
I have a component that uses mailslots and handles multiple clients...
Here is the code that handle the connection request. I am spawning multiple process of another application and conversing using tcpip. I did not take out any of the code that spawn the new process for fear of breaking something and not giving you the full effect of the code. Below this is the code that maintains the multiple buffers in the DataArrival event. Hope this helps. Enjoy.
Private Sub sckClient_ConnectionReques t(Index As Integer, ByVal requestID As Long)
'************************* ********** ********** ********** ******
'* Written By: Todd Wolff
'* Date: January 7, 1999
'*
'* Syntax: sckClient_ConnectionReques t(Index,re questID)
'*
'* Paramters: Index Index of the control in the array
'* requestID ID of the Connection Request
'*
'* Purpose: This is a winsock event. It is fired when a
'* connection is requested from a remote winsock.
'* This will try to reuse closed connections
'* otherwise it will add a winsock to the array
'* and make the connection.
'************************* ********** ********** ********** ******
Dim x As Integer
On Error GoTo sckClient_ConnectionReques t_Err
If Index = 0 Then
'--- try to find a connection that is closed
For x = 1 To m_intLastClient
'--- if the state is closed then exit the loop
If sckClient(x).State = sckClosed Or sckClient(x).State = sckClosing Then
'--- just a precaution
m_strClientBuffer(x) = vbNullString
sckClient(x).Close
Exit For
End If
Next 'x
'--- if a winsock is closed x will equal the number
'--- that was closed. If a no winsock was found
'--- to be closed x will be equal to m_intLastClient
'--- plus one.
If x > m_intLastClient Then
m_intLastClient = x
'--- add the new winsock to the control array
ReDim Preserve m_strClientBuffer(m_intLas tClient)
Load sckClient(m_intLastClient)
End If
'---
With sckClient(x)
' With sckClient(m_intLastClient)
'--- set the port of the control
.LocalPort = 0
'--- accept the request
.Accept requestID
'--- save processid for later
.Tag = m_dblLastProcess
'--- id there is a message then send it now
' sckClient(m_intLastClient) ,
If Len(m_strCurrMsgCode) > 0 Then
g_objProcesses.RunSQLStmt m_strCurrMsgCode, _
m_strCurrID, _
m_strCurrlParam, _
m_strCurrwParam, _
sckClient(x), _
m_dblLastProcess
'--- reset the current message variables
m_strCurrMsgCode = vbNullString
m_strCurrID = vbNullString
m_strCurrlParam = vbNullString
m_strCurrwParam = vbNullString
End If
End With
Else
MsgBox "Index = " & Index
End If
sckClient_ConnectionReques t_Exit:
Exit Sub
sckClient_ConnectionReques t_Err:
LogError Err.Number, Err.Source, Err.Description
Resume sckClient_ConnectionReques t_Err
End Sub
'****************** Data Arrival Event *******************'
Private Sub sckClient_DataArrival(Inde x As Integer, ByVal bytesTotal As Long)
'************************* ********** ********** ********** ******
'* Written By: Todd Wolff
'* Date: January 4, 1999
'*
'* Syntax: mvarWinsockConn_DataArriva l(bytesTot al)
'*
'* Paramters: bytesTotal The number of bytes in the buffer
'*
'* Purpose: This is a winsock event. It is fired when the
'* winsock receives data. This will receive the
'* responses from the clients and will forward them
'* through the outside winsock.
'************************* ********** ********** ********** ******
Dim strData As String
On Error GoTo sckClient_DataArrival_Err
'--- get the data from the tcp buffer
sckClient(Index).GetData strData
'--- check the first character to see if we need to reset the buffer
If Left$(strData, 1) = g_BOT Then
'--- check the length of the buffer to ensure
'--- that it is empty
If Len(m_strClientBuffer(Inde x)) > 0 Then
m_strClientBuffer(Index) = vbNullString
End If
End If
'--- append the received data to the buffer
m_strClientBuffer(Index) = m_strClientBuffer(Index) & strData
'--- check the last character to see if
'--- the transmission has ended
If Right$(m_strClientBuffer(I ndex), 1) = g_EOT Then
'--- reset the buffer and processing information
strData = m_strClientBuffer(Index)
m_strClientBuffer(Index) = vbNullString
If m_bolCaptureIncoming Then
m_bolClientReceived = True
Else
m_bolClientReceived = False
'--- send the data to the client
sckServer.SendData strData
End If
g_objProcesses("RS" & sckClient(Index).Tag).Proc essing = False
End If
sckClient_DataArrival_Exit :
Exit Sub
sckClient_DataArrival_Err:
LogError Err.Number, Err.Source, Err.Description
Resume sckClient_DataArrival_Exit
End Sub
Private Sub sckClient_ConnectionReques
'*************************
'* Written By: Todd Wolff
'* Date: January 7, 1999
'*
'* Syntax: sckClient_ConnectionReques
'*
'* Paramters: Index Index of the control in the array
'* requestID ID of the Connection Request
'*
'* Purpose: This is a winsock event. It is fired when a
'* connection is requested from a remote winsock.
'* This will try to reuse closed connections
'* otherwise it will add a winsock to the array
'* and make the connection.
'*************************
Dim x As Integer
On Error GoTo sckClient_ConnectionReques
If Index = 0 Then
'--- try to find a connection that is closed
For x = 1 To m_intLastClient
'--- if the state is closed then exit the loop
If sckClient(x).State = sckClosed Or sckClient(x).State = sckClosing Then
'--- just a precaution
m_strClientBuffer(x) = vbNullString
sckClient(x).Close
Exit For
End If
Next 'x
'--- if a winsock is closed x will equal the number
'--- that was closed. If a no winsock was found
'--- to be closed x will be equal to m_intLastClient
'--- plus one.
If x > m_intLastClient Then
m_intLastClient = x
'--- add the new winsock to the control array
ReDim Preserve m_strClientBuffer(m_intLas
Load sckClient(m_intLastClient)
End If
'---
With sckClient(x)
' With sckClient(m_intLastClient)
'--- set the port of the control
.LocalPort = 0
'--- accept the request
.Accept requestID
'--- save processid for later
.Tag = m_dblLastProcess
'--- id there is a message then send it now
' sckClient(m_intLastClient)
If Len(m_strCurrMsgCode) > 0 Then
g_objProcesses.RunSQLStmt m_strCurrMsgCode, _
m_strCurrID, _
m_strCurrlParam, _
m_strCurrwParam, _
sckClient(x), _
m_dblLastProcess
'--- reset the current message variables
m_strCurrMsgCode = vbNullString
m_strCurrID = vbNullString
m_strCurrlParam = vbNullString
m_strCurrwParam = vbNullString
End If
End With
Else
MsgBox "Index = " & Index
End If
sckClient_ConnectionReques
Exit Sub
sckClient_ConnectionReques
LogError Err.Number, Err.Source, Err.Description
Resume sckClient_ConnectionReques
End Sub
'****************** Data Arrival Event *******************'
Private Sub sckClient_DataArrival(Inde
'*************************
'* Written By: Todd Wolff
'* Date: January 4, 1999
'*
'* Syntax: mvarWinsockConn_DataArriva
'*
'* Paramters: bytesTotal The number of bytes in the buffer
'*
'* Purpose: This is a winsock event. It is fired when the
'* winsock receives data. This will receive the
'* responses from the clients and will forward them
'* through the outside winsock.
'*************************
Dim strData As String
On Error GoTo sckClient_DataArrival_Err
'--- get the data from the tcp buffer
sckClient(Index).GetData strData
'--- check the first character to see if we need to reset the buffer
If Left$(strData, 1) = g_BOT Then
'--- check the length of the buffer to ensure
'--- that it is empty
If Len(m_strClientBuffer(Inde
m_strClientBuffer(Index) = vbNullString
End If
End If
'--- append the received data to the buffer
m_strClientBuffer(Index) = m_strClientBuffer(Index) & strData
'--- check the last character to see if
'--- the transmission has ended
If Right$(m_strClientBuffer(I
'--- reset the buffer and processing information
strData = m_strClientBuffer(Index)
m_strClientBuffer(Index) = vbNullString
If m_bolCaptureIncoming Then
m_bolClientReceived = True
Else
m_bolClientReceived = False
'--- send the data to the client
sckServer.SendData strData
End If
g_objProcesses("RS" & sckClient(Index).Tag).Proc
End If
sckClient_DataArrival_Exit
Exit Sub
sckClient_DataArrival_Err:
LogError Err.Number, Err.Source, Err.Description
Resume sckClient_DataArrival_Exit
End Sub
After posting the code, I noticed that it has been edited by someone. It should still work but some of the comments and dead code are not mine. Just a disclaimer. ;)
ASKER
Thanks for the answer. That is more or less what I was looking for.
Does "g_objProcesses...=false" fire an event or does it just run some code in a class module? (just curious)
Does "g_objProcesses...=false" fire an event or does it just run some code in a class module? (just curious)
I am spawning an executable that I use winsock to talk to. g_objProcesses is the collection that houses the information about those spawned apps. The Processing flag lets me know if the spawned app is currently processing. If it is processing then I have to send a cancel message before I send a close message. If you have any other ?'s let me know.
Bought This Question.
ASKER