• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

unable to read data from server using socketchannle (help please)

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am unable to read data from socket channel .. I am trying write and read data from server ...
key.isReadable() is returning false ....
How do I read data from server ....
Here is my code ...
public String getResponse(byte[] request) throws Exception {
StringBuffer sbResponse = null;
byte[] array = null;
String str = null;
SocketChannel client = SocketChannel.open();
client.configureBlocking(false);
client.connect(new java.net.InetSocketAddress("xxx.xxx.xxx.xxx", yyyyy));
// Create selector
Selector selector = Selector.open();
// Record to selector (OP_CONNECT type)
SelectionKey clientKey = client.register(selector,SelectionKey.OP_CONNECT | SelectionKey.OP_READ);
// Waiting for the connection
if(selector.select(50) > 0) {
// Get keys
Set keys = selector.selectedKeys();
Iterator i = keys.iterator();
// For each key...
while (i.hasNext()) {
SelectionKey key = (SelectionKey) i.next();
// Remove the current key
i.remove();
// Get the socket channel held by the key
SocketChannel channel = (SocketChannel) key.channel();
// Attempt a connection
if (key.isConnectable()) {
// Close pendent connections
if (channel.isConnectionPending())
channel.finishConnect();
// Write data into channel
ByteBuffer requestByteBuffer = ByteBuffer.wrap(request);
channel.write(requestByteBuffer);
requestByteBuffer.clear();
}
if(key.isReadable()) {
//Allocates a new byte buffer
ByteBuffer buffer1 = ByteBuffer.allocate(100);
sbResponse = new StringBuffer();
try {
while (channel.read(buffer1) > 0) {
array = new byte[100];
buffer1.position(0);
buffer1.get(array, 0, 100);
str = new String(array);
sbResponse.append(str);
buffer1.clear();
}
} catch (IOException e) { e.printStackTrace(); }
}
if(channel != null) channel.close();
} // end while
} // end if
return sbResponse.toString();
}
Thanks,
suchi.
 
suchi suchi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
does any one know why my code is unable to read data from server?
key.isConnectable() returning true which means it is connected to server
Then why is key.isReadable() returing false ...
Thanks,
Suchi.
 
suchi suchi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can any one help me? Still I am unable to figureout what the problem is?
If I say key.isConnectable() returning true where as
key.isWritable() or key.isReadable() returing false ...
What I have to do to write and read data from server using socketchannel ....
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess this method is not called twice, to get the connection first and to read data the second.
If it is called only once there is missing a loop around selector.select() to allow the event OP.READ to occur.
There is useful/vital information in this article pratical NIO
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I played a bit more with Selector.
Here is a test that allows you to print the state of a selector -the selected keys an registered ones-- as it goes on accepting new connections and reading some text files from them. The text files are shown in a text area.
To the SelectionKey of a newly accepted connection I attach a Boolean that allows the processData method to know when it is processing the first data for a connection. Information about the files to receive is contained in the first Data object received. And for simplicity I assume this information is obtained by the first call to SocketChannel.read(). I could have created Files objects based on the determined names and saved to them the corresponding content.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And here is a program to test the one above. Please do not look at the code just use it
Write the ip on which the server is running in the text field. Hit Enter and select several text files to be transmitted.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wierd. The examples do not run if executed by "c:\Archivos de programa\java\j2re1.4.2_03\bin\java.exe", but they do by "c:\j2sdk.1.4.2_03\bin\java.exe"
 
reply
    Bookmark Topic Watch Topic
  • New Topic