Reading file part-by-part at client side and sending it via sockets and printing it part-by-part

前端 未结 2 1801
渐次进展
渐次进展 2020-12-11 12:24

I wanted to send a part of file to server where it will be printed on server screen...however dos reads entire input...kindly suggest what i can do....is there any other way

相关标签:
2条回答
  • 2020-12-11 13:03

    As @EJP says, an easy way for doing it is using InputStream.skip()

    The solution could be to create a new method,

    readBlock(int offset, byte[] buffer, BufferedInputStream bis)
    

    Which reads buffer.length bytes at the specified offset from bis

    public static int readBlock(int offset, byte[] buffer,
                BufferedInputStream bis) throws IOException {
    
            bis.skip(offset);
            int numberOfBytesRead = bis.read(buffer);
    
            return numberOfBytesRead;
        }
    

    So, your new Tser class will look like

    public class Tser {
        static int BLOCK_SIZE = 20; // only for this example
    
        public static void main(String a[]) throws IOException {
    
            ServerSocket sock = new ServerSocket(6000);
            Socket csock = sock.accept();
            DataInputStream dis = new DataInputStream(csock.getInputStream());
            FileWriter fw = new FileWriter("elephant");
            BufferedWriter bw = new BufferedWriter(fw);
            BufferedInputStream br = new BufferedInputStream(dis);
            String mess = "";
            int c;
    
            byte b[] = new byte[BLOCK_SIZE];
            // we want to read the part contained from byte 10 to 30. (20 bytes)
            readBlock(10, b, br);
            // beware of the encoding!
            System.out.println(new String(b, "UTF-8"));
            System.out.println("XX");
    
            // bw.write(mess);
            // System.out.print(mess);
            br.close();
            bw.close();
            dis.close();
            sock.close();
            csock.close();
        }    
    }
    

    And, you can do the same in the client:

    public class Tcle {
    
        public static void main(String a[]) throws IOException {
            Socket soc = new Socket("localhost", 6000);
    
            FileInputStream fis = new FileInputStream("sample");
            BufferedInputStream bis = new BufferedInputStream(fis);
            DataOutputStream dos = new DataOutputStream(soc.getOutputStream());
    
            // 64 bytes, starting at 2nd byte, for example.
            byte[] b = new byte[64];
            readBlock(2, b, bis);
            dos.write(b);
    
            bis.close();
            dos.close();
            soc.close();
    
        }
    
    
    
    }
    

    In this example, we do the splitting in parts in both sides (client and server).

    Let's suppose that the file sample contains the following characters:


    12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
    

    1.(Client side): we take 64 bytes starting at byte 2. So, we are sending:


    3456789012345678901234567890123456789012345678901234567890123456
    

    2.(Server side): we read 20 bytes starting at byte 10. (That's it, it will be the byte 74 of the original file). So, the output will be:

    34567890123456789012
    XX
    
    0 讨论(0)
  • 2020-12-11 13:30

    Use InputStream.skip() to get to where you want to start sending.

    And if you're using an InputStream to read, you should be writing with an OutputStream, not a Reader. Conversely, if you're using a Writer you should be reading with a Reader, but only if you know the data is text.

    0 讨论(0)
提交回复
热议问题