Client.java 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package com.oblivm.backend.network;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.IOException;
  5. import org.apache.commons.io.input.CountingInputStream;
  6. import org.apache.commons.io.output.CountingOutputStream;
  7. import com.oblivm.backend.flexsc.Flag;
  8. public class Client extends Network {
  9. CountingOutputStream cos;
  10. CountingInputStream cis;
  11. public void connect(String server, int port) throws InterruptedException {
  12. try {
  13. while (true) {
  14. try {
  15. sock = new java.net.Socket(server, port); // create socket
  16. // and
  17. // connect
  18. if (sock != null)
  19. break;
  20. } catch (IOException e) {
  21. Thread.sleep(10);
  22. }
  23. }
  24. if (Flag.countIO) {
  25. cos = new CountingOutputStream(sock.getOutputStream());
  26. cis = new CountingInputStream(sock.getInputStream());
  27. os = new BufferedOutputStream(cos);
  28. is = new BufferedInputStream(cis);
  29. } else {
  30. os = new BufferedOutputStream(sock.getOutputStream());
  31. is = new BufferedInputStream(sock.getInputStream());
  32. }
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. }
  36. setUpThread();
  37. }
  38. public void printStatistic() {
  39. if (Flag.countIO) {
  40. System.out.println("\n********************************\n" + "Data Sent from Client :"
  41. + cos.getByteCount() / 1024.0 / 1024.0 + "MB\n" + "Data Sent to Client :"
  42. + cis.getByteCount() / 1024.0 / 1024.0 + "MB" + "\n********************************");
  43. }
  44. }
  45. }