Results 1 to 3 of 3
- 02-25-2010, 09:28 AM #1
Member
- Join Date
- Feb 2010
- Posts
- 2
- Rep Power
- 0
How do I know that byte of stream is finished?
Hello
I am new to java , I have been listening streams form a remote device that is sending packets in tcp/ip , I am able to catch the packets but I dont know that when the packets are finished . As we know that in chatting application server or client just waits for an end of file or any termination character to terminate form the loop as in bufferedReader.readLine(); method , but how to get the total number of bytes and know that packets are ended and no more packets will arrive ????
Please help me in this I am stuck in this problem
Thank you !
- 02-25-2010, 07:41 PM #2
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
Well this is right.
Client writes to some stream (let's say PrintWriter)
and at the end of some message send '\r\n'.
Server uses let's say some BufferedReader on socket InputStreamReader
to read it and reading goes until '\r\n'.
Now you can easily count size of that message
which includes 2 additional special chars '\n' and '\r'
using simple...:
...so after '\r\n' end of stream is reached and it is -1.Java Code:int counter = 0; int i = 0; while ( ( i = in.read()) != -1 ) { counter ++; } System.out.println(counter);
but
Now this lead us to protocol. When client speaks to server there must be way to know the end,how to get the total number of bytes and know that packets are ended and no more packets will arrive
and that can be done if special char is used to mark end of stream, which server will recognize.
Or protocol has to be defined so every package is same size.
Or packages are diff size but info about size must be present in each package in special filed. That's how I see it.
It we talk about URLs and streams there is Content-Length header for this purpose.
- 02-26-2010, 12:08 PM #3
Member
- Join Date
- Feb 2010
- Posts
- 2
- Rep Power
- 0
Hi FON
Thanks for you reply . Yes you are absolutely right I have been trying by this code as you mentioned to check out that either byte is ended as "-1" is returned , but in my case when device is sending stream , it is ended with -1 came but when I tried to use it with winsock it gets the whole string , even with java when I try to loop till While(true) , then it shows me the whole string but the problem is that I need to get the whole string plus need to have some calculation to acknowledge the device that it is been reached and now I am ready to receive your further data....
I am totally confused what to do if I dont know that what device is sending to tell me that it is ended sending streams ... so thats why I am trying to find out the TCP packet that maybe it would tell me that it is contained with that number of data in it ...
I still couldnt solve my issue , please if there is any way to find out from TCP data packet layer then I might think I would be able to solve this issue otherwise...
Thanks and regards
Similar Threads
-
Finished Product: What now?
By Unome in forum Java AppletsReplies: 5Last Post: 02-11-2009, 10:41 AM -
raw byte stream to image
By nupurashi in forum Advanced JavaReplies: 2Last Post: 01-29-2009, 07:48 AM -
How to construct my finished program?
By matpj in forum New To JavaReplies: 0Last Post: 01-14-2009, 05:37 PM -
finished paint!
By diggitydoggz in forum New To JavaReplies: 3Last Post: 01-04-2009, 10:33 AM -
How to run a code when a download is finished
By aneesahamedaa in forum New To JavaReplies: 4Last Post: 10-14-2008, 12:37 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks