21/01/11 14:37
LolloVB
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:
e Server:
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?
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:
Imports System.Net.Sockets Imports System.Text.ASCIIEncoding Public Class Form1 Dim client As TcpClient Dim netstream As NetworkStream Dim filenameselected As String Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click client = New TcpClient() Try Application.DoEvents() client.Connect("127.0.0.1", 1000) Catch ex As Exception End Try If client.Connected = True Then netstream = client.GetStream MsgBox("Connesso!") Else MsgBox("Connessione fallita!") End If End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Try Dim open As New OpenFileDialog If open.ShowDialog = Windows.Forms.DialogResult.OK Then filenameselected = open.FileName BackgroundWorker1.RunWorkerAsync() End If Catch ex As Exception MsgBox(ErrorToString) End Try End Sub Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork Dim FileName As String = filenameselected Dim Reader As New IO.FileStream(FileName, IO.FileMode.Open) Dim Size As Int64 = FileLen(FileName) Dim Bytes(4095) As Byte If Size > 4096 Then For Block As Int64 = 0 To Size Step 4096 If Size - Block >= 4096 Then Reader.Read(Bytes, 0, 4096) Else Reader.Read(Bytes, 0, Size - Block) End If netstream.Write(Bytes, 0, 4096) Threading.Thread.Sleep(30) Next End If End Sub End Class
e Server:
Imports System.Net.Sockets Imports System.Text.UTF8Encoding Public Class Form1 Dim client As TcpClient Dim listener As TcpListener Dim netstream As NetworkStream Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load listener = New TcpListener(1000) listener.Start() End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Try If listener.Pending = True Then client = listener.AcceptTcpClient netstream = client.GetStream Timer2.Start() End If Catch ex As Exception Timer1.Stop() MsgBox(ErrorToString) End Try End Sub Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork Dim Stream As New IO.FileStream("C:\Immagine_Trasferita.jpg", IO.FileMode.Create) If client.Available > 0 Then Do Dim Bytes(4096) As Byte Dim Msg As String = ASCII.GetString(Bytes) If Msg.Contains("END") Then Exit Do End If netstream.Read(Bytes, 0, 4096) Stream.Write(Bytes, 0, 4096) Loop Stream.Close() MsgBox("File ricevuto") Timer2.Start() End If End Sub Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick If client.Available > 0 Then Timer2.Stop() BackgroundWorker1.RunWorkerAsync() End If End Sub 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 21/01/11 14:47
aaa