将文件发送到tcp服务器后的空文件

时间:2017-06-17 12:20:09

标签: vb.net tcp tcpclient

我尝试整理这个代码我已经管理确实使它工作但是然后我尝试将文件从客户端发送到服务器然后文件它变空并且没有扩展名。 好吧,我已尝试从互联网上的一些例子,但我无法正常工作

这是我的代码

Imports System.Threading
Imports System.Net.Sockets
Imports System.IO
Imports System.Net

Public Class Form1


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Button1.Visible = False
    RECIBE()
End Sub
Public Sub RECIBE()

    Dim CLIENTE As TcpClient
    Dim TAMAÑOBUFFER As Integer = 1024
    Dim ARCHIVORECIBIDO As Byte() = New Byte(TAMAÑOBUFFER - 1) {}
    Dim BYTESRECIBIDOS As Integer
    Dim FIN As Integer = 0
    Dim SERVIDOR As New TcpListener(IPAddress.Any, 8080)
    SERVIDOR.Start()
    While FIN = 0
        Dim NS As NetworkStream = Nothing
        Try
            Dim ACEPTA As String = "ACEPTA EL FICHERO ENTRANTE"
            Dim TITULO As String = "FICHERO ENTRANTE"
            Dim BOTONES As MessageBoxButtons = MessageBoxButtons.YesNo
            Dim RESULTADO As DialogResult
            If SERVIDOR.Pending Then
                CLIENTE = SERVIDOR.AcceptTcpClient
                NS = CLIENTE.GetStream
                RESULTADO = MessageBox.Show(ACEPTA, TITULO, BOTONES)
                If RESULTADO = Windows.Forms.DialogResult.Yes Then
                    Dim FICHERORECIBIDO As String = Nothing
                    If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                        FICHERORECIBIDO = SaveFileDialog1.FileName
                        Label1.Text = FICHERORECIBIDO
                    End If
                    If FICHERORECIBIDO <> String.Empty Then
                        Dim TOTALBYTESRECIBIDOS As Integer = 0
                        Dim FS As New FileStream(FICHERORECIBIDO, FileMode.OpenOrCreate, FileAccess.Write)
                        While (AYUDAENLINEA(BYTESRECIBIDOS, NS.Read(ARCHIVORECIBIDO, 0, ARCHIVORECIBIDO.Length))) > 0
                            FS.Write(ARCHIVORECIBIDO, 0, BYTESRECIBIDOS)
                            TOTALBYTESRECIBIDOS = TOTALBYTESRECIBIDOS + BYTESRECIBIDOS
                        End While
                        FS.Close()
                    End If
                    NS.Close()
                    CLIENTE.Close()
                    'SERVIDOR.Stop()
                    MsgBox("DESCARGA FINALIZADA")
                    FIN = 1
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End While
    SERVIDOR.Stop()
    Button1.Visible = True
End Sub
Private Shared Function AYUDAENLINEA(Of T)(ByRef OBJETIVO As T, VALOR As T)
    OBJETIVO = VALOR
    Return VALOR
End Function


End Class

客户端发件人 这是我的代码

Imports System.IO
Imports System.Net.Sockets

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        TextBox1.Text = OpenFileDialog1.FileName


    End If
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim TAMAÑOBUFFER As Integer = 1024
    Try
        Dim CLIENTE As New TcpClient(TextBox2.Text, TextBox3.Text)
        Dim NS As NetworkStream = CLIENTE.GetStream
        Dim FS As New FileStream(TextBox1.Text, FileMode.Open, FileAccess.Read)
        Dim PAQUETES As Integer = CInt(Math.Ceiling(CDbl(FS.Length) / CDbl(TAMAÑOBUFFER)))
        Dim LONGITUDTOTAL As Integer = CInt(FS.Length)
        Dim LONGITUDPAQUETEACTUAL As Integer = 0
        Dim CONTADOR As Integer = 0
        For I As Integer = 0 To PAQUETES - 1
            If LONGITUDTOTAL > TAMAÑOBUFFER Then
                LONGITUDPAQUETEACTUAL = TAMAÑOBUFFER
                LONGITUDTOTAL = LONGITUDTOTAL - LONGITUDPAQUETEACTUAL
            Else
                LONGITUDPAQUETEACTUAL = LONGITUDTOTAL
            End If
            Dim ENVIARBUFFER As Byte() = New Byte(LONGITUDPAQUETEACTUAL - 1) {}
            FS.Read(ENVIARBUFFER, 0, LONGITUDPAQUETEACTUAL)
            NS.Write(ENVIARBUFFER, 0, CInt(ENVIARBUFFER.Length))

        Next
        FS.Close()
        NS.Close()
        CLIENTE.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

End Class

0 个答案:

没有答案