Posto tutto il codice in modo tale sia tutto più chiaro:
CLIENT
codice:
Private percorso As String
Private nome As String
Private dimensione As Long
Private handle As Integer
Private a As Boolean
Private dati(4000) As Byte
Private Sub Command1_Click()
w.Close
w.Connect Text4.Text, 8000
w2.Close
w2.Connect Text4.Text, 8001
End Sub
Private Sub Command2_Click()
cd.ShowOpen
percorso = cd.FileName
nome = cd.FileTitle
dimensione = FileLen(percorso)
w.SendData "///" & nome & "///" & dimensione
End Sub
Private Sub w_Connect()
Form1.Caption = "Connesso."
End Sub
Private Sub w_DataArrival(ByVal bytesTotal As Long)
Dim data As String
w.GetData data
If data = "ack" Then
handle = FreeFile
a = True
Open percorso For Binary As handle
While a = True
Get handle, , dati
DoEvents
w2.SendData dati
Wend
End If
If data = "fine" Then
Close handle
a = False
End If
End Sub
SERVER:
codice:
Private nome() As String
Private dimensione As Long
Private handle As Integer
Private Sub Form_Load()
w.Close
w.LocalPort = 8000
w.Listen
w2.Close
w2.LocalPort = 8001
w2.Listen
End Sub
Private Sub w_ConnectionRequest(ByVal requestID As Long)
w.Close
w.Accept requestID
End Sub
Private Sub w_DataArrival(ByVal bytesTotal As Long)
Dim data As String
w.GetData data
If Left(data, 3) = "///" Then
nome = Split(data, "///")
dimensione = nome(2)
DoEvents
handle = FreeFile
Open "C:\" & nome(1) For Binary As handle
DoEvents
w.SendData "ack"
End If
End Sub
Private Sub w2_ConnectionRequest(ByVal requestID As Long)
w2.Close
w2.Accept requestID
End Sub
Private Sub w2_DataArrival(ByVal bytesTotal As Long)
On Error GoTo fine
Dim data() As Byte
w2.GetData data
Put handle, , data
List1.AddItem LOF(handle)
If UBound(data) + LOF(handle) >= dimensione Then
Close handle
w.SendData "fine"
End If
fine:
If Err.Number = 52 Then
End If
End Sub