wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes unable to read data from server using socketchannle (help please) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "unable to read data from server using socketchannle (help please)" Watch "unable to read data from server using socketchannle (help please)" New topic
Author

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

suchi suchi
Greenhorn

Joined: Mar 10, 2004
Posts: 3
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

Joined: Mar 10, 2004
Posts: 3
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

Joined: Mar 10, 2004
Posts: 3
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 ....
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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


SCJP2. Please Indent your code using UBB Code
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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

Joined: Jul 03, 2001
Posts: 2120
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

Joined: Jul 03, 2001
Posts: 2120
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"
 
GeeCON Prague 2014
 
subject: unable to read data from server using socketchannle (help please)