Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

data lost issue using sockets

Thread view: 
korcs - 19 Sep 2007 14:47 GMT
Hi,

I have an interesting issue with socket programming.

I would like to send String based information via Sockets.

As it could happen, that a part of the information gets lost on its
way to the client, I would like to send before all such packages the
size of the package as a "short" value (or something equal, but not
string because you can never be sure that your string completely
arrived ).

Does anybody know what is an elegant solution for this problem?

I send the strings using:

PrintWriter out = new PrintWriter(client.getOutputStream(), true);
out.println("test");

Thanks,

korcs
Gordon Beaton - 19 Sep 2007 15:02 GMT
> I would like to send String based information via Sockets.
>
[quoted text clipped - 5 lines]
>
> Does anybody know what is an elegant solution for this problem?

TCP is - by design - a reliable protocol. If TCP fails, you will lose
the socket connection.

What is this data loss you are imagining? Have you ever *really*
experienced it? Perhaps it's a problem in the application itself?

One reason you might want to prefix each message with its size is to
help the receiver know where one message in the stream ends and the
next one starts.

/gordon

--
derek - 19 Sep 2007 15:14 GMT
> As it could happen, that a part of the information gets lost on
> its
[quoted text clipped - 3 lines]
> string because you can never be sure that your string completely
> arrived ).

You should investigate why you are "losing" data first. Instead
of trying to put some custom checks of your own in there.
Tcp should be doing that for you. If you are "losing" data, then
you will proably "lose" some of your custom checks too, then
are you going to add more custom checks for your custom checks?

Try explaining how you are "losing" your data first.
korcs - 19 Sep 2007 15:53 GMT
> > As it could happen, that a part of the information gets lost on
> > its
[quoted text clipped - 11 lines]
>
> Try explaining how you are "losing" your data first.

I send an xml structure to a client.

This structure has of course an opening and an ending tag but the body
of the xml structure can have different lengths.

So I thought it is better to make sure that the client side knows in
advance, which size it should receive and if there was some bug during
the data transfer to know that the received xml struct is too big or
too small.
derek - 19 Sep 2007 16:06 GMT
If that is all you are sending, then dont bother with telling the other side how big it is. You could just close the socket on your end when you are done. The other end will read the data until no more data is available then process it. If there is more data after the xml, then you do need to have some agreement by which you both know where one piece of data ends and one starts. Either preface it with a length as you originally stated, or you could use a special character to signify the end of one data and the beginning of another. The choice between the two options would depend on the type of data you are sending. If you decide to use a special character, just make sure that it doesnt class with the actual data you are sending.
Roedy Green - 19 Sep 2007 17:09 GMT
>As it could happen, that a part of the information gets lost on its
>way to the client, I would like to send before all such packages the
>size of the package as a "short" value (or something equal, but not
>string because you can never be sure that your string completely
>arrived ).

TCP/IP guaranteed accurate delivery through checksums and packet
resends.

If you used a DataInputStream you could use writeUTF which prepends
each string with a 16-bit length field. see
http://mindprod.com/jgloss/jgloss/utf.html#WRITEUTF

You could prepend each field with the 32-bit length in bytes after
encoding.  If you start reading the string as a byte array, TCP will
automatically block till the whole thing is available.
See http://mindprod.com/jgloss/encoding.html

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Filip Larsen - 19 Sep 2007 21:51 GMT
> I would like to send String based information via Sockets.
>
[quoted text clipped - 3 lines]
> string because you can never be sure that your string completely
> arrived ).

You don't mention how you read the data back in, but a common mistake
that gives the behaviour you seem to observe, is to fail to read the
socket in a loop until all expected data has been read (like getting end
of file or n bytes or characters) as most Java input read methods do not
guarantee that they will read "the full block" like some of the
readFully methods do. For instance, this apply when using a Reader
subclass to read data into a character array.

Regards,
Signature

Filip Larsen

arabzwaj50@gmail.com - 20 Sep 2007 03:40 GMT
http://arabwazaef.com/arab/
                                                                 

                       
http://www.arabzwaj.com/welcome/viewtopic.php?t=2417&sid=eedb9c67db690f3d5fa9cd2
7e6f93e7d

                 

http://www.arabzwaj.com/welcome/viewtopic.php?t=2416&sid=eedb9c67db690f3d5fa9cd2
7e6f93e7d


                  .....                

