-
Jul 26th, 2012, 02:05 PM
#1
Thread Starter
New Member
TCP Image Tranfer Problem
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?
-
Oct 10th, 2012, 03:13 AM
#2
Re: TCP Image Tranfer Problem
If know this was posted since July but if you're still around lemme know.
-
Oct 10th, 2012, 12:11 PM
#3
Thread Starter
New Member
Re: TCP Image Tranfer Problem
Hi Niya! 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
-
Oct 10th, 2012, 08:53 PM
#4
Re: TCP Image Tranfer Problem
Hold on there, UDP doesn't provide guarantees like TCP. TCP guarantees that your data will arrive and that it will be in proper order when it arrives at its destination. With UDP you have do not have those guarantees. First of all its not a streaming protocol but a datagram protocol which means you must divide your data into packets. Those packets are not guaranteed to be in the order you sent them, even their arrival isn't guaranteed so be aware of that. I'd advise that if you do still pursue UDP that you at least keep your datagrams under 512 bytes and that should temper some of the unreliability of the data transfer.
-
Oct 11th, 2012, 09:29 AM
#5
Thread Starter
New Member
Re: TCP Image Tranfer Problem
I know UDP is less secure than TCP. But TCP is a bit slow, and i have to transfer a lot of informations (screenshoots) from some PCs to the server app... In my client app, I send a screenshoot each 4 seconds, and the server app must support up to 20 PCs... It's an hard work for the CPU...
-
Oct 11th, 2012, 10:03 AM
#6
Re: TCP Image Tranfer Problem
Not less secure....less reliable
Well your situation really does call for high throughput so UDP may be essential in this case.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|