This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes Urgent help please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Urgent help please" Watch "Urgent help please" New topic
Author

Urgent help please

Tom Eric
Greenhorn

Joined: Nov 03, 2003
Posts: 9
Hi,
I am trying to read data into ByteBuffer using SocketChannel.
The following code working fine but the problem is with ByteBuffer size.
The data varies from request to request .
How do I read data into ByteBuffer with out specifing size .
The problem with size is some records has more than 500 bytes and some are less than 500 bytes.
How do i solve this problem ..
Is there any way to read data dynamically from socket channel ...
Any help on this please ...
The data I have to read is in the following format ..
XXX ... yyyyy
AAA
BBBBBBBBBBBBBBBBBBBBBB
CCCCCCCCCCCCCCCCCCCC
DDDDDDDDDDDDDDDDDDDDDDd
etc ...

Here is my Code:
public ByteBuffer getResponse(byte[] request) throws Exception {
int lenth = 500;
InetSocketAddress isa = new InetSocketAddress("ip", port);
SocketChannel channel = SocketChannel.open(isa);
ByteBuffer requestBuffer = ByteBuffer.wrap(request);
channel.write(requestBuffer);
ByteBuffer responseByteBuffer = ByteBuffer.allocate(length);
int num = 0;
while (num == 0 ) {
responseByteBuffer.rewind();
num = channel.read(responseByteBuffer);
}
channel.close();
return responseByteBuffer;
}
Thanks.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8843
    
    7

How about a loop where you read x bytes from the channel then append them to some sort of buffer (java.io.ByteOutputStream comes to mind)? I'd definately check into the return value of read() if I were you. . .


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Tom Eric
Greenhorn

Joined: Nov 03, 2003
Posts: 9
Hi,
How do you use loop ?
What do you use in loop condition?
Any sample code ..
Thanks for your quick respose ..
tomerict erictomt
Greenhorn

Joined: Nov 12, 2003
Posts: 2
hi,
I tried with ByteOutputStream but not working ..
please can you give sample code how to read data into ByteOutputStream or
into ByteBuffer or what changes do i need to do in my code .
Thanks.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8843
    
    7

Originally posted by Tom Eric:

How do you use loop ?
What do you use in loop condition?

You are trying to conquer socket communication, especially using the new IO classes, and you don't know how to use a loop? Start from the beginning. Check out the
java tutorial. There's even a section on sockets
Tom Eric
Greenhorn

Joined: Nov 03, 2003
Posts: 9
Hi,
I know how to use I/O streams but the problem in my case is different ..
Simply don't say read java doc, fundamentals blah blah ...
If you know the answer tell me to the following question ..
I have data some thing like
xxxxxxx rrr \n
sfsf sdf sf \n
sdfsd sf \n
etc ...
What do u use in while loop condition ..
Thanks for your good feed back ..
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8843
    
    7

Like I said in my first post, check the return value of read(). As with every other IO stream it returns the number of bytes read or -1 for end-of-(file, stream, array). Since I don't have your requirements, I am assuming that you want to read the entire response, new lines and all.
Tom Eric
Greenhorn

Joined: Nov 03, 2003
Posts: 9
I done it .. Thanks for your response ...
 
Consider Paul's rocket mass heater.
 
subject: Urgent help please
 
Similar Threads
any help on reading data please
selector.select() problem
timeout not working by the time of reading data from channel (urgent help please ..)
does a nio socket buffer data internally too?
ServerSocketChannel.accept() seems to create a random port to send data to?