http://www.arabzwaj.com/welcome/viewtopic.php?t=2423&sid=eedb9c67db690f3d5fa9cd2
7e6f93e7d

                                                     
http://www.arabzwaj.com/welcome/viewtopic.php?t=2422&sid=eedb9c67db690f3d5fa9cd2
7e6f93e7d

               
http://www.arabzwaj.com/welcome/viewtopic.php?t=2418&sid=eedb9c67db690f3d5fa9cd2
7e6f93e7d


                       !!!            
http://www.arabzwaj.com/welcome/viewtopic.php?t=2367&sid=6c6614c5f9dd9b9eaa8c035
5e13e8f9c

                                                 

http://www.arabzwaj.com/welcome/viewtopic.php?t=2332
                                                   ...!

http://www.arabzwaj.com/welcome/viewtopic.php?p=11408#11408

                   
http://www.arabzwaj.com/welcome/viewtopic.php?p=11395#11395
                                   
http://www.arabzwaj.com/welcome/viewtopic.php?t=2329

                                                             
www.arabzwaj.com

                               

                                 
http://www.arabzwaj.com/welcome/viewtopic.php?t=2333
                                                   
http://www.arabzwaj.com/welcome/viewtopic.php?t=2332
                         
http://www.arabzwaj.com/welcome/viewtopic.php?t=2328
                               
http://www.arabzwaj.com/welcome/viewtopic.php?p=11403#11403
                                               
http://www.arabzwaj.com/welcome/viewtopic.php?p=11404#11404

             
http://www.arabzwaj.com/welcome/viewtopic.php?t=2336
                               
http://www.arabzwaj.com/welcome/viewtopic.php?p=11406#11406

                                                   ...!

http://www.arabzwaj.com/welcome/viewtopic.php?p=11408#11408
                                            !
http://www.arabzwaj.com/welcome/viewtopic.php?t=2340
                       ..............   !!!!!
http://www.arabzwaj.com/welcome/viewtopic.php?t=2341

                     
http://www.arabzwaj.com/arabzwaj/welcome/viewtopic.php?t=2303
                               
http://www.arabzwaj.com/arabzwaj/welcome/viewtopic.php?t=2299&start=0&postdays=0
&postorder=asc&highlight
=
   -                                  
http://www.arabzwaj.com/welcome/viewtopic.php?t=2278
(    )                                        
http://www.arabzwaj.com/welcome/viewtopic.php?t=2279
             
http://www.arabzwaj.com/arabzwaj/welcome/viewtopic.php?t=2302&start=0&postdays=0
&postorder=asc&highlight
=
                           
http://www.arabzwaj.com/arabzwaj/welcome/viewtopic.php?t=2301

                             
http://www.arabzwaj.com/welcome/viewtopic.php?t=2270
                                                     
http://www.arabzwaj.com/welcome/viewtopic.php?t=2272
                                                 
http://www.arabzwaj.com/arabzwaj/welcome/viewtopic.php?t=2297
           (           )

http://www.arabzwaj.com/welcome/viewtopic.php?t=2271
              (          )
http://www.arabzwaj.com/welcome/viewtopic.php?t=2273
                                         
http://www.arabzwaj.com/welcome/viewtopic.php?t=2264
>>FIFA 2007
http://www.arabzwaj.com/welcome/viewtopic.php?t=2266

                            www.arabzwaj.com

for marrige and friend ship                                      
     www.arabzwaj.com

www.arabzwaj.com                                        

www.arabzwaj.com

                           

the best web site formarrige and frindship www.arabzwaj.com         
                     

                                                                   
                                                       :-
www.arabzwaj.com/welcome

enter here and watch sexy arab girls

                     
www.arabwazaef.com/arab

                 
http://www.arabzwaj.com/welcome/viewtopic.php?t=1892
                   
http://www.arabzwaj.com/welcome/viewtopic.php?t=900
****                         ****
http://www.arabzwaj.com/welcome/viewtopic.php?t=1894
                                 (                      )
http://www.arabzwaj.com/welcome/viewtopic.php?t=1880
                           
http://www.arabzwaj.com/welcome/viewtopic.php?t=1879

a     +           +          
http://www.arabzwaj.com/welcome/viewtopic.php?t=1956

                       
http://www.arabzwaj.com/welcome/viewtopic.php?p=10201#10201
              ....                              
