Guest User

Weird occurance in java.io.DataInputStream.read()

a guest
Dec 4th, 2022
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. import java.io.DataInputStream;
  2. import java.io.DataOutputStream;
  3. import java.io.IOException;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6.  
  7. public class ByteTransmissionTest {
  8.     static int LENGTH = 1000000;
  9.     public static void main(String[] args) throws IOException {
  10.         startServer();
  11.         startClient();
  12.     }
  13.  
  14.     public static void startServer() throws IOException {
  15.         ServerSocket server = new ServerSocket(12345);
  16.         new Thread(() -> {
  17.             try {
  18.                 Socket client = server.accept();
  19.                 DataInputStream input = new DataInputStream(client.getInputStream());
  20.                 DataOutputStream output = new DataOutputStream(client.getOutputStream());
  21.                 byte[] bytes = new byte[LENGTH];
  22.                 output.write(bytes);
  23.                 System.out.println("Read length " + input.readUTF()); //only the first few thousand bytes!!
  24.             }catch (IOException ex){
  25.                 throw new RuntimeException(ex);
  26.             }
  27.         }).start();
  28.     }
  29.  
  30.     public static void startClient() throws IOException {
  31.         Socket socket = new Socket("localhost", 12345);
  32.         DataInputStream input = new DataInputStream(socket.getInputStream());
  33.         DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
  34.         byte[] bytes = new byte[LENGTH];
  35.         String read = String.valueOf(input.read(bytes));
  36.         System.out.println(read);
  37.         outputStream.writeUTF(String.valueOf(input.read(bytes)));
  38.     }
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment