Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net TCP文件共享_.net_Vb.net_Visual Studio 2010_Visual Studio_File - Fatal编程技术网

.net TCP文件共享

.net TCP文件共享,.net,vb.net,visual-studio-2010,visual-studio,file,.net,Vb.net,Visual Studio 2010,Visual Studio,File,问题是,每当我发送文件时,文件都会被发送,但它总是空的(0字节),我不知道是什么导致了这种情况 以下是发件人的代码: 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, ByV

问题是,每当我发送文件时,文件都会被发送,但它总是空的(0字节),我不知道是什么导致了这种情况

以下是发件人的代码:

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
以下是接收器的代码:

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

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

代码检查:不要使用VB文件(FileOpen/Close)。@usr我还可以使用什么来代替FileOpen/Close?@usr先生,您可以举个例子吗?您已经使用了FileStream。在网上很容易找到东西。你的问题得到回答了吗?@usr我的意思是,我如何才能在接收端做到这一点