http://www.arabzwaj.com/welcome/viewtopic.php?p=10210#10210
             000000                     0
http://www.arabzwaj.com/welcome/viewtopic.php?t=686
                    ..!
http://www.arabzwaj.com/welcome/viewtopic.php?p=10203#10203
                   
http://www.arabzwaj.com/welcome/viewtopic.php?p=10207#10207
                                             
http://www.arabzwaj.com/welcome/viewtopic.php?p=10211#10211

        ....                  !!!
http://www.arabzwaj.com/welcome/viewtopic.php?p=10200#10200
                 
http://www.arabzwaj.com/welcome/viewtopic.php?p=10212#10212
                              .....     !!
http://www.arabzwaj.com/welcome/viewtopic.php?p=10208#10208

                                                  :-
http://www.arabzwaj.com/welcome/viewtopic.php?p=10095#10095
         5000      
http://www.arabzwaj.com/welcome/viewtopic.php?t=1952&sid=15f0596ebc9c205da75f65e
b723025a2

                                       
http://www.arabzwaj.com/welcome/viewtopic.php?t=1587&start=0&postdays=0&postorde
r=asc&highlight
=
         
http://www.arabzwaj.com/welcome/viewtopic.php?p=10213#10213

                                         
http://www.arabzwaj.com/welcome/viewtopic.php?p=10209#10209
                             
http://www.arabzwaj.com/welcome/viewtopic.php?p=10205#10205
            .......              
http://www.arabzwaj.com/welcome/viewtopic.php?p=10206#10206

                                            :-
http://www.arabzwaj.com/welcome/viewtopic.php?p=9855#9855

             000000                     0
http://www.arabzwaj.com/welcome/viewtopic.php?t=686
                                                   
http://www.arabzwaj.com/welcome/viewtopic.php?t=1873
                 
http://www.arabzwaj.com/welcome/viewtopic.php?t=124

                                            :-
http://www.arabzwaj.com/welcome/viewtopic.php?p=9855#9855

                          (       )                      
http://www.arabzwaj.com/welcome/viewtopic.php?p=9736#9736
                          Windows Live Messenger 8.0
http://www.arabzwaj.com/welcome/viewtopic.php?t=1834
                       (       )
http://www.arabzwaj.com/welcome/viewtopic.php?t=1831
                               
http://www.arabzwaj.com/welcome/viewtopic.php?t=1812

                                                     
http://www.arabzwaj.com/welcome/posting.php?mode=editpost&p=9729

                  ...                    ..
http://www.arabzwaj.com/welcome/viewtopic.php?t=1796

                                       

http://www.arabzwaj.com/welcome/viewtopic.php?t=1789
                                                         
http://www.arabzwaj.com/welcome/viewtopic.php?t=1568&highlight=%E3%E1%DD
3                                    
http://www.arabzwaj.com/welcome/viewtopic.php?t=1830

                                             
http://www.arabzwaj.com/welcome/viewtopic.php?t=1790
                                     
http://www.arabzwaj.com/welcome/viewtopic.php?t=102

                          00,                              
http://www.arabzwaj.com/welcome/viewtopic.php?p=9739#9739
                   (      )
http://www.arabzwaj.com/welcome/viewtopic.php?p=9740#9740
                                       2038
http://www.arabzwaj.com/welcome/viewtopic.php?t=1837
               
http://www.arabzwaj.com/welcome/viewtopic.php?t=1824
                   
http://www.arabzwaj.com/welcome/viewtopic.php?t=1821
                                       
http://www.arabzwaj.com/welcome/viewtopic.php?t=1768
                             !!
http://www.arabzwaj.com/welcome/viewtopic.php?t=1751
                               
http://www.arabzwaj.com/welcome/viewtopic.php?t=1838
                                                         
http://www.arabzwaj.com/welcome/viewforum.php?f=11
                                                   
http://www.arabzwaj.com/welcome/viewtopic.php?t=234
                                         
                                               
                     
                                                                   
                                                               
www.arabzwaj.com
                               
        :-arabzwaj@yahoo.com
                                                               
                     
hide ip
              +                                  :
           :-
http://d.turboupload.com/d/839284/157615851606157516051580hide.rar.html
            :-
http://www.megaupload.com/?d=BBCGC4J0

the best web site formarrige and frindship www.arabzwaj.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.