Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class ByteTransmissionTest {
- static int LENGTH = 1000000;
- public static void main(String[] args) throws IOException {
- startServer();
- startClient();
- }
- public static void startServer() throws IOException {
- ServerSocket server = new ServerSocket(12345);
- new Thread(() -> {
- try {
- Socket client = server.accept();
- DataInputStream input = new DataInputStream(client.getInputStream());
- DataOutputStream output = new DataOutputStream(client.getOutputStream());
- byte[] bytes = new byte[LENGTH];
- output.write(bytes);
- System.out.println("Read length " + input.readUTF()); //only the first few thousand bytes!!
- }catch (IOException ex){
- throw new RuntimeException(ex);
- }
- }).start();
- }
- public static void startClient() throws IOException {
- Socket socket = new Socket("localhost", 12345);
- DataInputStream input = new DataInputStream(socket.getInputStream());
- DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
- byte[] bytes = new byte[LENGTH];
- String read = String.valueOf(input.read(bytes));
- System.out.println(read);
- outputStream.writeUTF(String.valueOf(input.read(bytes)));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment