Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do i send and receive data with TCPClient (with large serialized object)

Posted on 2008-06-12
7
Medium Priority
?
1,449 Views
Last Modified: 2013-11-07
Hi experts,

I try to send custom objects (dataset/serializable object, etc...) to clients from the server
the communication works fine, and i receive data when i use short string but now, i have implemented server side to send custom objects and since this implementation, i cannot deserialize back in the clients...

what i discovered is when i use customs objects, i received twice call from the server, i'm not sure of it but it seems that the client do a "beginread" to quickly and the server has not enough time to send the object entirely so it sends data "chunked"?

can you help me?

regards


here the Server code to send data to clients :

Dim dataExch As New DataExchange(Subscription, Data)

                'serialize the dataExchange class in binary format
                ms = ClsMisc.SerializeBinary(Of DataExchange)(dataExch)

                If ms IsNot Nothing Then

                    'convert the memorystream to get a full string to be send in plain text
                    Data = System.Convert.ToBase64String(ms.ToArray)
                Else
                    Exit Sub
                End If


                SyncLock Me._subscriber.GetStream
                    If Me._subscriber.GetStream.CanWrite Then
                        Dim writer As New IO.StreamWriter(Me._subscriber.GetStream)
                        writer.Write(Data.ToString)
                        writer.Flush()
                    End If
                End SyncLock



Client code :


 Dim BytesRead As Int32
            Dim strMessage As String

            Try
                ' Finish asynchronous read into readBuffer and return number of bytes read.
                BytesRead = Me._client.GetStream.EndRead(ar)
            Catch e As Exception
                Me.Close()
                RaiseEvent onDisconnected(Me)
            End Try

            If BytesRead < 1 Then
                ' If no bytes were read server has close.  Disable input window.
                Me.Close()
                RaiseEvent onDisconnected(Me)
                Exit Sub
            End If
   Dim ms As New MemoryStream
                        Dim b() As Byte = System.Convert.FromBase64String(strMessage)
                        ms.Read(b, 0, b.Length)

'HERE I CANNOT DESERIALIZE THE DATAEXCHANGE CLASS
                        Dim ds As DataExchange = ClsMisc.DeSerializeBinary(Of DataExchange)(ms)


0
Comment
Question by:Dnx_7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 25

Expert Comment

by:apeter
ID: 21770432
Either you should know how many bytes  you are sending ahead or you should have a mechanism of endding all messsges with a custom character you are sending and in the server side you should read the stream untill you read your "custom end character".

Else instead of custom end character send the length first and then send all your data.  Server should read this length first and then read the bytes untill you get the total length.
0
 
LVL 2

Author Comment

by:Dnx_7
ID: 21770840
what i tried to do is to send the first time the lenght of data and then send the data right after.

is it a good idea?

because i don't know how to read until all data is received...

regards
0
 
LVL 25

Expert Comment

by:apeter
ID: 21771798
When we handle our own way of sending and receiving bytes, we should have our own logic.  we have to pss this length somehow passed to the receiving fuction. This is one way when the systems are disconnected.
0
 
LVL 2

Author Comment

by:Dnx_7
ID: 21776872
ok i found a turn-around to get all data but here is my problem :

i describe the steps for the server :

create a new dataset with some data inside
create an instance of dataExchange (which is serializable)
serialize dataexchange with binary formatter into a memory stream
convert the memorystream into base64 string
send this string to the client


client side :

receive all the data into byte array
use memory stream to read the byte array => here is the problem, i always have a "0" byte readed!
deserialize back the string into dataexchange object
... do something with the dataExchange


where i have the problem i double check the base 64 send from the server and received from the client, they are exactly the same (i used winMerge to do this)

please help, i don't know what to do...

Regards
0
 
LVL 2

Accepted Solution

by:
Dnx_7 earned 0 total points
ID: 21777366
Hi i resolve my problem...

i replace :

ms.read(b, 0, b.length)
by

ms = New MemoryStream(b, False)

and it works...

Thank you anyway :)

Regards
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question