Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Exclamation How to connect n clients to a server?

    Greetings,

    I have encoded the program of client/server chat using ObjectStream - [Output/Input].
    Running both client and server went well, both can communicate.
    But when I open another client, the another client cannot connect to the server.

    What pseudocode I am to follow to enable that another client to communicate to the server without closing the connection between the first client?

    Thanks

    Note: Thread cross-posted on Oracle Forums
    Last edited by chyrl; 07-22-2010 at 02:53 PM.
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    In the server, have a loop that waits for a connection.
    When it gets a connection, create a thread to process that connection and loop back to wait for the next connection. There will be one thread for each connection.
    At some point you may need to set a limit on the number of threads running.

  3. #3
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Exclamation

    Quote Originally Posted by Norm View Post
    In the server, have a loop that waits for a connection.
    The server has a loop open for new connections.

    Quote Originally Posted by Norm View Post
    When it gets a connection, create a thread to process that connection and loop back to wait for the next connection. There will be one thread for each connection.
    Am I to use the Thread class? (Are there other like SwingWorker?)

    Quote Originally Posted by Norm View Post
    At some point you may need to set a limit on the number of threads running.
    Isn't that the jobs of the backLog of the ServerSocket class?
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    I used the Thread class in my server.
    I never had more than 3 clients so I don't know what happens with more.

  5. #5
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    Ok I'll try that logic.
    Anyways, what the pseudocode inside the thread? May I know?
    Last edited by chyrl; 07-22-2010 at 04:34 PM.
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  6. #6
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    @Norm:
    Did you take a notice to what computers was connected to the servers?
    Like the os.name property?
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    what computers was connected to the servers
    The clients were all local (on same computer as server)

  8. #8
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Exclamation

    Quote Originally Posted by Norm View Post
    The clients were all local (on same computer as server)
    Strange, I can't understand well the concept of Threading the Sockets.
    I have tried yet I get a NullPointerException from this

    Java Code:
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    
    import java.net.ServerSocket;
    import java.net.Socket;
    
    import javax.swing.JTextArea;
    import javax.swing.JComboBox;
    import javax.swing.DefaultComboBoxModel;
    
    public class Model {
    
        private int port;
        private int backLog;
        private JTextArea chatArea;
        private JComboBox computers;
        private ServerSocket server;
        private Computer[] connection;
    
        public Model(int port, int backLog, JTextArea chatArea, JComboBox computers) {
            this.port = port;
            this.backLog = backLog;
            this.chatArea = chatArea;
            this.computers = computers;
            display("Awaiting connections...\n");
        }
    
        public void runServer() {
            try {
                server = new ServerSocket(port, backLog);
    
                for(int i=0; i<connection.length; i++) {
                    connection[i] = new Computer(server.accept());
                    connection[i].run();
                }
    
            } catch(IOException e) {
                e.printStackTrace();
            }
        }
    
        public void closeServer() {
            try {
                server.close();
                for(int i=0; i<connection.length; i++) {
                    connection[i].close();
                }
            } catch(IOException e) {
                e.printStackTrace();
            }
        }
    
        private void display(String message) {
                chatArea.append(message);
        }
    
        private class Computer implements Runnable {
    
            private Socket socket;
            private ObjectInputStream input;
            private ObjectOutputStream output;
            private String computerName = "";
    
            public Computer(Socket socket) {
                this.socket = socket;
            }
    
            public void close() throws IOException {
                socket.close();
                input.close();
                output.close();
            }
    
            public void run() {
                try {
                    getStreams();
                } catch(IOException e) {
                    e.printStackTrace();
                }
            }
    
            private void getStreams() throws IOException {
                output = new ObjectOutputStream(socket.getOutputStream());
                output.flush();
                input = new ObjectInputStream(socket.getInputStream());
                display("Got I/O from " + socket.getInetAddress().getHostName());
            }
    
            
        }
    }
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    I get a NullPointerException from this
    Please copy full text of error message and paste it here.

  10. #10
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Exclamation

    Quote Originally Posted by Norm View Post
    Please copy full text of error message and paste it here.
    Here's the full message:

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
            at SampleServer.Model.runServer(Model.java:45)
            at SampleServer.Controller.actionPerformed(Controller.java:53)
            at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
            at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
            at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
            at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
            at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
            at java.awt.Component.processMouseEvent(Component.java:6099)
            at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
            at java.awt.Component.processEvent(Component.java:5864)
            at java.awt.Container.processEvent(Container.java:2058)
            at java.awt.Component.dispatchEventImpl(Component.java:4466)
            at java.awt.Container.dispatchEventImpl(Container.java:2116)
            at java.awt.Component.dispatchEvent(Component.java:4296)
            at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
            at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
            at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
            at java.awt.Container.dispatchEventImpl(Container.java:2102)
            at java.awt.Window.dispatchEventImpl(Window.java:2454)
            at java.awt.Component.dispatchEvent(Component.java:4296)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:284)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    And Controller: 53 has this line:
    Java Code:
    model.runServer();
    Last edited by chyrl; 07-25-2010 at 10:13 AM.
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    SampleServer.Model.runServer(Model.java:45)
    What code is at line 45 in Model? What object at that is null?

    Where is private Computer[] connection;
    assigned a value? Something like:
    connection = new Computer[10]; // create slots for 10 connections

  12. #12
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    Ok...
    I have included this code in the constructor.
    Java Code:
    connection = new Computer[backLog];
    Now this is strange.
    I have configured the problem. Yet my UI freezes when the runServer() is initiated.
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    Yet my UI freezes when the runServer() is initiated.
    Sounds like you are using Swing's EDT (event dispatch thread) to run the code.
    You should start your own thread from Swing's thread to allow the EDT to continue servicing the GUI stuff.

    To see where the code is "freezing" add some println() statements to show where the code is executing and where it hasn't gotten to yet.

  14. #14
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    Strange.
    Now I can't solve this problem.
    Though the server accepts now new connections.

    The GUI still freezes at runServer().
    How could I fix it? Can you give me hints?
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    Is Swing's EDT thread free to run? Is your blocking code on its own thread or on Swing's thread?

  16. #16
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Exclamation

    Quote Originally Posted by Norm View Post
    Is Swing's EDT thread free to run? Is your blocking code on its own thread or on Swing's thread?
    I can't understand what that EDT means.
    But here's my codes on this link.
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by chyrl View Post
    I can't understand what that EDT means.
    But here's my codes on this link.
    Your 'connection' array isn't initialized so it is null.

    kind regards,

    Jos

  18. #18
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Exclamation

    Quote Originally Posted by JosAH View Post
    Your 'connection' array isn't initialized so it is null.

    kind regards,

    Jos
    Yes, but when the constructor is called.
    It can't be null anymore.
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  19. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by chyrl View Post
    Yes, but when the constructor is called.
    It can't be null anymore.
    Why not? Your constructor doesn't initialize that array:

    Java Code:
    public Model(int port, int backLog, JTextArea chatArea, JComboBox computers) {
            this.port = port;
            this.backLog = backLog;
            this.chatArea = chatArea;
            this.computers = computers;
            display("Awaiting connections...\n");
    }
    kind regards,

    Jos

  20. #20
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Exclamation

    Quote Originally Posted by JosAH View Post
    Why not? Your constructor doesn't initialize that array:

    Java Code:
    public Model(int port, int backLog, JTextArea chatArea, JComboBox computers) {
            this.port = port;
            this.backLog = backLog;
            this.chatArea = chatArea;
            this.computers = computers;
            display("Awaiting connections...\n");
    }
    kind regards,

    Jos
    Kindly refer now to the update Model.java
    Here's the link.
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

Page 1 of 2 12 LastLast

Similar Threads

  1. clients cannot connect to server on another machine
    By java_muggers in forum Networking
    Replies: 9
    Last Post: 06-13-2010, 11:22 AM
  2. how to connect a server to multiple clients?
    By azhar in forum Networking
    Replies: 15
    Last Post: 03-22-2010, 12:54 PM
  3. multi clients connect to udp server
    By bachma7 in forum Networking
    Replies: 5
    Last Post: 12-12-2008, 08:56 PM
  4. SSL Server with authentication of clients
    By zoltan in forum Networking
    Replies: 0
    Last Post: 10-14-2008, 11:27 PM
  5. blocked between server and clients
    By ibtehal in forum Networking
    Replies: 6
    Last Post: 07-17-2008, 12:30 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
  •