Home > Software engineering >  VB winsock reconnection
VB winsock reconnection

Time:10-14

Winsock computer with a serial port server TCP connections and after successful connection (a computer program for the client side), disconnect after rewiring has been unable to connect, and winsock. 6, the state has always been don't know what the reason, give advice or comments please god

CodePudding user response:

Added: there are a bit odd, open the program code, run after the winsock can connect, at this time to stop to run the program (not shut down the program, you can also edit), and then run again, winsock. The state has been for 6, connected not; But if the close code, open to run the program again, but it can be connected, what reason is this??????

CodePudding user response:

For reference only
 Private Sub TimerTTSreconn_Timer () 
Dim after the As a Double
Dim TTSrpc As Long
On the Error Resume Next
TimerTTSreconn. Enabled=False
MainForm. Caption="(to connect to TTS) gateway control node"
The Debug. Print "to connect with TTS
"If tcpTTS. State & lt;> 0 Then
TcpTTS. Close
After=Now + 5 #/24 #/3600 #
Do
DoEvents
If tcpTTS. State=0 Then Exit the Do
If Now & gt; After Then
The Debug. Print "wait tcpTTS. State=0, 5 s, but=" + CStr (tcpTTS. State)
The Exit Do
End the If
Loop
End the If
TcpTTS. RemoteHost="127.0.0.1"
TcpTTS. RemotePort=2028
TcpTTS. Connect
After=Now + 5 #/24 #/3600 #
Do
DoEvents
If tcpTTS. State=7 Then Exit the Do
If Now & gt; After Then
The Debug. Print "Connect the TTS failure, try" + CStr (TTSrpc + 1)
The Exit Do
End the If
Loop
If tcpTTS. State & lt;> 7 Then
TTSrpc=TTSrpc + 1
If TTSrpc=20 Then
The Debug. Print "TTS connection failure"
End the If
The Else
TTSrpc=0
End the If
MainForm. Caption="" (running) gateway control node
End Sub

CodePudding user response:

What is check LocalPort value

CodePudding user response:

Winsock is a problem, port after disconnection, seems to be over a short period of time to be used again, I don't know what reason is this, however, it does

Every time you can reconnect to change a different port

CodePudding user response:

The reconnection, probably because you set LocalPort to a fixed value, the system will not immediately release after you disconnect the port, so I let you check the properties, if set to 0, can dynamically allocate local port, so that every time you connect local port is different, should will not appear on the reconnection is not problem

CodePudding user response:

http://blog.059505.com/winsock-sckconnecting/

Post: (solution)
 
As a Boolean Function ConnectSock ()
On Error GoTo ToExit 'open Error trap
DoEvents
With Winsocket
If the State & lt;> 0 Then. Close
If. The State=0 Then
The RemoteHost="127.0.0.1"
RemotePort=8885
. Connect
Me. SetfrmMsg "waiting connection... "
Do
DoEvents
Call DeLay (2)
If the State=6 Then
. Close
. Connect
End the If
If. State=7 Then Exit the Do
If the State & lt;> SckConnected Then Me. SetOutMsg "connection:" & amp; The State: the Exit Function
Loop
Me. SetfrmMsg
End the If
. SendData publishes the event SendMessageInfo $
End With
ConnectSock=True
ToExit:
If Err Then
Mee. SetOutMsg Err. The Description & amp; "ConnectSock (... "
End the If
End the Function

Sub DeLay (t As an Integer)
Dim As Long I
Dim tt As Long
Tt t * 100000=
For I=0 To tt
DoEvents
Next
End Sub
  • Related