Results 1 to 13 of 13
  1. #1
    Pav
    Pav is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default Client/Server & NIO

    Hi.
    This is my first post - welcome everybody!:)
    My goal is to create a client sending messages and a server which echoes it back.

    Here is Server.java code:

    Java Code:
    public class Server
    {
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        
        public Server(int port) throws IOException
        {
            startServer(port);
        }
        
        public void startServer(int port) throws IOException
        {
            Selector selector = Selector.open();
            ServerSocketChannel ssc = ServerSocketChannel.open();
            ssc.configureBlocking(false);
            ServerSocket ss = ssc.socket();
            InetSocketAddress address = new InetSocketAddress("localhost", port);
            ss.bind(address);
            ssc.register(selector, SelectionKey.OP_ACCEPT);
            for(;;)
            {            
                selector.select();
                Set selectedKeys = selector.selectedKeys();
                Iterator it = selectedKeys.iterator();
                while(it.hasNext())
                {
                    System.out.println("1. Something happens...");
                    SelectionKey key = (SelectionKey)it.next();
                    if(key.isAcceptable())
                    {
                        ServerSocketChannel ssc2 = (ServerSocketChannel)key.channel();
                        SocketChannel sc = ssc2.accept();
                        sc.configureBlocking(false);
                        SelectionKey newKey = sc.register(selector, SelectionKey.OP_READ);
                        it.remove();
                        System.out.println("2. Someone has connected...");
                        continue;
                    }                
                    if(key.isReadable())
                    {
                        System.out.println("3. Ready for reading...");                    
                        SocketChannel sc = (SocketChannel)key.channel();
                        while(true)
                        {                        
                            buffer.clear();                                        
                            int r = sc.read(buffer);
                            if(r <= 0)
                                break;
                            buffer.flip();
                            sc.write(buffer);
                        }
                        it.remove();
                    }                
                }            
            }
        }
        
        public static void main(String[] args)
        {
            try
            {
                new Server(9999);
            } catch (IOException e)
            {
                System.out.println("Server exception: " + e.getMessage());
            }
        }
    }
    And Client.java code:

    Java Code:
    public class Client
    {
        public Client(int port) throws IOException
        {
            SocketChannel socketChannel = SocketChannel.open();
            socketChannel.configureBlocking(false);
            socketChannel.connect(new InetSocketAddress("localhost", port));
            
            try
            {
                Thread.sleep(3000);
            } catch (InterruptedException e)
            {
                e.printStackTrace();
            }
            
            socketChannel.close();            
        }
    
        public static void main(String[] args)
        {
            try
            {
                new Client(9999);
            } catch (IOException e)
            {
                System.out.println("Client exception: " + e.getMessage());
            }
        }
    }

    My problems are following:

    1. When client closes after 3 seconds, server permanently generates such messages:
    (...)
    1. Something happens...
    3. Ready for reading...
    (...)

    How can I disconnect and display (on the server's side) only one message:
    "Client has disconnected" and avoid repeating those above?

    2. Could I have an example of sending message from client to server and vice versa? I've been trying to do this, but some exceptions are generated...

    3. How can I send whole objects through channels (serialization)?

    Thanks in advance for help!;)
    Last edited by Eranga; 05-23-2010 at 02:23 PM. Reason: code tags added

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Welcome to Java Forums. :)

    Please use code tags next time when you posting here in the forum. Unformated codes are really hard to read. If you don't know how do it, please check on my forum signature. You can find a link to the relevant page.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Pav View Post
    1. When client closes after 3 seconds, server permanently generates such messages:
    (...)
    1. Something happens...
    3. Ready for reading...
    (...)

    How can I disconnect and display (on the server's side) only one message:
    "Client has disconnected" and avoid repeating those above?
    Think about this, once a client disconnect how can you find out that from the server side? Do you have a logic in your implementation? Seems to me that your server doesn't know that client is disconnected.

  4. #4
    Pav
    Pav is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga
    Think about this, once a client disconnect how can you find out that from the server side? Do you have a logic in your implementation? Seems to me that your server doesn't know that client is disconnected.
    I thought that there is implemented some mechanism which informs server about disconnection (socketChannel.close();)... How can I let the server know about this event then? Shall I send a special message? As I wrote above, I don't know how to write information properly into the channel...

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Actually those things are depends on with the implementations.

    For an example you can design something like this. Keep track all the connected clients, say in a map. Within a predefined times if one of the client not send a request to the server, means that the client is disconnected from the system. In map you've to record the last request time for each client.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    If you want to you can do some implementation on the client side as well. For an example if 1000 clients are connect to the server, to check that all are connect, server has to send 1000 request to clients. Obviously that's not a good practice. So what you can do is, that connection validation part can be handle from the client side as well, up to some extend.

  7. #7
    Pav
    Pav is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default

    I've managed with disconnection problem with this simple way:

    Java Code:
    //Server.java snippet
    if(key.isReadable())
    {
        System.out.println("3. Ready for reading...");                    
        SocketChannel sc = (SocketChannel)key.channel();
        while(true)
        {                        
            buffer.clear();                                        
            int r = sc.read(buffer);
            if(r <= 0)
    		{
    			sc.close(); //here is amendment
    			break;
    		}
            buffer.flip();
            sc.write(buffer);
        }
        it.remove();
    }       
    //...
    Moreover, I realised that my client isn't able to connect with server:

    Java Code:
    SocketChannel socketChannel = SocketChannel.open();
    socketChannel.configureBlocking(false);
    socketChannel.connect(new InetSocketAddress("localhost", port)); //port is set to 9999
    
    while(!socketChannel.isConnected())
    {
    	System.out.println("Not connected");
    }
    Client falls into infinite loop... Why?

  8. #8
    Pav
    Pav is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default

    I solved this problem by deleting such a line from a client application:
    socketChannel.configureBlocking(false);

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Pav View Post
    I solved this problem by deleting such a line from a client application:
    socketChannel.configureBlocking(false);
    Glad to here that. So please mark the thread solved.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    And also I hope that you really aware whats really going on there. I mean the reason to delete it.

  11. #11
    Pav
    Pav is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default

    Not really, but it's the only way to run my system I've found:)

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Replies: 3
    Last Post: 07-29-2009, 04:52 AM
  2. Client server
    By yash in forum Networking
    Replies: 9
    Last Post: 01-04-2009, 04:33 PM
  3. client-server app
    By shwein in forum Networking
    Replies: 3
    Last Post: 10-14-2008, 05:20 PM
  4. Replies: 1
    Last Post: 10-05-2008, 03:12 PM
  5. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 09:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •