Hi, I'm making a server application which can control some Tcp Clients in a network...
Every connected TcpClient transfer at intervals of 3-4 seconds a bitmap image... The problem is there. The server app can receive first bitmap but it can't receive others bitmaps... I used BeginRead function to start reading a Network Stream, realizing a little loop....
This is my server code (PACKET_SIZE is a const= 4096)
VB.Net Code:
Private Sub GetImage3(ByVal ar As IAsyncResult) 'Dim Reader As BinaryReader Dim ReadBuffer(PACKET_SIZE - 1) As Byte Dim NData As Int32 Dim MStream As MemoryStream Dim LData As Int32 SyncLock _ClientTcp.GetStream _ClientTcp.GetStream.EndRead(ar) End SyncLock ' Read Length of data (Int32) 'NData = Reader.ReadInt32 NData = BitConverter.ToInt32(data, 0) ' Now comes the data, save it in a memory stream MStream = New MemoryStream MStream.Write(data, 4, LData) NData -= LData Dim counter As Integer = 0 While NData > 0 SyncLock _ClientTcp.GetStream LData = Me._ClientTcp.GetStream.Read(ImgData, 0, PACKET_SIZE) End SyncLock MStream.Write(ImgData, 0, LData) NData -= LData counter += 1 End While Dim ReceivedBitmap As Bitmap = CType(Bitmap.FromStream(MStream), Bitmap) RaiseEventScreenChanged(ReceivedBitmap) _ClientTcp.GetStream.BeginRead(data, 0, 4, AddressOf GetImage3, Nothing) End Sub
and is my send image client function (in the client app)....
VB.Net Code:
Public Shared Sub SendImage3(ByVal FilePath As String) Dim ByteArray() As Byte ' Data buffer Dim Fs As FileStream = New FileStream(FilePath, FileMode.Open, FileAccess.Read) Dim Reader As New BinaryReader(Fs) Try Dim Writer As New BinaryWriter(Iclient.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(ImageTrasferClient.PACKET_SIZE) 'write data to Network Stream Writer.Write(ByteArray) Loop While ByteArray.Length = PACKET_SIZE 'make sure all data is sent Writer.Flush() Writer.Close() Reader.Close() Catch ex As Exception ' Handle errors End Try End Sub
In the first time, server can receive image, but after this It can't receive any others images because it stops at NData = BitConverter.ToInt32(data, 0)
How can I solve this problem?![]()


Reply With Quote
I found the solution of this problem some times ago... The problem was I wrote "Writer.Close()" line after sending all the file chuncks. When you close the BinaryWriter (In this case "Writer"), attached to a Network Stream, and you close it, also the NetworkStream close the connections and do a disconnect from the server... I solved this problem removing this line and now it works fine =) Now I'm working to do this thing in UDP, I think It can be better fast than TCP 