Winsock question 
Author Message
 Winsock question

Here's the situation:

I'm trying to create a client-server application in which the client makes a
request to the server, the server produces a result and the server sends the
result to the client.

Here's the problem:

The client is making the request from within a function.  Once the client
makes the request, I do not want the client to proceed with any other code
until it either gets a response from the server or a timeout period is
reached.

I've tried to accomplish this in the following manner:

**** start of code ****
Public Function RollDice(ByVal items As Integer) As String

    Dim msg As String
    Dim d As Double
    Dim id As String
    d = Now
    id = CStr(d)

    msg = "/sessionID:" & d & "/items:" & items & "/request:" & server_die &
"/"

    SetTimerCounter 30
    tmrTimer.Enabled = True

    sckClient.SendData msg & vbCrLf

    'mdataarray() is a user-defined type that is available to the entire
module
    'and data is placed into the array by the sckClient.DataArrival event
    While (mdataarray(0).id <> id) And (mtimercounter > 0)
        DoEvents
    Wend
    If mtimercounter = 0 Then
        MsgBox "The server did not respond!"
    Else
        RollDice = mdataarray(0).s
    End If

End Function
*** end of code ****

Essentially, if I call the function RollDice like this:

    sret = RollDice(5)

then the client will make a request to the server, the server will generate
the die roll and then return the result of the roll back to the client.  If
the client doesn't hear back from the server then sret will equal "".

When I run the program, it works for a time.  However, eventually I will get
an "Automation Error" and the entire program will closes.

If I remove the DoEvents line from the While loop then my program hangs and
the CPU utilization spikes to 100%.

Question:  When using the Winsock control, how do I get the client to wait
for a response from the server without a) causing the client to hang or b)
causing the automation error?



Sun, 11 May 2003 03:00:00 GMT  
 Winsock question
What I would do is once you send the data to the server start up a timer
with the timeout value and then call the DoEvents right after. Like this:

Private Sub MyFunction()
'...Do Stuff
wsWinsock.SendData("Blah")
tmrTimer.Enabled = True
DoEvents
End Sub

Then, if the timer fires, you know you timed out:
Private Sub tmrTimer_Timer
    MsgBox("Timed Out!!!")

    'you can try again or not, up to you
    'If you don't want to try again, then stop the timer:
    tmrTimer.Enabled = False
End Sub

If you get a response from the server, then everything is cool
Private Sub wsWinsock_DataArrival
    'Stop the timer
    tmrTimer.Enabled = False

    'Get the data and do whatever with it
    wsWinsock.GetData m_Data

    'Continue doing your work...
End Sub

Hope this helps...
PCG


Quote:
> Here's the situation:

> I'm trying to create a client-server application in which the client makes
a
> request to the server, the server produces a result and the server sends
the
> result to the client.

> Here's the problem:

> The client is making the request from within a function.  Once the client
> makes the request, I do not want the client to proceed with any other code
> until it either gets a response from the server or a timeout period is
> reached.

> I've tried to accomplish this in the following manner:

> **** start of code ****
> Public Function RollDice(ByVal items As Integer) As String

>     Dim msg As String
>     Dim d As Double
>     Dim id As String
>     d = Now
>     id = CStr(d)

>     msg = "/sessionID:" & d & "/items:" & items & "/request:" & server_die
&
> "/"

>     SetTimerCounter 30
>     tmrTimer.Enabled = True

>     sckClient.SendData msg & vbCrLf

>     'mdataarray() is a user-defined type that is available to the entire
> module
>     'and data is placed into the array by the sckClient.DataArrival event
>     While (mdataarray(0).id <> id) And (mtimercounter > 0)
>         DoEvents
>     Wend
>     If mtimercounter = 0 Then
>         MsgBox "The server did not respond!"
>     Else
>         RollDice = mdataarray(0).s
>     End If

> End Function
> *** end of code ****

> Essentially, if I call the function RollDice like this:

>     sret = RollDice(5)

