This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Urgent help please

 
Tom Eric
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9264
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. . .
 
Tom Eric
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How do you use loop ?
What do you use in loop condition?
Any sample code ..
Thanks for your quick respose ..
 
tomerict erictomt
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9264
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9264
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I done it .. Thanks for your response ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic