hey
i posted this tutorial because i have asked lots of questions...
and i find help only in this forum
thx everybody
this Tutorial teaches you the basics of reverse connecting.
ok this is one of my first VB tutorials. so please if there is any thing needing improvment just tell me. now then the point of reverse connecting is so that the server program can bypass routers or other types of security programs. now in my tutorial i will be using winsock because it is the most used socket tool in VB but you can apply this to any other control you use. all that we have to do is try to connect to an ip, if we fail we can try again. the only thing is if we keep trying over and over we can start wasting system resources so we want our app to pause in between trys. and the last thing is we need to make sure if the connection times out we restart anyway.
so lets start off by adding winsock to our form. then we will add a timer and set the interval to 1000, which is the same as one second. we will have our app then attempt to connet every 10 seconds so after adding your timer lets just double click it and then set up some variables we will use like so:
______________
______________
______
Dim PauseTime As Long 'this will be our ten second delay
Private Sub Form_Load()
PauseTime = 10
End Sub
______________
______________
______
the next thing we need to do is make it so the timer will run our code every ten seconds.
______________
______________
______
Dim PauseTime As Long 'this will be our ten second delay
Private Sub Form_Load()
PauseTime = 10
End Sub
Private Sub Timer1_Timer()
PauseTime = PauseTime - 1
If PauseTime <= 0 Then
PauseTime = 10 'reset the timer
'code here
End If
End Sub
______________
______________
______
now then next we will attempt to connect with winsock, and if the connection times out or we get an error then we will just wait another ten secs and try again...
______________
______________
______
Dim PauseTime As Long 'this will be our ten second delay
Private Sub Form_Load()
PauseTime = 10
End Sub
Private Sub Timer1_Timer()
PauseTime = PauseTime - 1
If PauseTime <= 0 Then
PauseTime = 10 'reset the timer
Timer1.Enabled = False
Winsock1.Conne
ct "127.0.0.1", 776 'this is were we reverse connect to
Dim Chk: Chk = Timer 'our timer to make sure the connection dosent time out
Do While Winsock1.State <> sckConnected Or Winsock1.State <> sckError Or Timer < Chk + 10
DoEvents: Loop 'try to connect and if we get an error move on.
If Winsock1.State <> sckConnected Then Winsock1.Close: DoEvents: Timer1.Enabled = True
End If
End Sub
______________
______________
______
so thats your basic code. every 10 seconds it attemptes to connect, and if it cant it waits another ten seconds and trys again. if it does connect the timer stops so it dosent get any more errors. it also needs to handle any errors it gets and be able to detect if its connected, so all we have to do is fill in another little bit of code like so:
______________
______________
______
Private Sub Winsock1_Conne
ct()
'place code to execute once connected
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)
On Error Resume Next: Winsock1.Close: DoEvents 'close the socket
If Timer1.Enabled = False Then Timer1.Enabled = True 'make sure it keeps rying to connect
End Sub
______________
______________
______
there you go, thats all you should need to get a good reverse connect type application. plus this code has a small bonus ability. most tutorials ask for two timers, the second one checks if the connection was closed to start trying to reverse connect again, but because of the winsock error code that extra hassle is not needed

. the code could be optimised slightly but in this form it is easy to change the PauseTime settings so that the delay is either longer or shorter depending on your apps needs. also using a timer as to a loop allows you to enable or disable reverse connection quite easily. i hope these examples have helped you.