WaitForSingleObject for Winsock control On Windows 98

gshriki
gshriki used Ask the Experts™
on
Hey,
I have the following program:
An application that connects to another computer (remote_host in the code) with the winsock control.
Upon pressing Command1, the time of the press is written to Text1, and then wait for data arrival on the winsock control with WaitForSingleObject (timeout of 5 sec). After the timeout occurs, it writes the current time to Text2 control.
On Windows 2k and Windows NT it works just fine. I get a 5 sec delay between the times. On Win98, The WaitForSingleObject does not wait at all, and the timeout (5 sec) arrives at the same second of pressing the button.

For reproducing the problem, create one form and one module. On the form, create 3 text boxes (Text1, Text2, Text3), button control (Command1) and a winsock control (WinSock1).

Module code:

Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Declare Function WaitForSingleObjectEx Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long, ByVal bAlertable As Long) As Long
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Form code:

Private Sub Command1_Click()
    Dim a As Integer
   
    a = 0
    Text1.Text = Time
    Me.Refresh
    While a < 50
        Call WaitForSingleObject(Me.Winsock1.SocketHandle, 100)
        a = a + 1
    Wend
    Text2.Text = Time
End Sub

Private Sub Form_Load()
    Text1.Text = 1
    Me.Winsock1.Close
    Me.Winsock1.Protocol = sckTCPProtocol
    Me.Winsock1.Connect "remote_host", 3333
End Sub

Private Sub Winsock1_Connect()
    Me.Text3.Text = "OK"
End Sub

Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)

    Me.Text3.Text = "NOT OK"
End Sub



Please help...
10x,
   Gilad.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Let me cut a little from MSDN docs:

The WaitForSingleObject function can wait for the following objects:

Change notification
Console input
Event
Job
Mutex
Process
Semaphore
Thread
Waitable timer


I don't see socket as a waitable object in the list? Why not use Timer control to get timeout?

I think the code is correct.
I tested with info u mentioned here.
1) It was getting an error "Connection is forcefully rejected" when it was trying to connect to the port & ip(or comp name) i changed a little for my setup ( only line i changed was Me.Winsock1.Connect "sanjay", 3333 ) from your code.

2) i created  a server which was waiting on 3333 port with this code

Private Sub Form_Load()
Winsock1.LocalPort = 3333
Winsock1.Listen
End Sub

then the connection was ok. and message was "OK"


Here is a modified version of the code...
This code times out in 5 seconds( thought not tested on other os but i m sure it works the same on all win os)
then times out or stops trying to connect as soon as it connects.


Dim bConnected As Boolean
Private Sub Command2_Click()
Dim i As Integer
bConnected = False
For i = 1 To 5
   Me.Winsock1.Close
   Me.Winsock1.Protocol = sckTCPProtocol
   Me.Winsock1.Connect "sanjay", 3333
   Call MySleep(1) 'wait for 1 sec
   Text4.Text = Time
   If bConnected Then
      Exit For
   End If
Next i
If bConnected = False Then
   MsgBox "Timed out"
Else
   MsgBox "connected"
End If
End Sub

Private Sub Winsock1_Connect()
 
   bConnected = True
End Sub

Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
   bConnected = False
   Me.Text3.Text = "NOT yet"
End Sub
Sub MySleep(SecondsToWait As Integer)
   
    'Function to wait for specified seconds
    Dim StartTimer
    StartTimer = Timer
    Do While Timer < StartTimer + SecondsToWait
        DoEvents
    Loop
End Sub

Hope that helps.
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:
Thank you all for your comments, BUT:

Kimpan: it DOES work with winsock control... It works for me in WinNT, and Win2k OSs.

sanjaykattimani: I do not want workaround... I already manage to do it with the Sleep API (that is better then working with the timer). I want to work with the WaitForSingleObject API that will reduce the waiting time when the connection gets data...

10x again.

Anyone else?




Author of the Year 2009

Commented:
Hi gshriki,
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:

    Refund points and save as a 0-pt PAQ.

gshriki, Please DO NOT accept THIS comment as an answer.
EXPERTS: Post a comment if you are certain that an expert deserves credit.  Explain why.
==========
DanRollins -- EE database cleanup volunteer
Question placed in PAQ

Computer101
E-E Admin

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