A question about multi-client server

Jerryleo
Jerryleo used Ask the Experts™
on
I am a newbie in winsock programing. I got below code from internet

'--BAS Module Code--'
Public MaxCN as Long
Public cn As ADODB.Connection

Public Function DBConnect() As Boolean

    On Error GoTo OpenErr

    Dim MSDatabase

    Set cn = New ADODB.Connection

    MSDatabase = App.Path & "\" & "Database.mdb"
    cn.CursorLocation = adUseClient
    cn.Provider = "Microsoft.Jet.OLEDB.4.0"
    cn.Open MSDatabase, Admin
    DBConnect = True
   
    Exit Function

OpenErr:

    MsgBox "Error Opening " & MSDatabase & vbNewLine & Err.Description, vbCritical, "Open Database Error"
    DBConnect = False

End Function

Public Sub ParseRecv(RX As Variant, sckIndex as integer)
    ... ...
    select case mid(RX,6)
         ... ...    
         case "SQLCMD"   'for example "SQLCMD:Select * from joblist"
            call SendRS(mid(RX,8),sckindex)
        case "OPENDB"
        ... ...
    end select
    ... ...
End Sub

Public Sub SendRS(SQL as string, sckindex as integer)

    Dim Rs As ADODB.Recordset
    Dim Records As String
    Dim i As Integer, j as integer

    Set Rs = New ADODB.Recordset
    Rs.Open SQL, cn, adOpenKeyset, adLockReadOnly

    For i = 1 To Rs.RecordCount
         Records="RS"
         for j=1 to Rs.Fields.Count
            Records=Records & "," & cstr(Rs.Fields(j).Value )
         next j
        Rs.MoveNext
        frmServer.sckServer(sckindex).SendData Records
    Next i

    Rs.Close
    Set Rs = Nothing

End Sub
'--end block--'

'--Form Code--'
Private Sub sckServer_ConnectionRequest(Index As Integer, ByVal requestID As Long)

    MaxCN = MaxCN + 1                 'Increases the user count
    Load sckServer(MaxCN)             'Loads up the new winsock control
    sckServer(MaxCN).LocalPort = 0    'sets a random port
    sckServer(MaxCN).Accept requestID 'Accept connection

End Sub

Private Sub sckServer_DataArrival(Index As Integer, ByVal bytesTotal As Long)

    Dim Recieve As String
   
    If Len(Recieve) = Null Then
        Exit Sub
    Else
        sckServer(Index).GetData Recieve
        Call ParseRecv(Recieve, index) 'Analyze data
    End If
   
End Sub

'--end block--'

I don't understand some places very much. If two or more clients send diffrent requests at the same time, or when it's sending data to a client, another client sends a new request, how does it work? Does it can send diffrent data to diffretn client at the same time? Who can do some explanation for me?

Thanks a lot!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Let's see...

You create a winsock control with index 0 that works as a listener.

Every time a client requests a connection the event sckServer_ConnectionRequest creates a new instance of the winsock control itself and accepts the connection.

So basically winsock(0) receives the requests, creates winsock(1) and transfers the activity to winsock(1) that takes care of chatting with the client.

If another client asks for services at the same time (well slightly after :-)) winsock(0) creates another instance winsock(2) that talks with the second client.

Winsock(0) - Listener
Winsock(1) - First client
Winsock(2) - Second client
.
.
.
Winsock(n) - Client n

Hope it's clear...

Author

Commented:
cparlanti,

Thank you for your reply.

I know it is is a single process program. But how it dealt with incident of erupting simultaneously?

Suppose that two clients, C1 and C2, have already made a connection with server succeefuly. How about under following two a kind of situationses

1. C1 and C2 send different request at the same time, for example, C1 send a request, SQLCMD:Select * from joblist,  to inquire all the job lists; C2 send a request, SQLCMD:Select * from joblist where completed=1, to inquire all the completed jobs, at the same time. How does it deal with?

2. When it's sending data to C1, C2 sends a new request, for example, SQLCMD:Select * from joblist where jobID between 1000 and 2000. How does it deal with?
OK,

After the initial connection C1 and C2 are using different TCP/IP ports to communicate with their clients:

sckServer(MaxCN).LocalPort = 0    'sets a random port

In addition since the winsock oject is designed in a special way the two requests you're talking about are not going to be received TOGETHER. One is gonna be queued after the other.

So C1 send a request , SQLCMD:Select * from joblist,  to inquire all the job lists. Your program starts the task. Immediately after C2 send a request, SQLCMD:Select * from joblist where completed=1, to inquire all the completed jobs. Your program suspends the previous request and takes care of C2. Once C2 is finished, your program completes C2.

When designing an application like a server it's up to you to put in place mechanisms to make the server asyncronous.

An example could be to code in the sckServer_DataArrival event a function to simply queue up the client requests in an array. Then have some timed function routinely checking the queue and, if there is job to do, take care of performing the job, in the received order, and answering clients.

In addition, especially with data base applications, you may want to use the ADO techniques adAsyncExecute and adAsyncFetch so that you can submit a query, let the server handle it while you do something else, check every once in a while if the resulting recordset is ready for you.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
hello cparlanti,

I am not still very clear in some places. Could explain again ?

>So C1 send a request, Your program starts the task.
>Immediately after C2 send a request, Your program suspends the previous request and takes care of C2.
>Once C2 is finished, your program completes C2.

I understand like this, OK?  When my program is sending data to C1, C2 sends a request, C1 will be suspended, and my program takes care of C2, once C2 is finished, my program completes also. C1 just can gets partial data.


In other words, my program can only deal with one request once. And I have to queue up the client requests in an array in the sckServer_DataArrival event, deal with them one by one.  For example,

Private Sub sckServer_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    ... ...
       sckServer(Index).GetData Recieve
       If waiting Then
            Enqueue(Recieve, index)
       Else
           Call ParseRecv(Recieve, Index) 'Analyze data
           waiting = True
       End If
   
End Sub

Private Sub Timer1_Timer()
    If IsEmpty(Reuqest_Que) = False And Waiting = False Then
        DeQueue
    End If
End Sub


If I want send different data to different clients at the same time, how should I do?
First of all let me apologize for a typo in my answer. It should be:

So C1 send a request , SQLCMD:Select * from joblist,  to inquire all the job lists. Your program starts the task. Immediately after C2 send a request, SQLCMD:Select * from joblist where completed=1, to inquire all the completed jobs. Your program suspends the previous request and takes care of C2. Once C2 is finished, your program completes C1.

The last sentence was wrong!!!!!

If you want to answer to more than one client at the same time you're going to need to implement Multi-Threading. Here's a link to an sample that will help you:

http://www.freevbcode.com/ShowCode.Asp?ID=1287
Author of the Year 2009

Commented:
Hi Jerryleo,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept cparlanti's comment(s) as an answer.

Jerryleo, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept THIS comment as an answer.
==========
DanRollins -- EE database cleanup volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial