Results 1 to 3 of 3
  1. #1
    mani_minhaj is offline Member
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Angry 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 !

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    Quote Originally Posted by mani_minhaj View Post
    Hello
    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();
    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...:

    Java Code:
    int counter = 0;
    int i = 0;
    while (  ( i = in.read()) != -1 ) {
    	counter ++;
    	}
    		
    System.out.println(counter);
    ...so after '\r\n' end of stream is reached and it is -1.

    but

    how to get the total number of bytes and know that packets are ended and no more packets will arrive
    Now this lead us to protocol. When client speaks to server there must be way to know the end,
    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.

  3. #3
    mani_minhaj is offline Member
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Default

    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

  1. Finished Product: What now?
    By Unome in forum Java Applets
    Replies: 5
    Last Post: 02-11-2009, 11:41 AM
  2. raw byte stream to image
    By nupurashi in forum Advanced Java
    Replies: 2
    Last Post: 01-29-2009, 08:48 AM
  3. How to construct my finished program?
    By matpj in forum New To Java
    Replies: 0
    Last Post: 01-14-2009, 06:37 PM
  4. finished paint!
    By diggitydoggz in forum New To Java
    Replies: 3
    Last Post: 01-04-2009, 11:33 AM
  5. How to run a code when a download is finished
    By aneesahamedaa in forum New To Java
    Replies: 4
    Last Post: 10-14-2008, 01:37 PM

Posting Permissions

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