Click here to Skip to main content
13,410,463 members (35,432 online)
Rate this:
 
Please Sign up or sign in to vote.
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
Posted 12-Jan-18 14:34pm
Updated 16-Jan-18 13:43pm

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web04 | 2.8.180221.1 | Last Updated 16 Jan 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100