I am developing a server that is connected with many clients. I need to know when a client is disconnecting from server. So each client is sending a specific character to the server. If the character is not received after two seconds then I should disconnect the server from the client (releasing allocated resource for this client).
This is the main code of my server:
public EchoServer(int port) throws IOException {
this.port = port;
hostAddress = InetAddress.getByName("127.0.0.1");
selector = initSelector();
loop();
}
private Selector initSelector() throws IOException {
Selector socketSelector = SelectorProvider.provider().openSelector();
ServerSocketChannel serverChannel = ServerSocketChannel.open();
serverChannel.configureBlocking(false);
InetSocketAddress isa = new InetSocketAddress(hostAddress, port);
serverChannel.socket().bind(isa);
serverChannel.register(socketSelector, SelectionKey.OP_ACCEPT);
return socketSelector;
}
private void loop() {
for (;true;) {
try {
selector.select();
Iterator<SelectionKey> selectedKeys = selector.selectedKeys()
.iterator();
while (selectedKeys.hasNext()) {
SelectionKey key = selectedKeys.next();
selectedKeys.remove();
if (!key.isValid()) {
continue;
}
// Check what event is available and deal with it
if (key.isAcceptable()) {
accept(key);
} else if (key.isReadable()) {
read(key);
} else if (key.isWritable()) {
write(key);
}
}
Thread.sleep(1000);
timestamp++;
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
The first question is that, whether the way that I used in order to recognizing online clients (sending specific message every second) is a good approach or not?
If it is good, how can I detect with SelectionKey is related to witch client and then how can I disconnect the key from server?
The first question is that, whether the way that I used in order to recognizing online clients (sending specific message every second) is a good approach or not?
Not in the case of an echo server. In many cases such as this, all you need is to recognize end of stream and connection failure appropriately.
how can I detect with SelectionKey is related to which client
The SelectionKey has a channel, the channel has a socket, and the Socket has a remote IP address:port. That's all you need.
and then how can I disconnect the key from server?
Close the channel when you get -1 from the read() method, or any IOException when reading or writing.
whether the way that I used in order to recognizing online clients (sending specific message every second) is a good approach or not?
Yes, it is called a heartbeat.
how can I detect with SelectionKey is related to witch client and then how can I disconnect the key from server?
You can attach an object which has all the information need regarding a channel. You include this when you register the channel.
Related
I've pasted a server side code snippet below. This server code works under normal circumstances, however, the following scenario manages to break the code.
Server and client are on the same machine. I used the loopback address, and the actual IP address, it makes no difference.
Scenario
Server is online, Client makes request (WritableByteChannel.write(ByteBuffer src) returns 12 byte, which is the correct size, but as research revealed that only means the 12 bytes are written to the TCP buffer).
Server program is turned off. Client notices that the channel is closed on the remote side and closes it on its own side, it doesn't make any requests.
Server is online again.
Client tries to make a request, but fails, because the channel is closed/invalid and can't be reused (even though server is online again).
Client checks server's online status, gets positive result, connects again and immediately makes another request.
Server accepts client (code below), after that processes the if clause with the key.isReadable() condition, but then fails on the read, which indicates end-of-stream.
It would be too complex to create an SSCCE, please comment if important information is missing or this is too abstract and I'll provide further information.
Question
How can a freshly created/accepted channel fail on the read operation?
What am I missing? What steps can I undertake to prevent this?
I already tried wireshark, but I can't capture any packets on the designated TCP port, even if the communication is acutally working.
Problem/Additional Info
It's possible to capture packets into .pcap file with RawCap
The problem was the way the client checked the server status. I've added the method below.
Code snippets
Snippet 1
while (online)
{
if (selector.select(5000) == 0)
continue;
Iterator<SelectionKey> it = selector.selectedKeys().iterator();
while (it.hasNext())
{
SelectionKey key = it.next();
it.remove();
if (key.isAcceptable())
{
log.log(Level.INFO, "Starting ACCEPT!");
ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();
SocketChannel channel = serverSocketChannel.accept();
channel.configureBlocking(false);
channel.register(selector, SelectionKey.OP_READ);
log.log(Level.INFO, "{0} connected to port {1}!",
new Object[] {channel.socket().getInetAddress().getHostAddress(), isa.getPort()});
}
boolean accepted = false;
if (key.isReadable())
{
log.log(Level.INFO, "Starting READ!");
SocketChannel channel = (SocketChannel) key.channel();
bb.clear();
bb.limit(Header.LENGTH);
try
{
NioUtil.read(channel, bb); // server fails here!
}
catch (IOException e)
{
channel.close();
throw e;
}
bb.flip();
Snippet 2
public static ByteBuffer read(ReadableByteChannel channel, ByteBuffer bb) throws IOException
{
while (bb.remaining() > 0)
{
int read = 0;
try
{
read = channel.read(bb);
}
catch (IOException e)
{
log.log(Level.WARNING, "Error during blocking read!", e);
throw e;
}
// this causes the problem... or indicates it
if (read == -1)
{
log.log(Level.WARNING, "Error during blocking read! Reached end of stream!");
throw new ClosedChannelException();
}
}
return bb;
}
Snippet 3
#Override
public boolean isServerOnline()
{
String host = address.getProperty(PropertyKeys.SOCKET_SERVER_HOST);
int port = Integer.parseInt(address.getProperty(PropertyKeys.SOCKET_SERVER_PORT));
boolean _online = true;
try
{
InetSocketAddress addr = new InetSocketAddress(InetAddress.getByName(host), port);
SocketChannel _channel = SocketChannel.open();
_channel.connect(addr);
_channel.close();
}
catch (Exception e)
{
_online = false;
}
return _online;
}
Solution
The problem was not the method that checked, if the service is available/the server is online. The problem was the second point EJP mentioned.
Specific input was expected from the server and it was left in an inconsistent state if that conditions were not met.
I've added some fallback measures, now the the reconnect process - including the check method - is working fine.
Clearly the client must have closed the connection. That's the only way read() returns -1.
Notes:
You're throwing the inappropriate ClosedChannelException when read() returns -1. That exception is thrown by NIO when you've already closed the channel and continue to use it. It has nothing to do with end of stream, and shouldn't be used for that. If you must throw something, throw EOFException.
You also shouldn't loop the way you are. You should only loop while read() is returning a positive number. At present you are starving the select loop while trying to read data that may never arrive.
I am new to NIO i understand the concept of Asynchronous Socket but i am confused on Non Blocking part.
I am using java NIO Selector . My Code for Server is
public class EcoNonBlockingIOServer_7 {
public static int PORT_NUMBER = 5555;
public static void main(String[] argv) throws Exception {
new EcoNonBlockingIOServer_7().go(argv);
}
public void go(String[] argv) throws Exception {
int port = PORT_NUMBER;
if (argv.length > 0) { // Override default listen port
port = Integer.parseInt(argv[0]);
}
System.out.println("Listening on port " + port);
// Allocate an unbound server socket channel
ServerSocketChannel serverChannel = ServerSocketChannel.open();
// Get the associated ServerSocket to bind it with
ServerSocket serverSocket = serverChannel.socket();
// Create a new Selector for use below
Selector selector = Selector.open();
// Set the port the server channel will listen to
serverSocket.bind(new InetSocketAddress(port));
// Set nonblocking mode for the listening socket
serverChannel.configureBlocking(false);
// Register the ServerSocketChannel with the Selector
serverChannel.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
// This may block for a long time. Upon returning, the
// selected set contains keys of the ready channels.
int n = selector.select();
if (n == 0) {
continue; // nothing to do
}
// Get an iterator over the set of selected keys
Iterator it = selector.selectedKeys().iterator();
// Look at each key in the selected set
while (it.hasNext()) {
SelectionKey key = (SelectionKey) it.next();
// Is a new connection coming in?
if (key.isAcceptable()) {
ServerSocketChannel server = (ServerSocketChannel) key.channel();
SocketChannel channel = server.accept();
registerChannel(selector, channel, SelectionKey.OP_READ);
sayHello(channel);
}
// Is there data to read on this channel?
if (key.isReadable()) {
readDataFromSocket(key);
}
// Remove key from selected set; it's been handled
it.remove();
}
}
}
Now My Queries are:
If we register a channel with selector on any operation it always get blocked on selector.select() then how it is non blocking.
If we admit it uses OP_ACCEPT as key and maps channel accordingly but again In key is acceptable i am modifying this channel selector to OP_READ since it already has been accepted. Again It blocks on selector.select() for read event
*Please correct my understanding if i am wrong *
If we register a channel with selector on any operation it always get blocked on selector.select() then how it is non blocking.
select() is blocking. Every operation on a non-blocking channel itself is non-blocking, i.e. read() and write().
If we admit it uses OP_ACCEPT as key and maps channel accordingly but again In key is acceptable i am modifying this channel selector to OP_READ since it already has been accepted.
Very confused. The channel whose interest-ops == OP_ACCEPT is the listening socket. The channel you accepted from the listening socket is a connected socket, and it is this socket that you put into non-blocking mode, register with OP_ACCEPT, etc.
Again It blocks on selector.select() for read event
Correct, but it doesn't block in read() or write() or accept() or finishConnect(). Using a selector is actually called multiplexed I/O: you wait for multiple channels and multiple events at the same time in a single operation.
I am writing a server program that can accept communication from multiple (but fixed) number of clients. I want to keep the program single-threaded. To do so, I am using non-blocking socket to iterate over each client, but each client's channel uses blocking mode. Here's my server code:
class server {
public static void main(String args[])
throws Exception {
ServerSocketChannel channel = ServerSocketChannel.open();
channel.configureBlocking(false);
channel.socket().bind(new java.net.InetSocketAddress("localhost", 8005));
System.out.println("Server attivo porta 8005");
Selector selector = Selector.open();
channel.register(selector, SelectionKey.OP_ACCEPT);
for(;;) {
selector.select();
Set keys = selector.selectedKeys();
Iterator i = keys.iterator();
while(i.hasNext()) {
SelectionKey key = (SelectionKey) i.next();
i.remove();
if (key.isAcceptable()) {
SocketChannel client = channel.accept();
client.configureBlocking(true);
ObjectInputStream ois = new ObjectInputStream(
client.socket().getInputStream());
String s = (String)ois.readObject();
System.out.println(s);
}
}
}
}
}
The client uses simple blocking I/O, as shown here:
class client {
public static void main(String args[]) throws Exception {
SocketChannel channel = SocketChannel.open();
channel.configureBlocking(true);
channel.connect(new java.net.InetSocketAddress("localhost", 8005));
ObjectOutputStream oos = new ObjectOutputStream
(channel.socket().getOutputStream());
for (int i = 0; i < 100; i++) {
oos.writeObject(new String("Hello " + i));
System.out.println(i);
}
}
}
The problem is that although the client wants to write 100 times, the server is reading the message just once. Neither the server nor the client is giving any exception, but I am only getting the output "Hello 0" from the server. Is there any problem in what I am doing here? If so, what alternatives do I have?
Thanks.
Update: Closing the ObjectInputStream within the server's loop gives a BrokenPipeException by the client (the server behaves in the same way).
The issue is that you're just checking for new connections with key.isAcceptable(). You also need to check for reads with key.isReadble(). You should only be doing connection setup from key.isAcceptable().
See Java ServerSocketChannel SocketChannel (Callback)
The problem is that the server is not waiting for the client to send all it's data. In client server programs, what you need to do is to establish a clear protocol between both so that they are in sync when data is transmitted/received. This is usually done by signaling an end of transmission by either side by sending a designated symbol or closing the connection when they are done
This is my code:
channel = DatagramChannel.open();
socket = channel.socket();
channel.configureBlocking(false);
socket.bind(new InetSocketAddress(3000));
selector = Selector.open();
channel.register(selector, SelectionKey.OP_READ);
ByteBuffer buffer = ByteBuffer.allocate(65536);
while(true)
{
if(selector.select()>0)
{
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator iterator = selectionKeys.iterator();
while(iterator.hasNext())
{
SelectionKey key = (SelectionKey)iterator.next();
iterator.remove();
InetSocketAddress isa = (InetSocketAddress) channel.getRemoteAddress();
if(key.isReadable())
{
System.out.print(isa.getAddress().getHostAddress()+":"+isa.getPort());
}
}
}
}
the isa is null.I want to get the DatagramPack SocketAddress like socket.receive(DatagramPack); but i dont know channel how to get it. Use Channel.getSocketAddress() retun Null.
UDP is a connectionless protocol, so you will not be able to find the remote address of the channel, since there is no such thing. Once you open a UDP port for listening, everybody can send you messages, without establishing a direct connection. Every message you receive can potentially come from a different sender.
What you can do is to retrieve the remote address of the message. Check the DatagramChannel.receive() method: it will fill the buffer with the message, and return the address of the sender of that particular message.
Using NIO, we have bound two ports to ServerSocket class.
serverChannelPrimary = ServerSocketChannel.open();
serverChannelSecondary = ServerSocketChannel.open();
// Retrieves a server socket associated with this channel
serverSocketPrimary = serverChannelPrimary.socket();
serverSocketSecondary = serverChannelSecondary.socket();
// Opens a connection selector
connectionSelector = Selector.open();
// Bind the specified port num
serverSocketPrimary.bind(new InetSocketAddress(portOne));
serverSocketSecondary.bind(new InetSocketAddress(portTwo));
// Set nonblocking mode for the listening socket
serverChannelPrimary.configureBlocking(false);
serverChannelSecondary.configureBlocking(false);
// Register the ServerSocketChannel with the Selector
serverChannelPrimary.register(connectionSelector, SelectionKey.OP_ACCEPT);
serverChannelSecondary.register(connectionSelector, SelectionKey.OP_ACCEPT);
Now, we are also able to fetch the IP address of the clients that are connected when the the new client makes the first request, which we are adding to a vector clientIps.
while (isActive) {
try {
numberOfKeys = 0;
numberOfKeys = connectionSelector.select(timeOut);
if (numberOfKeys == 0) {
continue; // None of request available
}
// Get iterator through the selected keys list
Iterator<SelectionKey> iterKeys = connectionSelector
.selectedKeys().iterator();
while (iterKeys.hasNext()) {
try {
SelectionKey selectedKey = (SelectionKey) iterKeys
.next();
// Verify the key validity
if (!selectedKey.isValid()) {
logger.error("Received key is invalid");
continue;
} else if (selectedKey.isAcceptable()) {
// Accept the client request
ServerSocketChannel server = (ServerSocketChannel) selectedKey
.channel();
SocketChannel channel = server.accept();
// Get the socket associated with this channel
Socket clientInfo = channel.socket();
logger.debug("Application got client request from (Host name:"
+ clientInfo.getInetAddress().getHostName()
+ ",Ip address:"
+ clientInfo.getInetAddress()
.getHostAddress()
+ ",port:"
+ clientInfo.getPort());
String clientAddress=clientInfo.getInetAddress().getHostAddress();
if(!clientIps.contains(clientAddress)){
clientIps.add(clientAddress);
}
logger.debug("List of client : "+clientIps);
clientMgr.includeClient(channel);
}
} catch (Exception e) {
logger.error(e.getMessage());
} finally {
logger.debug("Since this key has been handled, remove the SelectedKey from the selector list.");
iterKeys.remove();
}
}
} catch (Exception e) {
logger.error(e.getMessage());
}
}
However, after the connection has been made, once we start getting data from multiple clients on both the ports, is it possible to determine, the IP address of each client whenever each client sends the data. I hope the code that I have provided is sufficient and clear to explain the situation we are having.
ServerSocketChannel is TCP, so the IP addresses at the two ends can't change.
In your line
SocketChannel channel = server.accept();
channel is specific to a particular client. These are the objects you will be using to communicate with each client and each one represents a single TCP session with a single remote ip/port tuple.
You can call SocketChannel.socket().getSocketAddress() to get the remote address of any specific SocketChannel.
Once you get the socketChannel to be able to send back to client, you get use the functions below.
//Not complete example
SocketChannel ssc;
/* after accepting and other such required operations */
ssc.socket().getInetAddress().toString();
/**
Returns:
the remote IP address to which this socket is connected, or null if the socket is not connected.
will return 10.50.10.20 as a string
*/
//To get remote port as an int
ssc.socket().getPort();
I do not see "reading" part of code, but I am sure you have one. You can try to get remote socket address (ip + port) like this:
if (selectionKey.isReadable()) {
SocketChannel client = (SocketChannel) selectionKey.channel();
// you can here read data from given socket; client.read(buffer);
// and also get remote (and local too) address
client.getRemoteAddress();
}