My prof. gave me this TCP/UDP 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...
How do I handle this?
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){};
}
}
The same thing happens in the Client class.
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!
Read the javadoc. That explains why the method is deprecated and how you should replace it.
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.)
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.)
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!)
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.
精彩评论