首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TCP文件共享

TCP文件共享
EN

Stack Overflow用户
提问于 2014-08-27 22:30:28
回答 1查看 61关注 0票数 1

问题是,每当我发送文件时,文件都会被发送,但是文件始终是空的(0字节),我不知道是什么原因造成的。

以下是发送方的代码:

代码语言:javascript
复制
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Public Class Form2
Dim filePath As String
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Button2.Enabled = False
    TextBox1.Enabled = False
    filePath = TextBox2.Text
    Dim sendThread As New Thread(AddressOf SendSub)
    sendThread.IsBackground = True
    sendThread.Start()
End Sub

Private Sub SendSub()
    Dim client As New TcpClient
    client.Connect(TextBox1.Text, 2222)
    Try
        Dim nstm As Stream = client.GetStream
        Dim fstm As Stream = New FileStream(filePath, FileMode.Open, FileAccess.Read)

        Dim buffer(1024 - 1) As Byte
        Do While True
            Dim bytesRead As Integer = fstm.Read(buffer, 0, buffer.Length)
            If bytesRead = 0 Then Exit Do
            nstm.Write(buffer, 0, bytesRead)
            nstm.Flush()
        Loop

        client.Close()
        nstm.Close()
        fstm.Close()

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Dim diag As New OpenFileDialog
    diag.InitialDirectory = "C:\"
    diag.Filter = "All Files (*.*)|*.*|All Files (*.*)|*.*"
    diag.FilterIndex = 2
    diag.RestoreDirectory = True
    If diag.ShowDialog = Windows.Forms.DialogResult.OK Then
        TextBox2.Text = diag.FileName
    End If
End Sub
End Class

这是接收者的代码:

代码语言:javascript
复制
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Public Class Form1
Dim filepath As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Button1.Enabled = False
    TextBox1.Enabled = False
    filepath = TextBox1.Text
    Dim listenerThread As New Threading.Thread(AddressOf ListeningSub)
    listenerThread.IsBackground = True
    listenerThread.Start()
End Sub
Private Sub ListeningSub()
    Dim server As New TcpListener(IPAddress.Any, 2222)
    server.Start()
    Try
        While True
            Dim c As TcpClient = server.AcceptTcpClient
            Dim s As NetworkStream = c.GetStream

            FileOpen(1, filepath, OpenMode.Binary)
            Dim buffer(1024 - 1) As Byte
            Do While True
                Dim bytesRead As Integer = s.Read(buffer, 0, buffer.Length)
                If bytesRead = 0 Then Exit Do
            Loop
            FileClose(1)

            s.Close()
            c.Close()
        End While
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim diag As New OpenFileDialog
    diag.InitialDirectory = "C:\"
    diag.Filter = "All Files (*.*)|*.*|All Files (*.*)|*.*"
    diag.FilterIndex = 2
    diag.RestoreDirectory = True
    If diag.ShowDialog = Windows.Forms.DialogResult.OK Then
        TextBox1.Text = diag.FileName
    End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Form2.Show()
End Sub
End Class
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-27 22:43:27

您根本不向目标文件写入。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25537960

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档