Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedOutputStream;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class Main {
- static DataOutputStream cout(Socket s) throws IOException {
- // replace those lines make the run very slow.
- //return new DataOutputStream( (s.getOutputStream()));
- return new DataOutputStream(new BufferedOutputStream(s.getOutputStream()));
- }
- static DataInputStream cin(Socket s) throws IOException {
- return new DataInputStream( (s.getInputStream()));
- //return new DataInputStream(new BufferedInputStream(s.getInputStream()));
- }
- public static void main(String[] args) throws IOException, InterruptedException {
- ServerSocket server = new ServerSocket(12345);
- new Thread(() -> {
- try {
- Thread.sleep(1000);
- Socket client = new Socket("127.0.0.1", 12345);
- DataOutputStream out = cout(client);
- DataInputStream in = cin(client);
- while (true) {
- int a = in.readInt();
- out.writeInt(a);
- out.flush();
- }
- } catch (IOException | InterruptedException e) {
- e.printStackTrace();
- }
- }).start();
- Socket client=server.accept();
- DataOutputStream out = cout(client);
- DataInputStream in = cin(client);
- for (int i=0;i<10000;i++){
- out.writeInt(i);
- out.flush();
- if (i!=in.readInt()){
- System.out.println("ERROR");
- }
- }
- client.close();
- server.close();
- Thread.sleep(1000);
- }
- }
Add Comment
Please, Sign In to add comment