problem reading bytes from server pleasereply soon
hi ,
i am trying to read a byte packet sent as a response by some web server , the problem i am facing is that i am not able to figure out how to end the loop reading bytes as received from server , i m putting the code below please reply soon
i know there is something wrong with the approach but i am not able to figure out how to do it
Thanks
int k = 0;
while((k=inputStream.read()) != -1)
{
System.out.print((char)k);
}
[479 byte] By [
coreJavaa] at [2007-11-27 11:40:08]

# 1
hi guys ,
i am not sure why isnt anyone replying , the question is not that dumb.
i have figured one way of doing it please reply if it is worth or if you have something better in mind .
Thanks in advance
inputStream = new DataInputStream(clientSocket.getInputStream());
byte []arr=new byte[2*1024];
inputStream.read(arr);
for(int i=0;i<arr.length;i++)
System.out.print((char)arr);
But the problem is that i dont know the size of the byte packet being recieved by me please reply .>
# 2
Just because you didn't get a reply within 49 minutes?
If the sender isn't closing the socket or the stream, you will never get the EOS indication, so the sender will have to use an application protocol to indicate the end of the transmission. Typically this is a length word prefixed to the data.
ejpa at 2007-7-29 17:30:06 >

# 3
Got the same problem.
So what do you suggest if -1 is never returned by in.read() method?
# 4
sorry for the panic i have created,
i was not able to find a solution for the problem and i am on a rush to complete my assignment.
the word you are talking about is there in some of the byte packets that i am getting not in all of them so if there is some other alternative to the above problem .
Best Regards
Mark
# 5
also the word is continuously changing
# 6
For streams that do not end (return -1) I suggest a retry:
if(in.available() == 0)
// Program a pause in the thread
if(in.available() == 0)
break;// abort input connection
# 7
In reply #3 NovaLoka wrote:
> So what do you suggest if -1 is never returned by in.read() method?
In reply #2 I had already written such a suggestion.
And using available() == 0 is not a viable option. It can return zero any time, not just at the end of the data being read.
ejpa at 2007-7-29 17:30:07 >

# 8
thanks for that reply ,
you are right i have already tried "available()" it never works
and the solution you have proposed of application protocol is not working for me either
can you suggest some other get around .
I am getting the byte packet from a web server and i dont have any information about its scheme i am just analyzing it using ethereal
I think this is a common problem every good developer on this forum has faced sometime in life so , please reply
Regards
Mark
# 9
> thanks for that reply ,
>
> you are right i have already tried "available()" it
> never works
> and the solution you have proposed of application
> protocol is not working for me either
Why? What have you tried?
> can you suggest some other get around .
Give up?
>
> I am getting the byte packet from a web server
Is this just HTTP?
# 10
no its msnms protocol sorry for the mistake i did not explained right
# 11
The MSNMS protocol already contains length words.
ejpa at 2007-7-29 17:30:07 >

# 12
Thank you very much for your time,
it is showing the length words in some of the response packets , not in all of them and that length word is not the same in all response packets for the same type of requests , it is changing
basically what i am trying to do is send some request packets and consume response packets but i dont have any scheme or information about how the server is building these packets
so i just need to figure out how do i get to know the size of the packet i am getting or where do i stop while reading the packet byte by byte since there is no EOS that i know
Regards
Mark
# 13
http://www.hypothetic.org/docs/msn/
# 14
I don't know anything about MSNMS, but according to http://www.hypothetic.org/docs/msn/general/payload_commands.php there are length words on every packet that isn't just a single line. The protocol must be self-describing and not dependent on EOS otherwise nobody could implement it. You just have to implement the protocol correctly, or better stil find an existing Java implementation of it.
> that length word is not the same in all response packets for the same type of requests , it is changing
Of course it is changing. It changes with the length of the payload.
ejpa at 2007-7-29 17:30:07 >

# 15
thanks for the replies,
i have been to that site before , actually the server i am trying to talk to is using msnsms protocol internally but the request/response packet is not ASCII like in msn messenger they have encrypted the packet in some way and i have to send/receive packets in bytes decrypting it .
i have searched the internet for quite some time and there is no information available about it
i will try to solve the problem using length words and will let you know if i am not able to figure out.
Thank You very much once again for your time
# 16
> In reply #3 NovaLoka wrote:
>
> > So what do you suggest if -1 is never returned by
> in.read() method?
>
> In reply #2 I had already written such a suggestion.
>
> And using available() == 0 is not a viable
> option. It can return zero any time, not just at the
> end of the data being read.
I feel the need of ending a lot of different types of unknown streams. Somewhere you have to decide the download is finished.
So if it doesn't return -1, I wait a little and if it seems dead, I will use the results (store them).
That's my approach.
It seems more practical.
Some of you guys are so rude!
# 17
It isn't more practical at all, it's just a problem waiting to happen. available() can return zero at any time for any of a large number of reasons that have nothing to do with your code or the sending code. You have to implement the application protocol correctly, and if you don't have one you need to design one. Every one that I am aware of contains either length words or distinct wrappers (e.g. lines, or XML).
ejpa at 2007-7-29 17:30:11 >

# 18
> That's my approach.
> It seems more practical.
> Some of you guys are so rude!
Please act less childishly.
It is not rude for someone to point out where you have given incorrect and/or otherwise misleading advice. It is your fault for posting replies on topics you don't know enough about to giving advice on.
There isn't a debate here about whether your idea is good sometimes and ejp's idea is good other times. Your idea is ALWAYS wrong. Always.
That's just a fact. And it's not rude to point that out.
If you feel that is rude then I suggest you leave this forum permanently and try JavaRanch instead. They are much more tolerant of error filled advice over there.