2013-12-12 30 views
0

我试图创建一个聊天客户端/服务器网络,我认为它对服务器来说工作正常,但客户端有一些问题,因为它有一个弃用的警告,然后我程序告诉我使用该标志 - Xlint,我跑的javac -Xlint:弃用ChatClient,但我不知道怎样做才能修复这个警告声明:如何解决ChatClient.java中的readLine()弃用警告?

ChatClient.java:47:警告:[折旧]的readLine()在DataInputStream所已弃用 流输出.writeUTF(到Console.ReadLine());

这是我ChatClient.java文件

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


public class ChatClient implements Runnable 
{ 
    private Socket socket    = null; 
    private Thread thread    = null; 
    private DataInputStream console = null; 
    private DataOutputStream streamOut = null; 
    private ChatClientThread client = null; 

public ChatClient(String serverName, int serverPort) 
{ 
    System.out.println("Establishing connection to server..."); 

    try 
    { 
     // Establishes connection with server (name and port) 
     socket = new Socket(serverName, serverPort); 
     System.out.println("Connected to server: " + socket); 
     start(); 
    } 

    catch(UnknownHostException uhe) 
    { 
     // Host unkwnown 
     System.out.println("Error establishing connection - host unknown: " + uhe.getMessage()); 
    } 

    catch(IOException ioexception) 
    { 
     // Other error establishing connection 
     System.out.println("Error establishing connection - unexpected exception: " + ioexception.getMessage()); 
    } 

} 

public void run() 
{ 
    while (thread != null){ 
     try 
     { 
      // Sends message from console to server 
      streamOut.writeUTF(console.readLine()); 
      streamOut.flush(); 
     } 

     catch(IOException ioexception) 
     { 
      System.out.println("Error sending string to server: " + ioexception.getMessage()); 
      stop(); 
     } 
    } 
} 


public void handle(String msg) 
{ 
    // Receives message from server 
    if (msg.equals(".quit")) 
    { 
     // Leaving, quit command 
     System.out.println("Exiting...Please press RETURN to exit ..."); 
     stop(); 
    } 
    else 
     // else, writes message received from server to console 
     System.out.println(msg); 
} 

// Inits new client thread 
public void start() throws IOException 
{ 
    console = new DataInputStream(System.in); 
    streamOut = new DataOutputStream(socket.getOutputStream()); 
    if (thread == null) 
    { 
     client = new ChatClientThread(this, socket); 
     thread = new Thread(this);     
     thread.start(); 
    } 
} 

// Stops client thread 
public void stop() 
{ 
    if (thread != null) 
    { 
     thread.stop(); 
     thread = null; 
    } 
    try 
    { 
     if (console != null) console.close(); 
     if (streamOut != null) streamOut.close(); 
     if (socket != null) socket.close(); 
    } 

    catch(IOException ioe) 
    { 
     System.out.println("Error closing thread..."); } 
     client.close(); 
     client.stop(); 
    } 


public static void main(String args[]) 
{ 
    ChatClient client = null; 
    if (args.length != 2) 
     // Displays correct usage syntax on stdout 
     System.out.println("Usage: java ChatClient host port"); 
    else 
     // Calls new client 
     client = new ChatClient(args[0], Integer.parseInt(args[1])); 
} 

} 

class ChatClientThread extends Thread 
{ 
    private Socket   socket = null; 
    private ChatClient  client = null; 
    private DataInputStream streamIn = null; 

public ChatClientThread(ChatClient _client, Socket _socket) 
{ 
    client = _client; 
    socket = _socket; 
    open(); 
    start(); 
} 

public void open() 
{ 
    try 
    { 
     streamIn = new DataInputStream(socket.getInputStream()); 
    } 
    catch(IOException ioe) 
    { 
     System.out.println("Error getting input stream: " + ioe); 
     client.stop(); 
    } 
} 

public void close() 
{ 
    try 
    { 
     if (streamIn != null) streamIn.close(); 
    } 

    catch(IOException ioe) 
    { 
     System.out.println("Error closing input stream: " + ioe); 
    } 
} 

public void run() 
{ 
    while (true) 
    { try 
     { 
      client.handle(streamIn.readUTF()); 
     } 
     catch(IOException ioe) 
     { 
      System.out.println("Listening error: " + ioe.getMessage()); 
      client.stop(); 
     } 
    } 
} 
} 

我认为这个问题是使用.readLine(),但我需要帮助解决这个问题。

+0

错误,请使用Javadoc弃用声明中提到的方法? – EJP

回答

0

您可以将@SuppressWarnings("deprecation")注释添加到您的类或方法来抑制警告。弃用通常意味着方法或类将在下一个主要版本中删除,但为了向后兼容,已保留在当前版本中。通常如果注释为@Deprecated,这是因为已经编写了一个新的方法或类,它以更简洁的方式执行相同的任务,所以我不一定建议仅仅添加@SuppressWarnings注释,而是查看以正确的方式读取行您正在使用的Java版本。

+0

对于停止()不推荐使用的警告我使用中断(),它的工作(我认为...),但有些事情告诉我,我做了我应该做的事情。对于这个警告(readLine()),有没有办法知道在java中读取一行的正确方法? – user3097315

+0

这里是一个阅读一条线的替代方法现在,您正在使用的方法不赞成http://stackoverflow.com/questions/5611387/datainputstream-deprecated-readline-method – drembert

+0

我仍然无法让它工作用这种方法,但也许这是正确的道路......我会继续努力。 – user3097315

0

这里的概念问题是DataInputStream不是一个用于处理文本的API。 readLine方法对流的字符编码方案具有硬连接假设。

从System.in线路输入的一种更好的方式将其包装成一个BufferedReader和使用BufferedReader.readLine()

如果您忽略该问题(例如使用@SuppressWarning),则可能会发现如果控制台配置为使用某些字符编码,则应用程序会损坏字符。例如,我怀疑UTF-8会中断。

+0

我正在考虑这个问题,SuppressWarning似乎并不合适,因为它会忽略问题,正如您所说的,程序不会将信息传输到服务器。我尝试了BufferedReader.readLine(),我不知道我是否正确使用它,但它仍然无法正常工作。 – user3097315

+0

如果您向我们展示了代码,其他人可能会提供帮助。这可能是你的问题与readLine无关。 –

+0

我在顶部有正常的客户端代码,我只是在streamOut.writeUTF(console.readLine())之前添加的;一个变量行= BufferedReader.readLine(console);然后我插入streamOut.writeUTF(行)。它看起来像这样: (...) line = BufferedReader.readLine(console); streamOut.writeUTF(line); (...) – user3097315