nofomopls
nofomopls

Reputation: 343

How to use BufferedReader to read content in DataInputStream

I wrote a simple socket tutorial about sending/receive messages between client and server. I used DataOutputStream to write the string in stream but server couldn't read it if I used BufferedReader

If I use PrintWriter to write(client side), it works.

What's wrong here? Tks so much.

1. Client:

    client = new Socket("localhost", 1982);
    DataOutputStream opStr = new DataOutputStream(client.getOutputStream());

    //pw = new PrintWriter(client.getOutputStream(), true);
    //pw.println("Hello, is there anybody out there?");// Can be read by BufferedReader
    opStr.writeUTF("Hello, anyone there?");
    opStr.flush();// BufferedReader can't read it

2. Server:

        openServer();// port 1982
        while(true) {
            Socket clientSocket = null;

                // listen to connection.
                clientSocket = echoServer.accept();

                DataInputStream inStr = new DataInputStream(
                        clientSocket.getInputStream());

                //System.out.println("M1: Got msg " + inStr.readUTF());// It showed the content

                BufferedReader bfReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

                System.out.println("got Messages: ");
                String strLine = "";
                // Don't show anything
                while ((strLine = bfReader.readLine()) != null) {
                    System.out.println(strLine);
                }
            }

Upvotes: 1

Views: 1795

Answers (2)

sjain
sjain

Reputation: 23344

You want to read the files as either text e.g. BufferedReader OR binary e.g. DataInputStream. So you can't use both.

Server.java

public class Server 
{
    public static void main(String[] args) 
    {
        DataInputStream inStr = null;
        String str;
        openServer();// port 1982
        while(true) 
        {
            Socket clientSocket = null;
            // listen to connection.
            clientSocket = echoServer.accept();
            try
            {
                inStr = new DataInputStream(clientSocket.getInputStream());
                str = inStr.readUTF();
                System.out.print((String) str);
                System.out.flush(); 
            } 
            catch (IOException io) 
            {
                System.err.println("I/O error occurred: " + io);
            } 
            catch (Throwable anything) 
            {
                System.err.println("Exception caught !: " + anything);
            }
            finally 
            {
                if (inStr != null) 
                 {
                    try 
                    {
                        inStr.close();
                    } 
                    catch (IOException io) 
                    {
                      System.err.println("I/O error occurred: " + io);
                    }
                }
            }
        }
    }

}

Upvotes: 0

user207421
user207421

Reputation: 310850

You can't. If you use writeUTF() at one end, you have to use readUTF() at the other end.

You just have to decide which API you're going to use, instead of trying to mix and match them.

Upvotes: 2

Related Questions