Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: 1 Server- Multiple Clients Program

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 1 Server- Multiple Clients Program

    I have a 1 server, multiple clients program. I can't get it to run. I keep getting error:
    3 warnings found:
    File: C:\Users\Pa\Desktop\MultiThreadChatClient.java [line: 80]
    Warning: The method readLine() from the type java.io.DataInputStream is deprecated
    File: C:\Users\Pa\Desktop\MultiThreadChatServer.java [line: 92]
    Warning: The method readLine() from the type java.io.DataInputStream is deprecated
    File: C:\Users\Pa\Desktop\MultiThreadChatServer.java [line: 98]
    Warning: The method readLine() from the type java.io.DataInputStream is deprecated


    Any Help would be much appreciated.

    import java.io.*;
    import java.net.*;
     
    public class MultiThreadChatClient implements Runnable{
     
        // Declaration section
        // clientClient: the client socket
        // os: the output stream
        // is: the input stream
     
        static Socket clientSocket = null;
        static PrintStream os = null;
        static DataInputStream is = null;
        static BufferedReader inputLine = null;
        static boolean closed = false;
     
        public static void main(String[] args) {
     
     // The default port 
     
     int port_number=2222;
            String host="localhost";
     
     if (args.length < 2)
         {
      System.out.println("Usage: java MultiThreadChatClient  \n"+
           "Now using host="+host+", port_number="+port_number);
         } else {
      host=args[0];
      port_number=Integer.valueOf(args[1]).intValue();
         }
     // Initialization section:
     // Try to open a socket on a given host and port
     // Try to open input and output streams
     try {
                clientSocket = new Socket(host, port_number);
                inputLine = new BufferedReader(new InputStreamReader(System.in));
                os = new PrintStream(clientSocket.getOutputStream());
                is = new DataInputStream(clientSocket.getInputStream());
            } catch (UnknownHostException e) {
                System.err.println("Don't know about host "+host);
            } catch (IOException e) {
                System.err.println("Couldn't get I/O for the connection to the host "+host);
            }
     
     // If everything has been initialized then we want to write some data
     // to the socket we have opened a connection to on port port_number 
     
            if (clientSocket != null && os != null && is != null) {
                try {
     
      // Create a thread to read from the server
     
                    new Thread(new MultiThreadChatClient()).start();
     
      while (!closed) {
                        os.println(inputLine.readLine()); 
                    }
     
      // Clean up:
      // close the output stream
      // close the input stream
      // close the socket
     
      os.close();
      is.close();
      clientSocket.close();   
                } catch (IOException e) {
                    System.err.println("IOException:  " + e);
                }
            }
        }           
     
        public void run() {  
     String responseLine;
     
     // Keep on reading from the socket till we receive the "Bye" from the server,
     // once we received that then we want to break.
     try{ 
         while ((responseLine = is.readLine()) != null) {
      System.out.println(responseLine);
      if (responseLine.indexOf("*** Bye") != -1) break;
         }
                closed=true;
     } catch (IOException e) {
         System.err.println("IOException:  " + e);
     }
        }
    }


    import java.io.*;
    import java.net.*;
     
    public class MultiThreadChatServer{
     
        // Declaration section:
        // declare a server socket and a client socket for the server
        // declare an input and an output stream
     
        static  Socket clientSocket = null;
        static  ServerSocket serverSocket = null;
     
        // This chat server can accept up to 10 clients' connections
     
        static  clientThread t[] = new clientThread[10];           
     
        public static void main(String args[]) {
     
     // The default port
     
     int port_number=2222;
     
     if (args.length < 1)
         {
      System.out.println("Usage: java MultiThreadChatServer \n"+
           "Now using port number="+port_number);
         } else {
      port_number=Integer.valueOf(args[0]).intValue();
         }
     
     // Initialization section:
     // Try to open a server socket on port port_number (default 2222)
     // Note that we can't choose a port less than 1023 if we are not
     // privileged users (root)
     
            try {
         serverSocket = new ServerSocket(port_number);
            }
            catch (IOException e)
         {System.out.println(e);}
     
     // Create a socket object from the ServerSocket to listen and accept 
     // connections.
     // Open input and output streams for this socket will be created in 
     // client's thread since every client is served by the server in
     // an individual thread
     
     while(true){
         try {
      clientSocket = serverSocket.accept();
      for(int i=0; i<=9; i++){
          if(t[i]==null)
       {
           (t[i] = new clientThread(clientSocket,t)).start();
           break;
       }
      }
         }
         catch (IOException e) {
      System.out.println(e);}
     }
        }
    } 
     
    // This client thread opens the input and the output streams for a particular client,
    // ask the client's name, informs all the clients currently connected to the 
    // server about the fact that a new client has joined the chat room, 
    // and as long as it receive data, echos that data back to all other clients.
    // When the client leaves the chat room this thread informs also all the
    // clients about that and terminates. 
     
    class clientThread extends Thread{
     
        DataInputStream is = null;
        PrintStream os = null;
        Socket clientSocket = null;       
        clientThread t[]; 
     
        public clientThread(Socket clientSocket, clientThread[] t){
     this.clientSocket=clientSocket;
            this.t=t;
        }
     
        public void run() 
        {
     String line;
            String name;
     try{
         is = new DataInputStream(clientSocket.getInputStream());
         os = new PrintStream(clientSocket.getOutputStream());
         os.println("Enter your name.");
         name = is.readLine();
         os.println("Hello "+name+" to our chat room.\nTo leave enter /quit in a new line"); 
         for(int i=0; i<=9; i++)
      if (t[i]!=null && t[i]!=this)  
          t[i].os.println("*** A new user "+name+" entered the chat room !!! ***" );
         while (true) {
      line = is.readLine();
                    if(line.startsWith("/quit")) break; 
      for(int i=0; i<=9; i++)
          if (t[i]!=null)  t[i].os.println("<"+name+"> "+line); 
         }
         for(int i=0; i<=9; i++)
      if (t[i]!=null && t[i]!=this)  
          t[i].os.println("*** The user "+name+" is leaving the chat room !!! ***" );
     
         os.println("*** Bye "+name+" ***"); 
     
         // Clean up:
         // Set to null the current thread variable such that other client could
         // be accepted by the server
     
         for(int i=0; i<=9; i++)
      if (t[i]==this) t[i]=null;  
     
         // close the output stream
         // close the input stream
         // close the socket
     
         is.close();
         os.close();
         clientSocket.close();
     }
     catch(IOException e){};
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 1 Server- Multiple Clients Program

    The method readLine() from the type java.io.DataInputStream is deprecated
    You need to read the API doc for the DataInputStream class's readLine method. There are comments there on the recommended classes and methods to use.
    Go here
    Java Platform SE 6
    and find a link to the class's doc in the lower left

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 1 Server- Multiple Clients Program

    Thank you so much.

Similar Threads

  1. Java TCP multiple clients
    By amjoseph in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 28th, 2012, 04:39 PM
  2. Server/clients connection periodically ask for a file
    By akis3110 in forum Java Theory & Questions
    Replies: 2
    Last Post: June 28th, 2011, 08:06 AM
  3. Transmitting from server to all clients.
    By newbie in forum Java Networking
    Replies: 0
    Last Post: December 19th, 2010, 05:07 PM
  4. Grade averaging program with array and multiple methods.
    By jeremykatz in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 9th, 2009, 09:44 PM
  5. How to setup a server-multiple- clientenvironment
    By jigoku in forum Java Networking
    Replies: 5
    Last Post: October 29th, 2009, 09:19 AM