Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using winsock without user intervention 1

Status
Not open for further replies.

nickt

Programmer
Mar 16, 2001
19
US
I've written a vb program that will connect to a server using sckTCPProtocol and send a string and obtain the response string. However it requires user input. Before I can think about creating a dll (for a web server) I need the exe to perform the whole process by itself.
Here's the form code:

--------------------------------------------------

Private Sub cmdDoAll_Click()

cmdConnect_Click
cmdSend_Click
cmdDisconnect_Click

End Sub

Private Sub cmdExit_Click()

Debug.Print "winsock state before end: " & Winsock1.State
End
End Sub

Private Sub cmdConnect_Click()
'connect to the server
Winsock1.Connect
Debug.Print "winsock state after connect: " & Winsock1.State

End Sub

Private Sub cmdDisconnect_Click()
'disconnect from server
Winsock1.Close
Debug.Print "winsock state after close: " & Winsock1.State

End Sub

Private Sub cmdSend_Click()
Dim myString As String
'define string to send
myString = Chr(1) & "adavids2 nthorp N N testingtcp" & Chr(10)
Debug.Print "myString = " & myString

'send the string to the server
Winsock1.SendData myString

End Sub

Sub winsock1_connect()
'this runs when the client has connected successfully to the server
Debug.Print "winsock state after winsock1_connect function: " & Winsock1.State

End Sub

Sub Winsock1_SendComplete()
'this runs when the string has been sent to the server
Debug.Print "winsock state after sendcomplete: " & Winsock1.State

End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
'this runs when a reply is received from the server

'sets in_data to equal the reply from the server
Winsock1.GetData in_data, vbString, bytesTotal
Debug.Print in_data

End Sub

---------------------------------------------------------
The program works fine and in_data gets populated with the reply from the server if the user clicks on cmdConnect and then clicks on cmdSend. (winsock1_connect runs in this scenario)
However, if user clicks on cmdDoAll the winsock state hangs at 6 (connecting) and the send fails. winsock1_connect doesn't run.
Can anyone help as to why the winsock state never reaches 7 (connected)?
also, just noticed that if I put a watch on winsock1.state then I get the same error for the method that does work.
congrats and thanks if you got this for!
 
I think you need to put a do while loop in your cmdConnect_Click so that it waits until the state reaches 7 connected before it carries on:

Winsock1.connect

Do while winsock1.state <> 7
doevents
' Might want a timer in here too so it times out after
' a predefined time
Loop

' Carry on

(I haven't tested the code, but I have done this in my own projects)
 
I would have to agree with madlarry to a certain extent. I've previously ran into the exact same error; however, I couldn't get the do while loop to work with the comparison that he is doing. Logically it makes sense, and that's what I tried initially, but I've found that it works better to set a boolean flag in the winsock1_connect and do the comparison on that. So, my suggestion would be.....

on error goto err:
Winsock1.connect

do
doevents
loop while NOT blnCONNECTED

' Continue on the way.

err:
'Give error Message.

Sub winsock1_connect()
blnCONNECTED = True
'this runs when the client has connected successfully
'to the server
Debug.Print &quot;winsock state after winsock1_connect
function: &quot; & Winsock1.State
End Sub

I'm not trying to pick a fight with madlarry, it's just that in my efforts, I've had problems implementing the .state <> 7 method. With my method, you now have another boolean to keep track of. This means before you connect you need to set it to false and after you disconnect you need to set it to false.


Hope this helps,
Eric
 
thanks for the help guys.
Eric, I tried something similar to your method earlier but the winsock1_connect sub was never running. madlarry's solution seems to be working ok so far. I'm just converting it into a dll at the moment and will return to this board if all goes t*ts up.
out of interest, the do loop looped over 3000 times until it connected and a further 120 times when I put a loop in to wait for the reply after the string had been sent.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top