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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get winsock true status 1

Status
Not open for further replies.

G7RAU

Programmer
Aug 29, 2003
6
Hi
I use winsock for a telnet link and I find that the only way to find out if the connection is up is to send a vbcrlf to the destination. I would have thought that winsock.status would have checked open conn but it does not. Is there an easy way to check the connection is open without sending a crlf?

 
Check the state property .......

if Winsock1.State = sckconnected then "your code here"
 
Hi vbrocks, I have tried using winsock.state before but it brings up a connected status even if the telnet link was lost (easy to test, just pulled the ethernet cable out the back of the PC).
 
There could be another port open, try this it should show you......

For i = 1 To Winsock1.Count - 1
If Winsock1(i).State = sckConnected Then
debug.print Winsock1(i)
End If
Next
 
The problem may be something along these lines. If you have a piece of code that looks something like this:

Winsock1.Connect strIP, intPort
if Winsock1.State = sckConnected Then
<i>YOUR CODE</i>
End If

Then the winsock does not have enough time to connect. What you need to do is have something more along these lines:

Dim strStartTime As String
Winsock1.Connect strIP, intPort
strStartTime = Time
Do While Winsock1.State <> sckConnected Or Second(CDate(Time - CDate(strStartTime))) > 5
doEvents()
Loop
if Winsock1.State <> sckConnected Then MsgBox(&quot;Could not connect&quot;)
 
Hi ICISYD ok, yep, that works but if the connection is dropped after say 5mins then the winsock state still comes back as connected, untill I send data through the telnet connection, then the state reports correctly that the connection is lost. A real pain having to send data to get state.
 
If the connection is dropped the winsock_close event should be called. You could use that to set a global variable.
 
Hi agn ICISYD
The problem is an odd one. The winsock app works ok but if the remote side drops (Via the internet in this case) the state recalled is wrong until U try to pass data to the remote side (I chk state app side with timer every 10secs). I noticed that telnet servers also seem to have the same problem, not recognising that a client has dropped off (not disconnected the proper way using exit /q etc). Maybe this is one of them pain in the b*** things we have to live with?? :)
 
The winsock control actually has an event that is called when the connection is dropped. To use it you would enter something along the lines of:

Private Sub Winsock1_Close()
MsgBox &quot;The connection has been dropped&quot;
End Sub

This should display the message box when Winsock1 is closing.
 
There are two sides to every coin, just like there are two sides to winsock: UDP and TCP/IP. Perhaps winsock considers your telnet to be like UDP, which does not require a formal &quot;connection&quot;. Can you have a telnet connection using the TCP/IP protocol? Anyways, here is a substitute you can try.....
 
Try this - connect to the server, unplug the network cable, and then do a netstat -an from the command line and see if you have a TIME_WAIT status on the connection to the telnet server. If the connection is in TIME_WAIT status, the socket will still be open even though there is no physical media to transmit data. This could explain your problem.

 
Hi ngagne, spot on, looks like I am doing the only thing possible.
All, thanks for the ideas / suggestions.

 

The issue that you experienced has been plaguing me for over a year, good work on the crlf concept. I have a question though: I am trying to receive information from CNC controls (Industrial Machines). These machines have serial ports, I convert the serial to Ethernet, which in turn interfaces with my Winsock app. My problem is that frequently part of the serial transmission is cut off. Usually between 10-150 characters of the beginning of the file.(The file is Ascii). Have you experienced anything like this using Winsock? Thanks. Cartoondog
 
CartoonDog,

You should really start a new thread with your question instead of tagging it onto this very old one.

Robert
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top