Simple chat problem - really stuck! Please help me :o)

From: Ali (AyCeeUK_at_hotmail.com)
Date: 02/22/04

  • Next message: Daniel Pope: "Accessing Microsoft Outlook from a Java applet"
    Date: 22 Feb 2004 08:18:54 -0800
    
    

    I'm trying to write a really basic chat program that allows 2 client
    programs to send messages to each other via a server. I've managed to
    write the code so that both clients can connect to the server and send
    and recieve messages to/from the server but when I send a message from
    one client I can't get the message to appear in the second client. I
    think it's something to do with threads but I don't know what! I've
    been looking at the source code for loads of different chat programs
    online but they all seem far too complicated and I can't figure out
    what I'm doing wrong.

    Can anyone give me any pointers as to what I need to do? Any help will
    be very gratefully received.

    This is the code I have so far...

    ThreadedChatServer.java

    import java.io.*;
    import java.net.*;

    public class ThreadedChatServer
    {

      public static void main(String[] args) throws IOException
      {
           ServerSocket server = null;
           boolean listening = true;
            
     try
     {
       server = new ServerSocket(8000);
     }
     catch(IOException e)
     {
       System.out.println("Error connecting " + e);
     }
       
     while (listening)
     {
       new ChatHandler(server.accept()).start();
     }
       
     server.close();
     }
    }
    **************

    ChatHandler.java

    import java.io.*;
    import java.net.*;
    import javax.swing.*;

    public class ChatHandler extends Thread
    {
      private Socket connectToClient = null;
      private BufferedReader in;
      private PrintWriter out;
      private String message = "";
      private String reply = "";
     
      public ChatHandler(Socket connectToClient)
      {
        this.connectToClient = connectToClient;
      }

      public void run()
      {
        try
        {
          in = new BufferedReader(new
    InputStreamReader(connectToClient.getInputStream()));
          out = new PrintWriter(connectToClient.getOutputStream(),true);
        
          // A variable that will say if the chat is over //
          boolean finished = false;

          while (!finished)
          {
            try
            {
       message = in.readLine();
              out.println(message);
       Thread.sleep(100);
     }
     catch(IOException e)
     {
       System.out.println("Error connecting " + e);
     }
     catch(InterruptedException i)
     {
       System.out.println("Thread interrupted " + i);
     }
          }

          try
          {
     in.close();
     out.close();
     connectToClient.close();
          }
          catch(IOException e)
          {
     System.out.println("Error connecting " + e);
          }
        }
        catch(IOException e)
        {
          System.out.println("Error connecting " + e);
        }
      }
    }
    ******************

    ChatClient.java

    ... code for text areas etc...
    ...press send message button then ...

    // Sends the message to the server //
       out.println(userName + "> " + messageField.getText());
         
    // Prints out the server's reply //
       chatHistory.append("\n" + in.readLine());
         
    // Clears the message field //
       messageField.setText("");

    ... etc ...

    Thanks,
    AyCee :o)


  • Next message: Daniel Pope: "Accessing Microsoft Outlook from a Java applet"