Results 1 to 3 of 3
  1. #1
    Noelf21 is offline Member
    Join Date
    Dec 2009
    Posts
    12
    Rep Power
    0

    Default MultiThreading Chat Program

    Hi i am creating a java chat application i have a client and a server however the chat application has to host many clients, i was just wondering where do i need to insert the multithreading code and what code do i need to use for this to work in the following code.

    Server

    public class ChatServer extends javax.swing.JFrame {
    private ServerSocket server;
    private Socket connection;
    int counter = 1;
    private ObjectInputStream input;
    private ObjectOutputStream output;

    /** Creates new form ChatServer */
    public ChatServer() {
    initComponents();
    jTextField1.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event){
    sendData(event.getActionCommand());
    jTextField1.setText("");
    }
    }
    );
    // runServer();
    }
    public void runServer(){
    try
    {
    server = new ServerSocket( 12345, 2 ); // set up ServerSocket
    while (true){
    try{
    waitForConnection();
    getStreams();
    processConnection();
    }//end try
    catch (EOFException eofException)
    {
    displayMessage("\nServer terminated connection");
    }
    finally
    {
    closeConnection();
    counter++;
    }
    }
    } // end try
    catch ( IOException ioException )
    {
    ioException.printStackTrace();
    } // end catch

    //jTextArea1.setText( "Server awaiting connections\n" );


    }
    private void waitForConnection() throws IOException{
    displayMessage("Server awaiting connections\n");
    connection= server.accept();
    displayMessage("Connection" +counter+"received from: " + connection.getInetAddress().getHostName());
    }
    private void getStreams() throws IOException{
    output = new ObjectOutputStream(connection.getOutputStream());
    output.flush();
    input = new ObjectInputStream(connection.getInputStream());
    displayMessage("\nGot I/O streams\n");
    }
    private void processConnection() throws IOException{
    String message = "Connection Successful";
    sendData(message);
    do
    {
    try{
    message = (String)input.readObject();
    displayMessage("\n"+message);
    } catch(ClassNotFoundException cnfException){
    displayMessage("\nunknown object type");
    }


    }while (!message.equals("Terminate"));
    }
    private void closeConnection(){
    displayMessage("\nterminating Connection");
    try{
    output.close();
    input.close();
    connection.close();
    }catch(IOException ioException){
    ioException.printStackTrace();
    }
    }
    private void sendData(String message){
    try{
    output.writeObject("Server: "+message);
    output.flush();
    displayMessage("\nServer>>>"+message);
    }catch(IOException ioException){
    jTextArea1.append("\nError writing object");
    }
    }
    private void displayMessage(final String messageToDisplay){
    SwingUtilities.invokeLater(new Runnable(){
    public void run(){
    jTextArea1.append(messageToDisplay);
    }
    });
    }
    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">


    public static void main(String args[]) {
    ChatServer s= new ChatServer();
    s.setVisible(true);
    s.runServer();
    /* java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    // new ChatServer().setVisible(true);


    }
    });*/
    }



    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();
    jTextField1 = new javax.swing.JTextField();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
    setTitle("Server");

    jLabel1.setText("Enter Your Message Here:");

    jTextArea1.setColumns(20);
    jTextArea1.setRows(5);
    jScrollPane1.setViewportView(jTextArea1);

    jTextField1.setText("jTextField1");

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(org.jdesktop.layout.Gro upLayout.LEADING)
    .add(layout.createSequentialGroup()
    .add(layout.createParallelGroup(org.jdesktop.layou t.GroupLayout.LEADING)
    .add(layout.createSequentialGroup()
    .addContainerGap()
    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
    .add(layout.createSequentialGroup()
    .add(18, 18, 18)
    .add(layout.createParallelGroup(org.jdesktop.layou t.GroupLayout.LEADING)
    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 352, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    .add(jLabel1))))
    .addContainerGap())
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(org.jdesktop.layout.Gro upLayout.LEADING)
    .add(layout.createSequentialGroup()
    .addContainerGap()
    .add(jLabel1)
    .add(18, 18, 18)
    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 35, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    .add(45, 45, 45)
    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
    .addContainerGap())
    );

    pack();
    }// </editor-fold>

  2. #2
    Atriamax is offline Member
    Join Date
    Sep 2009
    Posts
    41
    Rep Power
    0

  3. #3
    renamed is offline Member
    Join Date
    Dec 2009
    Location
    Rio de Janeiro
    Posts
    38
    Rep Power
    0

    Default

    In server you will have a thread that will be waiting for new connections and a several threads... each of them will be responsible for one client.
    Please don't laugh at my English... I'm trying my best! :)

Similar Threads

  1. Multithreaded Client/Server Chat program
    By f0ns in forum Threads and Synchronization
    Replies: 3
    Last Post: 10-21-2009, 05:26 PM
  2. Help with chat client program
    By srivigneshwar in forum New To Java
    Replies: 1
    Last Post: 04-03-2009, 06:32 PM
  3. simple chat program
    By munishmhr in forum Networking
    Replies: 2
    Last Post: 03-25-2009, 04:00 PM
  4. Develop chat program using SCTP protocol
    By rams in forum Networking
    Replies: 0
    Last Post: 11-03-2008, 05:07 AM
  5. Java Program chat
    By susan in forum Advanced Java
    Replies: 1
    Last Post: 07-25-2007, 09:05 PM

Posting Permissions

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