简体   繁体   中英

Sending file details over java socket fails

The code below is part of a larger attempt; it tries to transfer a file's details between server and client and fails miserably. Server writes 3 elements to socket, client should receive same 3 elements. Client blocks after reading first element. What am I doing wrong?!?

public class SocketIssues {

    static void client() throws Exception {

        new Thread() {

            @Override
            public void run() {

                try {
                    Thread.sleep(1000); // enough time for server to open its socket
                    Socket s = new Socket("localhost", 50001);

                    final DataInputStream dis = new DataInputStream(s.getInputStream());
                    final BufferedReader in = new BufferedReader(new InputStreamReader(dis));
                    System.out.println("CLIENT STARTED");
                    System.out.println("Operation: " + in.readLine());
                    System.out.println("Length: " + dis.readLong());
                    System.out.println("Name: " + dis.readUTF());
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        }.start();

    }

    static void server() throws Exception {

        ServerSocket ss = new ServerSocket(50001);
        Socket s = ss.accept();
        System.out.println("SERVER: client connected");
        DataOutputStream dos = new DataOutputStream(s.getOutputStream());
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(dos));
        long l = 2194;
        String nume = "The file name";

        out.write("FILE1" + System.lineSeparator());
        out.flush();
        dos.writeLong(l);
        dos.flush();
        dos.writeUTF(nume);
        dos.flush();
        System.out.println("SERVER: done sending" + System.lineSeparator());
    }

    public static void main(String[] args) throws Exception {
        client();
        server();

    }
}

Try sticking with just the DataOutputStream instead of mixing them up between data and a buffered writer and use the read/write UTF() method like you're already doing with the last object:

    DataOutputStream dos = new DataOutputStream(s.getOutputStream());
    long l = 2194;
    String nume = "The file name";

    dos.writeUTF("FILE1");
    dos.flush();
    dos.writeLong(l);
    dos.flush();
    dos.writeUTF(nume);
    dos.flush();
    System.out.println("SERVER: fisier trimis" + System.lineSeparator());

then in the client:

                final DataInputStream dis = new DataInputStream(s.getInputStream());
                System.out.println("CLIENT STARTED");
                System.out.println("Operation: " + dis.readUTF());
                System.out.println("Length: " + dis.readLong());
                System.out.println("Name: " + dis.readUTF());

Essentially, there's 2 layers of buffering going on. It's possible that when you call readLine() from the BufferedReader, it goes ahead and steals more bytes from the underlying stream because, well, that's what it's supposed to do. Then, when you go back to the DataInputStream and try to read an object, the preamble is gone (BufferedReader stole it), and it'll block waiting for it, eventhough there's bytes in the stream.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM