Search code examples
javaserversocket

Not receiving any further messages after receiving a file over socket


I am sending a file over socket from client to server. Thats working fine. But once a file is received, server program is not receiving any further messages. Its all receiving is null. Here is the client-server code.

Client:

main(...){
  Socket sock = new Socket("127.0.0.1", 12345);
  File file = new File("file.txt");
  byte[] mybytearray = new byte[(int) file.length()];
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
    bis.read(mybytearray, 0, mybytearray.length);
    OutputStream os = sock.getOutputStream();
    os.write(mybytearray, 0, mybytearray.length);

    PrintWriter out = new PrintWriter(os, true);
    out.println("next message");

    //closing here all streams and socket
}

Server:

main(...){
 ServerSocket servsock = new ServerSocket(12345);
    while (true) {
        Socket sock = servsock.accept();
        byte[] mybytearray = new byte[1024];
        InputStream is = sock.getInputStream();
        Scanner scan1 = new Scanner(is);
        FileOutputStream fos = new FileOutputStream("myfile.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        int bytesRead = is.read(mybytearray, 0, mybytearray.length);
        bos.write(mybytearray, 0, bytesRead);
        bos.close();
        fos.close();
        //Till here works fine, and file is successfully received.

        //Below is the code to receive next message. 
        //Unfortunately it is not working 
        BufferedReader input = new BufferedReader(new InputStreamReader(is));
        String line = input.readLine();
        System.out.println(line); //prints null, Whats the reason?
   }
}

Solution

  • This is an example that assumes it is a file then a line of text. In both cases I send the length first so it can just be dealt with as byte arrays.

    Server:

    ServerSocket servsock = new ServerSocket(12345);
    while (true) {
        Socket sock = servsock.accept();
        try (DataInputStream dis = new DataInputStream(sock.getInputStream())) {
            int len = dis.readInt();
            byte[] mybytearray = new byte[len];
            dis.readFully(mybytearray);
            try (FileOutputStream fos = new FileOutputStream("myfile.txt")) {
                fos.write(mybytearray);
            }
            len = dis.readInt();
            mybytearray = new byte[len];
            dis.readFully(mybytearray);
            String line = new String(mybytearray);
            System.out.println("line = " + line);
        }
    }
    

    Client:

    Socket sock = new Socket("127.0.0.1", 12345);
    File file = new File("file.txt");
    byte[] mybytearray = new byte[(int) file.length()];
    DataInputStream dis = new DataInputStream(new FileInputStream(file));
    dis.readFully(mybytearray);
    try(DataOutputStream os = new DataOutputStream(sock.getOutputStream())) {
        os.writeInt(mybytearray.length);
        os.write(mybytearray, 0, mybytearray.length);
        String nextMessage = "next message\n";
        byte message[] = nextMessage.getBytes();
        os.writeInt(message.length);
        os.write(message, 0, message.length);
    }