views:

91

answers:

3

Time to learn TCP/UDP ... Anyways, my prof gave me this example and he said it worked for him. However when I go to compile I get a deprecated API error saying readLine is not in use anymore... :\

Code:

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

public class Server{

    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();
        }

        try {
        serverSocket = new ServerSocket(port_number);
        }
        catch (IOException e)
        {System.out.println(e);}

    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);}
    }
    }
}

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+" ***");

        for(int i=0; i<=9; i++)
        if (t[i]==this) t[i]=null;

        is.close();
        os.close();
        clientSocket.close();
    }
    catch(IOException e){};
    }
}

Dang... :\ (This is the same thing for the Client class for the very same piece of code...)

+5  A: 

I'm assuming that you would like to know what you should be using instead? The Java API document states that you should use BufferedReader instead and they even give you the codez!

D.Shawley
I just tried following instructions and ... well I can't. I already have that line it wants me to replace and the line I am replacing!
Dan
I'm expecting that you're getting import issues which can easily be fixed in a proper IDE like Eclipse (not Notepad!) by CTRL-SPACING on the red, underlined class name and then selecting the correct entry, or by 'organising imports' in the refactoring menu. Otherwise, next to all the other import statements, put 'import java.io.BufferedReader;' and 'import java.io.InputStreamReader;'
Chris Dennett
+4  A: 
  1. Read the javadoc. That explains why the method is deprecated and how you should replace it.

  2. Obviously, you need to use your intelligence when you make the change provided in the Javadoc. If you declare a BufferedReader, then you have to import the class, and make sure that the code uses it. And you need to make sure that no code tries to use the wrapped stream. (Shouldn't be a problem in this example.)

  3. You can use a method despite it being deprecated. You just need to configure the Java compiler (for example, via your IDE's compiler preferences) to treat deprecated methods as warnings not errors. (I wouldn't recommend it in this case though. It would be better to fix your code, as per the suggestions in the javadoc.)

  4. Your professor ought to know better. That particular method was apparently first marked as deprecated in JDK 1.1. (Or perhaps he/she was testing your ability to sort out problems for yourself!)

Stephen C
A: 

I get a deprecated API error saying readLine is not in use anymore

No you don't. You get a [deprecation] warning saying that the method has been deprecated. You can still use it provided you understand the warning in the Javadoc.

However I agree that the code should be updated. It should use a BufferedReader and a BufferedWriter. (a) DataInputStream.readline() has been deprecated and (b) you shouldn't use a PrintWriter or PrintStream over a network, as they swallow exceptions you need to know about.

EJP