Guest User

Untitled

a guest
Dec 6th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. import java.io.BufferedOutputStream;
  2. import java.io.DataInputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.IOException;
  5. import java.net.ServerSocket;
  6. import java.net.Socket;
  7.  
  8. public class Main {
  9. static DataOutputStream cout(Socket s) throws IOException {
  10. // replace those lines make the run very slow.
  11. //return new DataOutputStream( (s.getOutputStream()));
  12. return new DataOutputStream(new BufferedOutputStream(s.getOutputStream()));
  13. }
  14. static DataInputStream cin(Socket s) throws IOException {
  15. return new DataInputStream( (s.getInputStream()));
  16. //return new DataInputStream(new BufferedInputStream(s.getInputStream()));
  17. }
  18. public static void main(String[] args) throws IOException, InterruptedException {
  19. ServerSocket server = new ServerSocket(12345);
  20. new Thread(() -> {
  21. try {
  22. Thread.sleep(1000);
  23. Socket client = new Socket("127.0.0.1", 12345);
  24. DataOutputStream out = cout(client);
  25. DataInputStream in = cin(client);
  26. while (true) {
  27. int a = in.readInt();
  28. out.writeInt(a);
  29. out.flush();
  30. }
  31. } catch (IOException | InterruptedException e) {
  32. e.printStackTrace();
  33. }
  34. }).start();
  35. Socket client=server.accept();
  36. DataOutputStream out = cout(client);
  37. DataInputStream in = cin(client);
  38. for (int i=0;i<10000;i++){
  39. out.writeInt(i);
  40. out.flush();
  41. if (i!=in.readInt()){
  42. System.out.println("ERROR");
  43. }
  44. }
  45. client.close();
  46. server.close();
  47. Thread.sleep(1000);
  48. }
  49. }
Add Comment
Please, Sign In to add comment