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)
{
}
}
}
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)
{
}
}
}