0

I was trying to create a chat client/server network, and i think it was working fine for the server, but the client had some problems, since it has a deprecated warning and i then the program told me to use the flag -Xlint and I ran javac -Xlint:deprecated ChatClient but I don't know what to do to repair this warning statement:

ChatClient.java:47: warning: [deprecation] readLine() in DataInputStream has been deprecated streamOut.writeUTF(console.readLine());

This is my ChatClient.java file

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

I think the problem is the use of .readLine() but I need help to solve it.

2 Answers2

0

You could add the @SuppressWarnings("deprecation") annotation to your class or method to suppress the warning. Deprecation typically means that a method or class will be removed in the next major release but has been kept in the current release for backward compatibility. Usually if something is annotated as @Deprecated it is because a new method or class has been written that performs the same task in a cleaner way, so I would not necessarily suggest just adding the @SuppressWarnings annotation but looking into the correct way to read a line in the version of Java you are using.

drembert
  • 1,246
  • 1
  • 9
  • 18
  • For the stop() deprecated warning i used interrupt() and it worked (i think...) but something tells me i made something i shouldn't have done. For this warning (readLine()), is there a way to know the correct way to read a line in java? – user3097315 Dec 12 '13 at 22:58
  • Here is a post on alternative methods of reading a line now that the method you are using is deprecated http://stackoverflow.com/questions/5611387/datainputstream-deprecated-readline-method – drembert Dec 12 '13 at 23:03
  • I still couldn't get it to work with this methods, but maybe that's in the right path...i'll keep trying. – user3097315 Dec 12 '13 at 23:24
0

The conceptual problem here is that DataInputStream is not an API this is designed for processing text. The readLine method has hard-wired assumption on what the streams's character encoding scheme.

A better way to a line of input from System.in would be to wrap it as a BufferedReader and use BufferedReader.readLine().

If you ignore the issue (e.g. using @SuppressWarning), you are liable to find that your application mangles characters if the console is configured to use some character encodings. I suspect that UTF-8 will break, for example.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I was thinking about that problem, SuppressWarning doesn't seem to fit right because it will ignore the problem as you said and the program doesn't transfer the information to server. I tried BufferedReader.readLine(), I don't know if i used it properly, but it still didn't work. – user3097315 Dec 12 '13 at 23:27
  • If you showed us the code, someone else may be able to help. It may be your problem is unrelated to readLine. – Stephen C Dec 13 '13 at 00:18
  • I have the normal client code in the top section, i just added before the streamOut.writeUTF(console.readLine()); a variable line=BufferedReader.readLine(console); and then I inserted streamOut.writeUTF(line). It would look like this: (...) line=BufferedReader.readLine(console); streamOut.writeUTF(line); (...) – user3097315 Dec 14 '13 at 17:00