> then the client will make a request to the server, the server will
generate
> the die roll and then return the result of the roll back to the client.
If
> the client doesn't hear back from the server then sret will equal "".

> When I run the program, it works for a time.  However, eventually I will
get
> an "Automation Error" and the entire program will closes.

> If I remove the DoEvents line from the While loop then my program hangs
and
> the CPU utilization spikes to 100%.

> Question:  When using the Winsock control, how do I get the client to wait
> for a response from the server without a) causing the client to hang or b)
> causing the automation error?



Fri, 16 May 2003 03:00:00 GMT  
 Winsock question
I called Microsoft tech support and eventually discovered the following:

There is a known bug (couldn't find an article in technet so I don't have a
"q" number) in which automation errors will occur if you compile the program
using the "Compile To Native Code" option.  I switched to "Compile To
P-Code" and all of my problems disappeared.



Quote:
> What I would do is once you send the data to the server start up a timer
> with the timeout value and then call the DoEvents right after. Like this:

> Private Sub MyFunction()
> '...Do Stuff
> wsWinsock.SendData("Blah")
> tmrTimer.Enabled = True
> DoEvents
> End Sub

> Then, if the timer fires, you know you timed out:
> Private Sub tmrTimer_Timer
>     MsgBox("Timed Out!!!")

>     'you can try again or not, up to you
>     'If you don't want to try again, then stop the timer:
>     tmrTimer.Enabled = False
> End Sub

> If you get a response from the server, then everything is cool
> Private Sub wsWinsock_DataArrival
>     'Stop the timer
>     tmrTimer.Enabled = False

>     'Get the data and do whatever with it
>     wsWinsock.GetData m_Data

>     'Continue doing your work...
> End Sub

> Hope this helps...
> PCG



> > Here's the situation:

> > I'm trying to create a client-server application in which the client
makes
> a
> > request to the server, the server produces a result and the server sends
> the
> > result to the client.

> > Here's the problem:

> > The client is making the request from within a function.  Once the
client
> > makes the request, I do not want the client to proceed with any other
code
> > until it either gets a response from the server or a timeout period is
> > reached.

> > I've tried to accomplish this in the following manner:

> > **** start of code ****
> > Public Function RollDice(ByVal items As Integer) As String

> >     Dim msg As String
> >     Dim d As Double
> >     Dim id As String
> >     d = Now
> >     id = CStr(d)

> >     msg = "/sessionID:" & d & "/items:" & items & "/request:" &
server_die
> &
> > "/"

> >     SetTimerCounter 30
> >     tmrTimer.Enabled = True

> >     sckClient.SendData msg & vbCrLf

> >     'mdataarray() is a user-defined type that is available to the entire
> > module
> >     'and data is placed into the array by the sckClient.DataArrival
event
> >     While (mdataarray(0).id <> id) And (mtimercounter > 0)
> >         DoEvents
> >     Wend
> >     If mtimercounter = 0 Then
> >         MsgBox "The server did not respond!"
> >     Else
> >         RollDice = mdataarray(0).s
> >     End If

> > End Function
> > *** end of code ****

> > Essentially, if I call the function RollDice like this:

> >     sret = RollDice(5)

> > then the client will make a request to the server, the server will
> generate
> > the die roll and then return the result of the roll back to the client.
> If
> > the client doesn't hear back from the server then sret will equal "".

> > When I run the program, it works for a time.  However, eventually I will
> get
> > an "Automation Error" and the entire program will closes.

> > If I remove the DoEvents line from the While loop then my program hangs
> and
> > the CPU utilization spikes to 100%.

> > Question:  When using the Winsock control, how do I get the client to
wait
> > for a response from the server without a) causing the client to hang or
b)
> > causing the automation error?



Fri, 16 May 2003 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. A Winsock Question and a Audio CD Question

2. Winsock question

3. Winsock question

4. Winsock question

5. Winsock question.

6. Winsock question

7. WINSOCK question

8. Another VB/Winsock Question

9. Winsock question.

10. Another winsock question

11. Winsock question

12. WINSOCK Question

 

 
Powered by phpBB® Forum Software