Socket文件传输时的空文件输出

时间:2016-10-12 09:17:24

标签: c# vb.net sockets c#-to-vb.net

我将C#代码复制并翻译成VB.net,使用套接字发送和接收文件。已翻译的代码有效,但输出文件没有内容。代码的哪一部分导致了这个?

原始C#发送文件代码:Link和接收文件代码:Link

这是翻译的代码:

发送文件:

Imports System.IO
Imports System.Net.Sockets
Imports System.Text

Public Class Main
    Private splitter As String = "'\\'"
    Private filename As String = ""
    Private split() As String = Nothing

    Private Sub openFileButton_Click(sender As Object, e As EventArgs) Handles openFileButton.Click
        openFileSend.ShowDialog()
    End Sub

    Private Sub openFileSend_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles openFileSend.FileOk
        Dim x() As Char = splitter.ToCharArray()
        filePathDisplay.Text = openFileSend.FileName
        filename = openFileSend.SafeFileName
        'MsgBox("File name: " & openFileSend.FileName & Environment.NewLine & "File name(spit): " & openFileSend.SafeFileName)
        If filePathDisplay.Text <> Nothing Then
            sendFileButton.Enabled = True
        End If
    End Sub

    Private Sub sendFileButton_Click(sender As Object, e As EventArgs) Handles sendFileButton.Click
        Dim sock = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        Dim filenameByte() As Byte = Encoding.UTF8.GetBytes(filename)
        Dim filedataByte() As Byte = File.ReadAllBytes(filePathDisplay.Text)
        Dim fileLen() As Byte = BitConverter.GetBytes(filenameByte.Length)
        Dim bytesize = 4 + filenameByte.Length + filedataByte.Length
        Dim fbyte(bytesize) As Byte

        fileLen.CopyTo(fbyte, 0)
        filenameByte.CopyTo(fbyte, 4)
        filedataByte.CopyTo(fbyte, 4 + filenameByte.Length)

        sock.Connect("127.0.0.1", 9000)
        sock.Send(fbyte)
        sock.Close()
    End Sub
End Class

接收档案:

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

Public Class Main

    Private listenThread As Thread = Nothing
    Private rPath As String = "test"
    Private Delegate Sub TheDelegate()

    Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        listenThread = New Thread(AddressOf startListen)
        listenThread.Start()
    End Sub

    Public Shared allDone As New ManualResetEvent(False)

    Private Sub startListen()
        Dim ybytes(1024) As Byte
        Dim ipEndpoint = New IPEndPoint(IPAddress.Any, 9000)
        Dim sock As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        Try
            sock.Bind(ipEndpoint)
            sock.Listen(100)
            While True
                allDone.Reset()
                sock.BeginAccept(New AsyncCallback(AddressOf assyncCB), sock)
                allDone.WaitOne()
            End While
        Catch ex As Exception

        End Try
    End Sub

    Private Sub assyncCB(result As IAsyncResult)
        allDone.Set()
        Dim sock As Socket = DirectCast(result.AsyncState, Socket)
        Dim handler As Socket = sock.EndAccept(result)

        Dim state = New _sObjects()
        state.sock = handler
        handler.BeginReceive(state.buffer, 0, _sObjects.buffersize, SocketFlags.None,
                            New AsyncCallback(AddressOf readCallback), state
                            )

    End Sub

    Private Sub readCallback(ar As IAsyncResult)
        Dim fileLen As Integer = 1
        Dim content As String = String.Empty
        Dim state As _sObjects = DirectCast(ar.AsyncState, _sObjects)
        Dim handler As Socket = state.sock
        Dim bRead As Integer = handler.EndReceive(ar)
        Dim flag As Integer = 0
        If bRead > 0 Then
            If flag = 0 Then
                fileLen = BitConverter.ToInt32(state.buffer, 0)
                Dim fileName As String = Encoding.UTF8.GetString(state.buffer, 4, fileLen)
                rPath = "C:\" + fileName
                MsgBox(rPath)
                flag += 1
            End If
            If flag >= 1 Then
                Dim writer As BinaryWriter = New BinaryWriter(File.Open(rPath, FileMode.Append))
                If (flag = 1) Then
                    writer.Write(state.buffer, fileLen, bRead - (4 + fileLen))
                    flag += 1
                Else
                    writer.Write(state.buffer, 0, bRead)
                    writer.Close()
                    handler.BeginReceive(state.buffer, 0, _sObjects.buffersize, 0,
                            New AsyncCallback(AddressOf readCallback), state
                            )
                End If
            End If
        Else
            Invoke(New TheDelegate(AddressOf notifyMe))
        End If
    End Sub
    Private Sub notifyMe()
        MsgBox("File Received!")
    End Sub

    Private Sub Main_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        listenThread.Abort()
    End Sub
End Class

Public Class _sObjects
    Public sock As Socket = Nothing
    Public Const buffersize As Integer = 1023
    Public buffer(1023) As Byte
End Class

(手动翻译)

0 个答案:

没有答案