Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - (VB.NET) Invio file tramite system.net.sockets
Forum - C# / VB.NET - (VB.NET) Invio file tramite system.net.sockets

Avatar
LolloVB (Normal User)
Newbie


Messaggi: 15
Iscritto: 08/01/2011

Segnala al moderatore
Postato alle 15:37
Venerdì, 21/01/2011
Salve a tutti!

Allora, sono arrivato al capitolo sui sockets del libro che sto leggendo.

Tramite il libro sono riuscito a stabilire connessioni; multiconnessioni e trasferimenti di stringhe di testo.

Fino a qui tutto apposto.

Ora però voglio provare a trasferire un vero e proprio file tra i 2 host, per fare ciò ho creato un progetto a parte (Client e Server).

Non comprendendo bene le parole del libro mi sono interessato alla guida di Totem sul trasferimento tramite sockets. E sono arrivato a questo codice:

Client:

Codice sorgente - presumibilmente VB.NET

  1. Imports System.Net.Sockets
  2. Imports System.Text.ASCIIEncoding
  3. Public Class Form1
  4.  
  5.     Dim client As TcpClient
  6.     Dim netstream As NetworkStream
  7.     Dim filenameselected As String
  8.  
  9.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  10.  
  11.     End Sub
  12.  
  13.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  14.         client = New TcpClient()
  15.         Try
  16.             Application.DoEvents()
  17.             client.Connect("127.0.0.1", 1000)
  18.         Catch ex As Exception
  19.  
  20.         End Try
  21.  
  22.         If client.Connected = True Then
  23.             netstream = client.GetStream
  24.             MsgBox("Connesso!")
  25.         Else
  26.             MsgBox("Connessione fallita!")
  27.         End If
  28.     End Sub
  29.  
  30.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  31.         Try
  32.             Dim open As New OpenFileDialog
  33.             If open.ShowDialog = Windows.Forms.DialogResult.OK Then
  34.                 filenameselected = open.FileName
  35.                 BackgroundWorker1.RunWorkerAsync()
  36.             End If
  37.         Catch ex As Exception
  38.             MsgBox(ErrorToString)
  39.         End Try
  40.     End Sub
  41.  
  42.     Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
  43.  
  44.         Dim FileName As String = filenameselected
  45.         Dim Reader As New IO.FileStream(FileName, IO.FileMode.Open)
  46.         Dim Size As Int64 = FileLen(FileName)
  47.         Dim Bytes(4095) As Byte
  48.  
  49.         If Size > 4096 Then
  50.  
  51.             For Block As Int64 = 0 To Size Step 4096
  52.  
  53.                 If Size - Block >= 4096 Then
  54.                     Reader.Read(Bytes, 0, 4096)
  55.                 Else
  56.                     Reader.Read(Bytes, 0, Size - Block)
  57.                 End If
  58.  
  59.                 netstream.Write(Bytes, 0, 4096)
  60.  
  61.                 Threading.Thread.Sleep(30)
  62.             Next
  63.  
  64.         End If
  65.     End Sub
  66. End Class



e Server:

Codice sorgente - presumibilmente VB.NET

  1. Imports System.Net.Sockets
  2. Imports System.Text.UTF8Encoding
  3.  
  4. Public Class Form1
  5.  
  6.     Dim client As TcpClient
  7.     Dim listener As TcpListener
  8.     Dim netstream As NetworkStream
  9.  
  10.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  11.         listener = New TcpListener(1000)
  12.         listener.Start()
  13.     End Sub
  14.  
  15.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  16.         Try
  17.             If listener.Pending = True Then
  18.                 client = listener.AcceptTcpClient
  19.                 netstream = client.GetStream
  20.  
  21.                 Timer2.Start()
  22.             End If
  23.         Catch ex As Exception
  24.             Timer1.Stop()
  25.             MsgBox(ErrorToString)
  26.         End Try
  27.     End Sub
  28.  
  29.     Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
  30.  
  31.         Dim Stream As New IO.FileStream("C:\Immagine_Trasferita.jpg", IO.FileMode.Create)
  32.  
  33.         If client.Available > 0 Then
  34.  
  35.             Do
  36.                 Dim Bytes(4096) As Byte
  37.                 Dim Msg As String = ASCII.GetString(Bytes)
  38.  
  39.                 If Msg.Contains("END") Then
  40.                     Exit Do
  41.                 End If
  42.  
  43.                 netstream.Read(Bytes, 0, 4096)
  44.                 Stream.Write(Bytes, 0, 4096)
  45.  
  46.             Loop
  47.  
  48.             Stream.Close()
  49.             MsgBox("File ricevuto")
  50.             Timer2.Start()
  51.  
  52.         End If
  53.     End Sub
  54.  
  55.     Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
  56.         If client.Available > 0 Then
  57.             Timer2.Stop()
  58.             BackgroundWorker1.RunWorkerAsync()
  59.         End If
  60.     End Sub
  61. End Class



Il problema è che questo codice non funziona, difatti ho provato a mettere un messaggio che mi avvertisse che il file è stato aperto e che ci sta scrivendo dentro, ma quel messaggio non parte e da qui deduco che il server non riceve, però altra cosa strana è che il client continua ad inviare byte anche dopo molto tempo che il trasferimento è attivo (quando chiudo il server il client va in errore su netstream.write).

Il punto è che non riesco proprio a capire dove sbaglio... qualche idea?




Ultima modifica effettuata da LolloVB il 21/01/2011 alle 15:47
PM
Avatar
LolloVB (Normal User)
Newbie


Messaggi: 15
Iscritto: 08/01/2011

Up
0
Down
V
Segnala al moderatore
Postato alle 20:23
Venerdì, 21/01/2011
Fa niente ragazzi, lavorandoci su senza usare la guida di totem (usando la logica) sono riuscito a creare un file transfeer perfetto, perchè alla fine il procedimento è lo stesso dell' invio dei messaggi di testo, cambia solo che l' invio è in byte (che divido in blocchi da 4096 byte)...

Grazie cmq a chiunque ha letto questa domanda :k:

Ultima modifica effettuata da LolloVB il 21/01/2011 alle 20:25
Non potresti postare il codice please? - Milmor94 - 15/07/11 11:46
PM