1

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);
                }
            }
nofomopls
  • 343
  • 4
  • 17
  • I think that the way you have written data will allow you to read the data in that way only... like buffered reader/writer... data reader/writer... See the line you have commented in the server above... instr.readUTF(); that's what i meant... – CoderNeji Jul 28 '15 at 09:22
  • I found this: BufferedReader can't wrap an InputStream directly. It wraps another Reader http://stackoverflow.com/questions/5200187/convert-inputstream-to-bufferedreader. All i need is changing to new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), "UTF-8"). Tks so much – nofomopls Jul 28 '15 at 09:40
  • @ducanhng No. That doesn't gve you a solution for reading `writeUTF()` output with `BufferedReader.` There isn't one. – user207421 Jul 28 '15 at 09:50

2 Answers2

2

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

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

}
sjain
  • 23,126
  • 28
  • 107
  • 185