Link to home
Create AccountLog in
Avatar of almiceli
almiceli

asked on

Winsock control does not work consistently

I am updating a program that originally read barcode reader data via serial port to communicate via ethernet using the MS Winsock control in VB6.

The problem is that the Winsock control works sometimes, then it doesn't. Tracing through the code it apears that while it is not working, it is stuck in the "connecting" state. When it does work, the connection state is "connected"

Coding appears to be very simple and straight forward.

Private Sub Form_Load()

Dim RemoteIPAddress As Variant
Dim RemotePort As Variant
   
If Not Winsock1.State = sckClosed Then
    Winsock1.Close
End If

RemoteIPAddress = "192.168.15.200"
RemotePort = "3001"
Winsock1.RemoteHost = RemoteIPAddress
Winsock1.RemotePort = RemotePort
Winsock1.Connect

End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Dim strData As String
    Winsock1.GetData strData
    Text1.Text = Text1.Text & strData
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Winsock1.Close
End Sub


When I first run this program, it displays the data coming from the reader. If I open and close the
program, it no longer gets the data. Connection state remains in "connecting" If I wait about 5 minutes with the program closed, then reopen it, it will once again communicate. It seams like the PC is staying connected to the port and does not trigger the data_arrival event. Then it must time out and release it, but only if I'm disconnected (program not running) I can leave the program running all day when it does not communicate and it never begins to communicate. I'm running Vista Business if that make a difference.1

Can someone please help? I need to have this project done soon.

Thank you in advance for your help
Avatar of CSecurity
CSecurity
Flag of Iran, Islamic Republic of image

i had this problem in one of my programs and i solved it:
you should add in winsock's error sub, these codes:

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)

winsock1.close
winsock1.connect

End Sub

indeed in every error you should close the connection and connect again
Regards
ASKER CERTIFIED SOLUTION
Avatar of almiceli
almiceli

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Yes, it's ok.

I already told you that.

I told you to put Winsock1.close in error and that will solve your problem putting it into winsock1_close will also work. I told you that before.
Avatar of almiceli
almiceli

ASKER

I'm sorry but I read your solution again and no-where does it say to place the winsock1.close in the Winsock1_close event. You said to place the close in the error event.

I really do appreciate your help Thank you again.

Ok, that's good. Dear admin, please close this question with full refund. Thanks.
Avatar of modus_operandi
Closed, 500 points refunded.
modus_operandi
EE Moderator
It's really strange for me how people become ungrateful/unthankful.