Results 1 to 6 of 6

Thread: TCP Image Tranfer Problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    15

    Exclamation 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:
    1. Private Sub GetImage3(ByVal ar As IAsyncResult)
    2.         'Dim Reader As BinaryReader
    3.         Dim ReadBuffer(PACKET_SIZE - 1) As Byte
    4.         Dim NData As Int32
    5.         Dim MStream As MemoryStream
    6.         Dim LData As Int32
    7.  
    8.         SyncLock _ClientTcp.GetStream
    9.             _ClientTcp.GetStream.EndRead(ar)
    10.         End SyncLock
    11.  
    12.         ' Read Length of data (Int32)
    13.         'NData = Reader.ReadInt32
    14.         NData = BitConverter.ToInt32(data, 0)
    15.         ' Now comes the data, save it in a memory stream
    16.         MStream = New MemoryStream
    17.         MStream.Write(data, 4, LData)
    18.         NData -= LData
    19.         Dim counter As Integer = 0
    20.         While NData > 0
    21.             SyncLock _ClientTcp.GetStream
    22.                 LData = Me._ClientTcp.GetStream.Read(ImgData, 0, PACKET_SIZE)
    23.             End SyncLock
    24.             MStream.Write(ImgData, 0, LData)
    25.             NData -= LData
    26.             counter += 1
    27.         End While
    28.         Dim ReceivedBitmap As Bitmap = CType(Bitmap.FromStream(MStream), Bitmap)
    29.         RaiseEventScreenChanged(ReceivedBitmap)
    30.         _ClientTcp.GetStream.BeginRead(data, 0, 4, AddressOf GetImage3, Nothing)
    31.     End Sub

    and is my send image client function (in the client app)....
    VB.Net Code:
    1. Public Shared Sub SendImage3(ByVal FilePath As String)
    2.         Dim ByteArray() As Byte ' Data buffer
    3.         Dim Fs As FileStream = New FileStream(FilePath, FileMode.Open, FileAccess.Read)
    4.         Dim Reader As New BinaryReader(Fs)
    5.         Try
    6.             Dim Writer As New BinaryWriter(Iclient.GetStream) ' Get socket's stream
    7.             'send size of file
    8.             Writer.Write(CInt(Fs.Length))
    9.  
    10.             'Send the file data
    11.             Do
    12.                 'read data from file
    13.                 ByteArray = Reader.ReadBytes(ImageTrasferClient.PACKET_SIZE)
    14.                 'write data to Network Stream
    15.                 Writer.Write(ByteArray)
    16.             Loop While ByteArray.Length = PACKET_SIZE
    17.             'make sure all data is sent
    18.             Writer.Flush()
    19.             Writer.Close()
    20.             Reader.Close()
    21.         Catch ex As Exception
    22.             ' Handle errors
    23.         End Try
    24.     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?

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: TCP Image Tranfer Problem

    If know this was posted since July but if you're still around lemme know.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    15

    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

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    15

    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...

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width