Results 1 to 20 of 22
- 07-22-2010, 02:11 PM #1
Senior Member
- Join Date
- Mar 2010
- Location
- Manila, Philippines
- Posts
- 257
- Rep Power
- 4
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 ForumsLast 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.
- 07-22-2010, 03:17 PM #2
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.
- 07-22-2010, 03:33 PM #3
Senior Member
- Join Date
- Mar 2010
- Location
- Manila, Philippines
- Posts
- 257
- Rep Power
- 4
Every project, package, class, method, variable, syntax, algorithm, etc.
are registered in my memory bank. Thanks to this thread.
- 07-22-2010, 03:43 PM #4
I used the Thread class in my server.
I never had more than 3 clients so I don't know what happens with more.
- 07-22-2010, 04:30 PM #5
Senior Member
- Join Date
- Mar 2010
- Location
- Manila, Philippines
- Posts
- 257
- Rep Power
- 4
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.
- 07-24-2010, 10:46 PM #6
Senior Member
- Join Date
- Mar 2010
- Location
- Manila, Philippines
- Posts
- 257
- Rep Power
- 4
@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.
- 07-24-2010, 10:56 PM #7
The clients were all local (on same computer as server)what computers was connected to the servers
- 07-24-2010, 11:59 PM #8
Senior Member
- Join Date
- Mar 2010
- Location
- Manila, Philippines
- Posts
- 257
- Rep Power
- 4
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.
- 07-25-2010, 12:26 AM #9
Please copy full text of error message and paste it here.I get a NullPointerException from this
- 07-25-2010, 09:56 AM #10
Senior Member
- Join Date
- Mar 2010
- Location
- Manila, Philippines
- Posts
- 257
- Rep Power
- 4
Here's the full message:
And Controller: 53 has this line: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)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.
- 07-25-2010, 12:39 PM #11
What code is at line 45 in Model? What object at that is null?SampleServer.Model.runServer(Model.java:45)
Where is private Computer[] connection;
assigned a value? Something like:
connection = new Computer[10]; // create slots for 10 connections
- 07-25-2010, 01:11 PM #12
Senior Member
- Join Date
- Mar 2010
- Location
- Manila, Philippines
- Posts
- 257
- Rep Power
- 4
Ok...
I have included this code in the constructor.
Now this is strange.Java Code:connection = new Computer[backLog];
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.
- 07-25-2010, 01:19 PM #13
Sounds like you are using Swing's EDT (event dispatch thread) to run the code.Yet my UI freezes when the runServer() is initiated.
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.
- 07-25-2010, 01:50 PM #14
Senior Member
- Join Date
- Mar 2010
- Location
- Manila, Philippines
- Posts
- 257
- Rep Power
- 4
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.
- 07-25-2010, 02:19 PM #15
Is Swing's EDT thread free to run? Is your blocking code on its own thread or on Swing's thread?
- 07-25-2010, 02:32 PM #16
Senior Member
- Join Date
- Mar 2010
- Location
- Manila, Philippines
- Posts
- 257
- Rep Power
- 4
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.
- 07-25-2010, 02:42 PM #17
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 07-25-2010, 02:48 PM #18
Senior Member
- Join Date
- Mar 2010
- Location
- Manila, Philippines
- Posts
- 257
- Rep Power
- 4
- 07-25-2010, 02:51 PM #19
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Why not? Your constructor doesn't initialize that array:
kind regards,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"); }
Jos
- 07-25-2010, 02:54 PM #20
Senior Member
- Join Date
- Mar 2010
- Location
- Manila, Philippines
- Posts
- 257
- Rep Power
- 4
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.
Similar Threads
-
clients cannot connect to server on another machine
By java_muggers in forum NetworkingReplies: 9Last Post: 06-13-2010, 11:22 AM -
how to connect a server to multiple clients?
By azhar in forum NetworkingReplies: 15Last Post: 03-22-2010, 12:54 PM -
multi clients connect to udp server
By bachma7 in forum NetworkingReplies: 5Last Post: 12-12-2008, 08:56 PM -
SSL Server with authentication of clients
By zoltan in forum NetworkingReplies: 0Last Post: 10-14-2008, 11:27 PM -
blocked between server and clients
By ibtehal in forum NetworkingReplies: 6Last Post: 07-17-2008, 12:30 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks