Ho creato una connessione client-server in LAN. Vorrei creare un trasferimento file reciproco (server invia a client e client invia a server). Il trasferimento deve essere semplice:
1- con un openfiledialog si sceglie il file
2- si attende conferma dall'altro PC
3- invia il file in un percorso prestabilito (esempio: desktop)
Naturalmente so l'IP dell'altro PC e ho aperto (anche se non era necessario) una porta del router per velocizzare il tutto... ecco il codice usato per la creazione del programma:
CLIENT
Come è possibile vedere ho realizzato una chat per ora.codice:Imports System.Net.Sockets Imports System.Text.UTF8Encoding Public Class Form1 Dim ascolta As TcpListener Dim cliente As TcpClient Dim flusso As NetworkStream Dim x As Integer Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing Try Dim temporaneo() As Byte = UTF8.GetBytes("chiusaconnessione123454321") flusso.Write(temporaneo, 0, temporaneo.Length) Catch ex As Exception End End Try End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ascolta = New TcpListener(8888) ascolta.Start() Timer2.Start() End Sub Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick If ascolta.Pending() Then Timer2.Stop() cliente = ascolta.AcceptTcpClient ascolta.Stop() flusso = cliente.GetStream() Timer1.Start() Label1.Text = "NICCO è connesso alla chat." My.Computer.Audio.Play(My.Resources.Click, AudioPlayMode.Background) Button1.Enabled = True End If End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick If cliente.Available > 0 Then Dim temporaneo(cliente.Available - 1) As Byte flusso.Read(temporaneo, 0, temporaneo.Length) Dim testo As String = UTF8.GetString(temporaneo) If testo = "chiusaconnessione123454321" Then MsgBox("NICCO si è disconnesso dalla chat.", MsgBoxStyle.Information, "Attenzione") ascolta.Stop() ascolta = New TcpListener(8888) ascolta.Start() Timer2.Start() Label1.Text = "In attesa che NICCO si connetta..." Button1.Enabled = False Else RichTextBox1.AppendText(vbCrLf & "- NICCO: " & testo) If Me.TopMost = False Then My.Computer.Audio.Play(My.Resources.Tick, AudioPlayMode.Background) End If End If End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If TextBox1.Text = "" Or TextBox1.Text = " " Or TextBox1.Text = " " Then pausa.Start() Else invia() End If End Sub Private Sub invia() pausa.Start() Button1.Enabled = False Dim temporaneo() As Byte = UTF8.GetBytes(TextBox1.Text) flusso.Write(temporaneo, 0, temporaneo.Length) RichTextBox1.AppendText(vbCrLf & "- LOLLO: " & TextBox1.Text) If Me.TopMost = False Then My.Computer.Audio.Play(My.Resources.Tick, AudioPlayMode.Background) End If TextBox1.Text = "" End Sub Private Sub pausa_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pausa.Tick x = x + 1 If x = 1 Then pausa.Stop() Button1.Enabled = True x = 0 End If End Sub Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown If e.KeyCode = Keys.Enter Then If Button1.Enabled = True Then If TextBox1.Text = "" Or TextBox1.Text = " " Or TextBox1.Text = " " Then Else invia() End If End If End If End Sub Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress Dim valore As Int32 = System.Convert.ToInt32(e.KeyChar) e.Handled = (valore = 13) End Sub End Class
SERVER
Spero mi sappiate aiutare, ciao.codice:Imports System.Net.Sockets Imports System.Text.UTF8Encoding Public Class Form1 Dim cliente As TcpClient Dim flusso As NetworkStream Dim x As Integer Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing Try Dim temporaneo() As Byte = UTF8.GetBytes("chiusaconnessione123454321") flusso.Write(temporaneo, 0, temporaneo.Length) Catch ex As Exception End End Try End Sub Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load collegamento.Start() End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick If cliente.Available > 0 Then Dim temporaneo(cliente.Available - 1) As Byte flusso.Read(temporaneo, 0, temporaneo.Length) Dim testo As String = UTF8.GetString(temporaneo) If testo = "chiusaconnessione123454321" Then MsgBox("LOLLO si è disconnesso dalla chat.", MsgBoxStyle.Information, "Attenzione") collegamento.Start() Button1.Enabled = False Label1.Text = "In attesa che LOLLO si connetta..." Else TextBox2.AppendText(vbCrLf & "- LOLLO: " & testo) End If End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If TextBox1.Text = "" Or TextBox1.Text = " " Or TextBox1.Text = " " Then pausa.Start() Else invia() End If End Sub Private Sub invia() pausa.Start() Button1.Enabled = False Dim temporaneo() As Byte = UTF8.GetBytes(TextBox1.Text) flusso.Write(temporaneo, 0, temporaneo.Length) TextBox2.AppendText(vbCrLf & "- NICCO: " & TextBox1.Text) TextBox1.Text = "" End Sub Private Sub collegamento_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles collegamento.Tick Try Dim indirizzo As Net.IPAddress Net.IPAddress.TryParse("192.168.1.173", indirizzo) cliente = New TcpClient cliente.Connect(indirizzo, 8888) If cliente.Connected Then collegamento.Stop() Label1.Text = "LOLLO è connesso alla chat." Button1.Enabled = True flusso = cliente.GetStream() Timer1.Start() End If Catch ex As Exception End Try End Sub Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown If e.KeyCode = Keys.Enter Then If Button1.Enabled = True Then If TextBox1.Text = "" Or TextBox1.Text = " " Or TextBox1.Text = " " Then Else invia() End If End If End If End Sub Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress Dim valore As Int32 = System.Convert.ToInt32(e.KeyChar) e.Handled = (valore = 13) End Sub Private Sub pausa_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pausa.Tick x = x + 1 If x = 1 Then pausa.Stop() Button1.Enabled = True x = 0 End If End Sub End Class

Rispondi quotando