发送数据套接字VB.NET [英] Send data socket VB.NET

查看:96
本文介绍了发送数据套接字VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我有客户端和服务器,工作正常。服务器发送文件到客户端和客户端收到它好,但当我再次尝试重新发送另一个文件服务器和客户端时,他们失去了沟通。请帮助我?thks



服务器代码:

Hi i have client and server and work fine.Server send file to client and client receive it good,But when i try again to resend another file server and client they lose communication.Please Help me?thks

Server Code:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       Dim imgPath As String = "C:\Users\Sinestic\Desktop\1.exe"
       Dim Client As New TcpClient("127.0.0.1", 9999)
       Dim ByteArray() As Byte ' Data buffer
       Dim Fs As FileStream = New FileStream(imgPath, FileMode.Open, FileAccess.Read)
       Dim Reader As New BinaryReader(Fs)
       Try
           Dim Writer As New BinaryWriter(Client.GetStream) ' Get socket's stream
           'send size of file
           Writer.Write(CInt(Fs.Length))
           'Send the file data
           Do
               'read data from file
               ByteArray = Reader.ReadBytes(2048)
               'write data to Network Stream
               Writer.Write(ByteArray)
           Loop While ByteArray.Length = 2048
           'make sure all data is sent
           Writer.Flush()
           Writer.Close()
           Reader.Close()
       Catch ex As Exception
           MessageBox.Show(ex.ToString)
       End Try

   End Sub





客户代码:



Client Code:

<pre> Client = Listener.AcceptTcpClient()
        Dim Reader As BinaryReader
        Dim ReadBuffer(PACKET_SIZE - 1) As Byte
        Dim NData As Int32
        Dim MStream As MemoryStream
        Dim LData As Int32
        Reader = New BinaryReader(Client.GetStream)
        ' Read Length of data (Int32)
        NData = Reader.ReadInt32
        ' Now comes the data, save it in a memory stream
        MStream = New MemoryStream
        While NData > 0
            LData = Client.GetStream.Read(ReadBuffer, 0, PACKET_SIZE)
            MStream.Write(ReadBuffer, 0, LData)
            NData -= LData
        End While
        Timer1.Enabled = False
        'PictureBox1.Image = Image.FromStream(MStream)
        'PictureBox1.Image.Save("C:\Users\Sinestic\Desktop\150.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
        Dim file As New FileStream("C:\Users\Sinestic\Desktop\1000.exe", FileMode.Create, FileAccess.Write)
        MStream.WriteTo(file)
        file.Close()





我尝试过:





What I have tried:

Hi i have client and server and work fine.Server send file to client and client receive it good,But when i try again to resend another file server and client they lose communication.Please Help me?thks

推荐答案

看来调用Writer.Close()(BinaryWriter.Close)也会关闭底层流 - 在本例中是你的TcpClient。



https://msdn.microsoft。 com / zh-CN / library / system.io.binarywriter.close(v = vs.110).aspx



- Pete
It appears that calling Writer.Close() (BinaryWriter.Close) also closes the underlying stream - which in this case is your TcpClient.

https://msdn.microsoft.com/en-us/library/system.io.binarywriter.close(v=vs.110).aspx

- Pete


这篇关于发送数据套接字VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