1 2 Previous Next 16 Replies Latest reply: Feb 24, 2010 12:23 AM by EJP RSS

    Chat Multi Client Server  programm On LAN

    843789
      I am currently working on multi Client -Server Chat programming,I am new with java programming.
      I have 4 files in this project1. ChatServer,2.ChatClient, 3.ChatServerThread, 4.ChatClientThread.
      My all programms runns on console but in Chatserver , when it running that time it not shows any message on console,which is printed in the program.In the ChatClient when it runns using appletviewer command it runns applet but , there is only connect butoon is working other two buttons Bye & Send not working, So please give me solutionn for this . & tell me what is the requirement fir this programms.
      Followings are my code :
      import java.net.*;
      import java.io.*;
       
      public class ChatServer implements Runnable
      { 
       
         private ChatServerThread clients[] = new ChatServerThread[50];
         private ServerSocket server = null;
         private Thread       thread = null;
         private int clientCount = 0;
       
         public ChatServer(int port)
         {  
       
       
           try
            { 
              System.out.println("Binding to port " + port + ", please wait  ...");
               server = new ServerSocket(port);  
               System.out.println("Server started: " + server);
               start();
               }
            catch(IOException ioe)
              {  
               System.out.println("Can not bind to port " + port + ": " + ioe.getMessage()); }
              }
         public void run()
         {  
           while (thread != null)
            {  
              try
               {  
                 System.out.println("Waiting for a client ..."); 
                  addThread(server.accept());
                  }
               catch(IOException ioe)
                 {
                  System.out.println("Server accept error: " + ioe); stop(); }
               }
            }
         public void start()  
           { 
            if (thread == null)
             { 
              
              thread = new Thread(this); 
               thread.start();
            }
           }
         public void stop()  
            { 
           if (thread != null)
             { 
               thread.stop(); 
               thread = null;
            }
           }
         private int findClient(int ID)
           {  
            for (int i = 0; i < clientCount; i++)
               if (clients.getID() == ID)
      return i;
      return -1;
      }
      public synchronized void handle(int ID, String input)

      if (input.equals(".bye"))
      {
      clients[findClient(ID)].send(".bye");
      remove(ID);
      }
      else
      for (int i = 0; i < clientCount; i++)
      clients[i].send(ID + ": " + input);
      }
      public synchronized void remove(int ID)

      int pos = findClient(ID);
      if (pos >= 0)
      {
      ChatServerThread toTerminate = clients[pos];
      System.out.println("Removing client thread " + ID + " at " + pos);
      if (pos < clientCount-1)
      for (int i = pos+1; i < clientCount; i++)
      clients[i-1] = clients[i];
      clientCount--;
      try
      {
      toTerminate.close();
      }
      catch(IOException ioe)
      {  
      System.out.println("Error closing thread: " + ioe); }
      toTerminate.stop();
      }
      }
      private void addThread(Socket socket)

      if (clientCount < clients.length)

      System.out.println("Client accepted: " + socket);
      clients[clientCount] = new ChatServerThread(this, socket);
      try

      {
      clients[clientCount].open();
      clients[clientCount].start();
      clientCount++;
      }
      catch(IOException ioe)
      {
      System.out.println("Error opening thread: " + ioe);
      }
      }
      else
      System.out.println("Client refused: maximum " + clients.length + " reached.");
      }
      public static void main(String args[])
      {
      /* ChatServer server = null;
      if (args.length != 1)
      System.out.println("Usage: java ChatServer port");
      else
      server = new ChatServer(Integer.parseInt(args[0])); */
      }
      }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
        • 1. Re: Chat Multi Client Server  programm On LAN
          DrClap
          That code doesn't do anything because all of the code in your "main" method is commented out.
          • 2. Re: Chat Multi Client Server  programm On LAN
            843789
            But if its comments removed then it prints :
            Usage: java ChatServer port
            no any other thing is happen.
            • 3. Re: Chat Multi Client Server  programm On LAN
              801447
              .. did you try
              java ChatServer 1234
              • 4. Re: Chat Multi Client Server  programm On LAN
                843789
                If you are saying that use
                ChatServer(int port)
                like
                {codeChatServer(1234)}
                then its not work & it gives error.                                                                                                                                                                                                                                                                        
                • 5. Re: Chat Multi Client Server  programm On LAN
                  3004
                  nktprashant wrote:
                  If you are saying that use
                  ChatServer(int port)
                  like
                  {codeChatServer(1234)}
                  then its not work & it gives error.
                  Did it occur to you to maybe provide more details than just "it gives error"? Like pasting in the exact complete error message, and indicating clearly which line is causing it?

                  Just wondering.
                  • 6. Re: Chat Multi Client Server  programm On LAN
                    843789
                    after compiling ChatServer.java
                    It gives following errors:
                    C:\j2sdk1.4.1\bin>javac ChatServer.java
                    ChatServer.java:10: illegal start of type
                    public ChatServer(1234)
                    ^
                    ChatServer.java:118: <identifier> expected
                    }
                    ^
                    ChatServer.java:4: ChatServer should be declared abstract; it does not defi
                    n() in ChatServer
                    public class ChatServer implements Runnable
                    ^
                    ChatServer.java:10: missing method body, or declare abstract
                    public ChatServer(1234)
                    ^
                    .\ChatServerThread.java:24: cannot resolve symbol
                    symbol : method remove (int)
                    location: class ChatServer
                    server.remove(ID);
                    ^
                    .\ChatServerThread.java:35: cannot resolve symbol
                    symbol : method handle (int,java.lang.String)
                    location: class ChatServer
                    {  server.handle(ID, streamIn.readUTF());
                    ^
                    .\ChatServerThread.java:39: cannot resolve symbol
                    symbol : method remove (int)
                    location: class ChatServer
                    server.remove(ID);
                    ^
                    Note: .\ChatServerThread.java uses or overrides a deprecated API.
                    Note: Recompile with -deprecation for details.
                    7 errors

                    C:\j2sdk1.4.1\bin>
                    • 7. Re: Chat Multi Client Server  programm On LAN
                      EJP
                      So how did you get the output you reported in [reply #2|http://forums.sun.com/thread.jspa?messageID=10929748#10929748] when you can't even compile it in reply #6?

                      And the code that won't compile in reply 6 certainly isn't the code you posted originally.
                      • 8. Re: Chat Multi Client Server  programm On LAN
                        843789
                        it gives error like :

                        C:\j2sdk1.4.1\bin>javac ChatServer.java
                        ChatServer.java:10: illegal start of type
                        public ChatServer(1234)
                        ^
                        ChatServer.java:119: <identifier> expected
                        }
                        ^
                        ChatServer.java:4: ChatServer should be declared abstract; it
                        n() in ChatServer
                        public class ChatServer implements Runnable
                        ^
                        ChatServer.java:10: missing method body, or declare abstract
                        public ChatServer(1234)
                        ^
                        4 errors
                        • 9. This Thread is now moved
                          DarrylBurke
                          Note: This thread was originally posted in the [Java Programming|http://forums.sun.com/forum.jspa?forumID=31] forum, but moved to this forum for closer topic alignment.
                          • 10. Re: Chat Multi Client Server  programm On LAN
                            EJP
                            Don't just mindlessly repost the same information thanks, it's just a waste of everybody's time. I don't care about your time but mine is valuable.

                            Answer the questions.
                            • 11. Re: Chat Multi Client Server  programm On LAN
                              EJP
                              If you are saying that ...
                              No that's not what he said at all. Read the reply again and act on it and stop fiddling around with source code you don't understand.
                              • 12. Re: This Thread is now moved
                                843789
                                I dont get meaning what you are saying, In my code someone make change without my permission on my pc.
                                Bcoz
                                when first I compile may program that time it gives error :
                                "Usage: java ChatServer port"
                                & now the code is
                                ChatServer(int port)
                                then it gives errors accept() method
                                error are:
                                C:\j2sdk1.4.1\bin>javac ChatServer.java
                                ChatServer.java:23: cannot resolve symbol
                                symbol : method accept ()
                                location: class ServerSocket
                                addThread(server.accept()); }
                                ^
                                Note: ChatServer.java uses or overrides a deprecated API.
                                Note: Recompile with -deprecation for details.
                                1 error
                                • 13. Re: This Thread is now moved
                                  EJP
                                  I dont get meaning what you are saying
                                  I asked how did you run the code in reply #2 when you can't compile it in reply #6? The question is perfectly clear.
                                  In my code someone make change without my permission on my pc.
                                  If that's true, which I take leave to doubt, no forum can help you.
                                  Bcoz
                                  Eh?
                                  when first I compile may program that time it gives error :
                                  "Usage: java ChatServer port"
                                  That's not compiling. That's executing. You can't execute a program until you've compiled it. So why won't it compile now? Have you changed it? If so why? To what? What was wrong with the program as at reply #2? apart from you not knowing how to run it? which has already been answered?

                                  Is this for real?
                                  • 14. Re: This Thread is now moved
                                    793415
                                    nktprashant wrote:
                                    ..Bcoz
                                    As far as I can guess, that word is supposed to be 'because'. Please take the effort to [write well|http://catb.org/~esr/faqs/smart-questions.html#writewell]. If you fail to do so, it will most likely result that the people who are having trouble understanding your post, will stop trying to understand it, and move on to the post of other people who take more care with what they write.
                                    1 2 Previous Next