Re: Non-Blocking Socket and BufferedInputStream
Code Listing 18-4: NonBlockingServer.java
import java.nio.*;
import java.nio.channels.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class NonBlockingServer
{
public static void main(String[] args)
{
try
{
Selector selector = Selector.open();
ServerSocketChannel serverSocketChannel =
ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false);
ServerSocket serverSocket = serverSocketChannel.socket();
serverSocket.bind(new InetSocketAddress(9000));
System.out.println("Non-blocking Server created on port
9000");
serverSocketChannel.register(selector,
SelectionKey.OP_ACCEPT);
System.out.println("Waiting for client connections...");
int amountToProcess = 0;
while(true)
{
amountToProcess = selector.selectNow();
if(amountToProcess > 0)
{
try
{
Set keys = selector.selectedKeys();
Iterator iterator = keys.iterator();
while(iterator.hasNext())
{
SelectionKey selectionKey =
(SelectionKey) iterator.next();
iterator.remove(); // remove the key
int operation = selectionKey
.interestOps();
if((SelectionKey.OP_ACCEPT & operation)
!= 0)
{
// Accept the connection...
ServerSocketChannel channel =
(ServerSocketChannel)
selectionKey.channel();
SocketChannel socket =
channel.accept();
socket.configureBlocking(false);
// register for a writing operation
socket.register(selector,
SelectionKey.OP_WRITE);
System.out.println("Client
Connected...");
}
else if((SelectionKey.OP_READ &
operation) != 0)
{
// Attempt to read...
System.out.println("About to read
from client...");
SocketChannel socket =
(SocketChannel) selectionKey
.channel();
// get the message from the client...
ByteBuffer incomingLengthInBytes =
ByteBuffer.allocate(4);
// size of an 'int'
socket.read(incomingLengthInBytes);
incomingLengthInBytes.rewind();
int incomingLength =
incomingLengthInBytes.getInt();
System.out.println("Got Incoming
Length as: "+incomingLength+"
bytes");
// now allocate the correct size for
// the message...
ByteBuffer incomingData = ByteBuffer
.allocate(incomingLength);
socket.read(incomingData);
incomingData.rewind();
String string = incomingData
.asCharBuffer().toString();
// Finally print received message...
System.out.println("Received:
"+string);
// terminate the connection...
socket.close();
}
else if((SelectionKey.OP_WRITE &
operation) != 0)
{
// Attempt to write...
System.out.println("Now going to
write to client...");
SocketChannel socket =
(SocketChannel) selectionKey
.channel();
socket.register(selector,
SelectionKey.OP_READ);
String stringToSend = "This is a
message";
int length = stringToSend.length()
* 2;
ByteBuffer lengthInBytes =
ByteBuffer.allocate(4);
// 4 = size of a 'int'
ByteBuffer dataToSend =
ByteBuffer.allocate(length);
lengthInBytes.putInt(length);
lengthInBytes.rewind();
dataToSend.asCharBuffer()
.put(stringToSend);
ByteBuffer sendArray[] =
{lengthInBytes, dataToSend};
socket.write(sendArray);
//socket.close();
System.out.println("Sent Message to
Client...");
}
}
}
catch(IOException e)
{
System.out.println(e);
}
}
}
}
catch(IOException e)
{
System.out.println(e);
}
}
}
Code Listing 18-5: NonBlockingClient.java
import java.nio.*;
import java.nio.channels.*;
import java.io.*;
import java.net.*;
public class NonBlockingClient
{
public static void main(String[] args)
{
try
{
SocketChannel socketChannel = SocketChannel.open();
socketChannel.connect(new InetSocketAddress("127.0.0.1",
9000));
// wait for the message from the server...
ByteBuffer incomingLengthInBytes =
ByteBuffer.allocate(4); // size of an 'int'
socketChannel.read(incomingLengthInBytes);
incomingLengthInBytes.rewind();
int incomingLength = incomingLengthInBytes.getInt();
System.out.println("Got Incoming Length as:
"+incomingLength+" bytes");
// now allocate the correct size for the message...
ByteBuffer incomingData =
ByteBuffer.allocate(incomingLength);
socketChannel.read(incomingData);
incomingData.rewind();
String string = incomingData.asCharBuffer().toString();
// Finally print the received message...
System.out.println("Received: "+string);
// Send a message back to the server...
String replyMessage = "Message Received - Thank you!";
int length = replyMessage.length() * 2;
ByteBuffer replyLength = ByteBuffer.allocate(4);
replyLength.putInt(length);
replyLength.rewind();
ByteBuffer replyText = ByteBuffer.allocate(length);
replyText.asCharBuffer().put(replyMessage);
ByteBuffer toSend[] = {replyLength, replyText};
socketChannel.write(toSend);
}
catch(IOException e)
{
System.out.println(e);
}
}
}
On Dec 29, 5:23 am, mshe...@mail.com wrote:
Hi,
We are new to Java. We are using some exising code which does a
byte-by-byte read from the socket. The data that is being read is an
xml string. A byte-by-byte read takes about 150 to 200 milliseconds. We
need to improve the response time.
The application uses BufferedInputStream's read method to read the
data. On the net we found some sites which suggested doing read in
chunks (read an array of bytes).
Looping until read returns -1 does not work as read block which the
data read is complete.
As per the documentation the read API should return -1 if there is no
data.
Similarly DataInputStream's readFully also blocks.
Is there a non-blocking way of doing a read in Java?
Would help if you suggest some alternative.
Thanks and Regards,
M Shetty