DataInputStream readLong () gets invalid value

Hi, I am having problems with the method. readLong() DataInputStream.When I put the value through DataOutputStream.writeLong(), this is the correct value, but when it is sent, it is much larger than it should be, I have the code on so that the program does not hang

this is a client

    socket = new Socket(ipAddress, Port);

    bos = new BufferedOutputStream(socket.getOutputStream());
    dos = new DataOutputStream(bos);

    File f = new File("C:/Users/lukeLaptop/Downloads/RemoveWAT22.zip");

    long length = f.length();
    dos.writeLong(length);

    String name = f.getName();
    dos.writeUTF(name);

    FileInputStream fis = new FileInputStream(f);
    bis = new BufferedInputStream(fis);

    int theByte = 0;
    while ((theByte = bis.read()) != -1) {
        bos.write(theByte);
    }
    bis.close();

    dos.close();

server side

    ServerSocket server = new ServerSocket(8080);

    while (true) {
        socket = server.accept();

        System.out.println("Got a client !");

        bis = new BufferedInputStream(socket.getInputStream());

        dis = new DataInputStream(bis);

        int filesCount = dis.readInt();



        long fileLength = dis.readLong();
        //String fileName = dis.readUTF();

        File f = new File("C:/test/here/test.zip");

        FileOutputStream fos = new FileOutputStream(f);
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        for (int j = 0; j < fileLength; j++) {
            bos.write(bis.read());
        }

        bos.close();
        dis.close();

EDIT

If someone can help me with the code, it is very valuable, I am new to sockets and I am a bit confused about things, all I need is to send the file length using the writeLong method for dataoutputstream and send also the name with writeUTF, but I do not know what is happening and how to fix it.

+3
source share
2 answers

writeUTF.

javadoc :

, UTF-8 - .

, writeShort, . , . , , UTF-8 . , , . str, .

, , . .

, DataInputStream.readUTF() .

+1

:

dos.writeLong(length);
dos.writeUTF(name);

.

:

int filesCount = dis.readInt();
long fileLength = dis.readLong();
//String fileName = dis.readUTF();

.

, filesCount, , fileName, . , ?

, , , .

0

All Articles