Nio channel, sendint to standard socket

F

farseer

Hi,
I have a server which was implemented using standard (non-NIO) sockets.
It waits to accept() a connection fromt the client, and once that
occurs, it spawns a thread which sends contents of a file to a the
client.

I tried creating a simple NIO client to connect to this server. I am
able to connect to the server (the server accepts the connection). but
from there, once i try to send a string to the server, the server does
not seem to respond. it waits on the clientSocket.read() line. My
question is, can NIO classes be used to communicate with a server
socket implemented using standard Socket streams?

here is snippet of the server code that waits to read. It appears it is
no able to detect streams from the client:
System.out.println( "waiting for read...");
String clientMsg = clientSocket.readLine();
System.out.println( "read msg: " + clientMsg);

here's the snippet of code that connects to the server and sends a
message:
InetSocketAddress socketAddress = new InetSocketAddress(
ip, port );
// Connect
channel = SocketChannel.open( );
channel.configureBlocking( false );
channel.socket().connect( socketAddress );

selector = Selector.open( );
channel.register( _selector, SelectionKey.OP_CONNECT
| SelectionKey.OP_READ );

try
{
ByteBuffer buffer = ByteBuffer.allocateDirect( 1024 );

while ( selector.select( ) > 0)
{
// Get set of ready objects
Set readyKeys = _selector.selectedKeys();
Iterator readyItor = readyKeys.iterator();

// Walk through set
while (readyItor.hasNext())
{
// Get key from set
SelectionKey key = (SelectionKey)readyItor.next();

// Remove current entry
readyItor.remove();

// Get channel
SocketChannel keyChannel = (SocketChannel)key.channel();

if (key.isConnectable())
{
if ( keyChannel.isConnectionPending() )
keyChannel.finishConnect();

//keyChannel.configureBlocking( false );

buffer.put( "0".getBytes() );
buffer.flip();
keyChannel.write( buffer );

}
else if (key.isReadable())
{
keyChannel.read( buffer );
buffer.flip();

//System.out.println( new String( buffer.array() ) );
}
}
}
}
catch ( IOException ioe)
{
ioe.printStackTrace();
}
finally
{
if ( _channel != null )
{
try
{
_channel.close();

}
catch (IOException ignored)
{
}
}
}
 
G

Gordon Beaton

My question is, can NIO classes be used to communicate with a server
socket implemented using standard Socket streams?

Of course, the protocol doesn't care what library routines you use.
here is snippet of the server code that waits to read. It appears it
is no able to detect streams from the client:

System.out.println( "waiting for read...");
String clientMsg = clientSocket.readLine();

Your server is waiting for a complete *line* of text from the client.
It will block here until you send a newline or close the connection.
buffer.put( "0".getBytes() );
buffer.flip();
keyChannel.write( buffer );

The message you've sent from the client doesn't end with a newline, so
the server continues to wait.

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top