Multi reverse connection

Description

Ce code permet de vous connecter en mode reverse. Plus précisement, au lieu d'avoir un client se connectant a un server, c'est l'inverse. Il montre aussi comment utiliser un array de socket pour pouvoir accepter plusieurs connections.

Source / Exemple :


'Sin Notidication 23.06.2005 by TheLord

'Greetz-
'UndergroundKonnekt

'Please visit http://undergroundkonnekt.net

'Note: dont make yourself a ripper, if you find this source interesting then
'Give the credit to its author.

Option Explicit

Dim List_nb As Long
Dim Data As String
Dim z As Long

Private Sub Command1_Click()
'Stop listening

Socket2(0).Close
Socket3.Close

If Lv.ListItems.Count <> 0 Then
    Lv.ListItems.Clear 'clear de la listview
End If

Statut.Caption = "Statut: Idle..."
sinconn.Caption = "Connection"

End Sub

Private Sub Command2_Click()

Form2.Show

End Sub

Private Sub Form_Unload(Cancel As Integer)
'On ferme tous les sockets
Socket2(0).Close

For z = 1 To Lv.ListItems.Count
        Unload Socket2(Lv.SelectedItem.SubItems(2))
Next z
If Socket3.State <> sckClosed Then
    Socket3.Close
End If

End
End Sub

Private Sub Listen_Click()
'Listening
    Socket2(0).Close
    Socket2(0).LocalPort = 1234
    Socket2(0).Listen
        If Socket2(0).State = sckListening Then
    Statut.Caption = "statut: listening on port 1234"
        End If

End Sub

Private Sub Form_Load()
'Definition des colonnes du listview
With Lv
    .View = lvwReport
        Call .ColumnHeaders.Add(, , "RemoteHost")
        Call .ColumnHeaders.Add(, , "Remoteport")
        Call .ColumnHeaders.Add(, , "Sck Index")
End With

End Sub

Private Sub Lv_DblClick()

sinconn_Click

End Sub

Private Sub sinconn_Click()
'Reverse connection
Select Case sinconn.Caption
    Case "Connection"
    If Socket3.State = sckConnected Then
        Exit Sub
    Else
        'On envoie un packet pour que le server se connecte ("connect")
        If Lv.ListItems.Count <> 0 Then
            If Socket2(Lv.SelectedItem.SubItems(2)).State = sckConnected Then
                Socket3.Close
                Socket3.LocalPort = 56
                Socket3.Listen
                Socket2(Lv.SelectedItem.SubItems(2)).SendData "connect"
            End If
        End If
    End If
    'Deconnection
    Case "Disconnection"
        Socket3.Close
        If Socket3.State <> sckConnected Then
            sinconn.Caption = "Connection"
            If Socket2(0).State = sckListening Then
                Statut.Caption = "Statut: listening on port 1234"
            Else
                Statut.Caption = "Statut: Idle..."
            End If
        End If
End Select

End Sub

Private Sub Socket2_Close(Index As Integer)

Socket2(Index).Close 'On ferme le socket concerné par l'event

'Boucle for pour passer en revue toute les ip de la listview
'puis remove du socket concerné par le close
For z = 1 To Lv.ListItems.Count
    If Lv.ListItems.Item(z).Text = Socket2(Index).RemoteHostIP Then
        Lv.ListItems.Remove Lv.ListItems(z).Index
Exit For
    End If
Next z

End Sub

Private Sub Socket2_ConnectionRequest(Index As Integer, ByVal requestId As Long)
Dim objlist As ListItem

'l'array du socket a connecté est la valeur de List_nb
List_nb = List_nb + 1 'on incremente le socket de +1 a chaque connection

Load Socket2(List_nb) 'load du socket

Socket2(List_nb).Close

Socket2(List_nb).Accept requestId

'Gestion de la listview, add des infos du socket (ip, port, index du socket)
For z = 1 To Lv.ListItems.Count
      If (Lv.ListItems(z).Text = CStr(Index)) Then
         Set objlist = Lv.ListItems(z)
Exit For
      End If
Next z
'on definit l'objet (socket2(List_nb)) concerné par la connection
If (objlist Is Nothing) Then
    Set objlist = Lv.ListItems.Add(, , Socket2(List_nb).RemoteHostIP)
End If ' <= l'objet = ip du socket

With objlist 'repartition des infos dans les colonnes de la listview
    .SubItems(1) = Socket2(List_nb).RemotePort
    .SubItems(2) = Socket2(List_nb).Index
End With

   Set objlist = Nothing
   
End Sub

Private Sub Socket2_Error(Index As Integer, 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)
'la même que l'Event Socket2_Close
Socket2(Index).Close

    For z = 1 To Lv.ListItems.Count
        If Lv.ListItems.Item(z).SubItems(2) = Socket2(Index).RemotePort Then
            Lv.ListItems.Remove Lv.ListItems(z).Index
    Exit For
        End If
    Next z
    
If Index <> 0 Then
    Socket2(Index).Close
End If

Statut.Caption = "Statut: Idle..."

End Sub

Private Sub Socket3_Close()

Socket3.Close
If Socket2(0).State = sckListening Then
    Statut.Caption = "Statut: listening on port 1234"
Else
    Statut.Caption = "Statut: Idle..."
End If

sinconn.Caption = "Connection"

End Sub

Private Sub Socket3_ConnectionRequest(ByVal requestId As Long)

Socket3.Close

Socket3.Accept requestId

    If Socket3.State = sckConnected Then
        Statut.Caption = "Statut: Connected to " & Form2.socket4.RemoteHost
    End If
If Socket3.State = sckConnected Then
    sinconn.Caption = "Disconnection"
End If
    
End Sub

Private Sub Socket3_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)

Socket3.Close

If Socket2(0).State = sckListening Then
    Statut.Caption = "Statut: listening on port 1234"
Else
    Statut.Caption = "Statut: Idle..."
End If

sinconn.Caption = "Connection"

End Sub

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.