http://forums.chasenet.org/index.php?showtopic=4150&mode=threaded&pid=67424

Server


Option Explicit

Private Sub Socket1_OnClose()

socket1.Close

End Sub

'pas de commentaire ici c'est assez simple pour s'en passer!
Private Sub Form_Unload(Cancel As Integer)

socket1.Close
socket4.Close

End Sub

Private Sub Socket1_DataArrival(ByVal bytesTotal As Long)
Dim StrData As String

socket1.GetData StrData
If StrData = "connect" Then
   socket4.Close
   socket4.Connect "127.0.0.1", 56
End If

End Sub


Private Sub Socket4_OnClose()

socket4.Close

End Sub


Private Sub socket1_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)
socket1.Close
End Sub

Private Sub socket4_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)
socket4.Close
End Sub

Private Sub Timer1_Timer()

If socket1.State <> sckConnected Then
socket1.Close
socket1.Connect "127.0.0.1", 1234
End If

End Sub


















CODE

'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 mme 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