0

我想使用 Socket 通过 vb.net 中的套接字传输文件。

4

1 回答 1

1

服务器端:

Do
    Try
        message = reader.ReadString()

        If message = "ATTACHMENT" Then
            writer.Write("")
            Dim num = reader.ReadInt64
            Dim BR As BinaryReader
            Dim FS As NetworkStream
            FS = output
            Dim fulname As String = reader.ReadString
            BR = New BinaryReader(FS)
            Dim buffer(num) As Byte
            buffer = BR.ReadBytes(num)

            Dim BW As BinaryWriter
            Dim FSOut As FileStream
            FSOut = New FileStream(fulname, FileMode.Create, FileAccess.Write)
            BW = New BinaryWriter(FSOut)
            BW.Write(buffer)
            message = ""
            BW.Close()
            writer.Write("CLIENT >> ATTACHMENT RECIEVED")
        End If

    Catch ex As Exception
        System.Environment.Exit(System.Environment.ExitCode)
    End Try
Loop While message <> "SERVER >> EXIT"  

客户端:

Do
    Try
        messageRecieved = reader.ReadString()

        If messageRecieved = "ATTACHMENT" Then
            writer.Write("")
            Dim num = reader.ReadInt64
            Dim BR As BinaryReader
            Dim FS As NetworkStream
            FS = socketStream
            Dim fulname As String = reader.ReadString
            BR = New BinaryReader(FS)
            Dim buffer(num) As Byte
            buffer = BR.ReadBytes(num)

            Dim BW As BinaryWriter
            Dim FSOut As FileStream
            FSOut = New FileStream(fulname, FileMode.Create, FileAccess.Write)
            BW = New BinaryWriter(FSOut)
            BW.Write(buffer)
            messageRecieved = ""
            BW.Close()
            writer.Write("SERVER >> ATTACHMENT RECIEVED")
        End If
    Catch ex As Exception
        Exit Do
    End Try
Loop While messageRecieved <> "CLIENT >> EXIT" And connection.Connected
于 2012-05-03T08:48:39.577 回答