Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.PrintWriter;
  5. import java.net.ServerSocket;
  6. import java.net.Socket;
  7. import java.util.Objects;
  8.  
  9. public class Client {
  10.  
  11. public static void main(String[] args) {
  12. Client c1 = new Client(9001);
  13. //Client c2 = new Client(9002);
  14. //c1.createConnection("192.168.0.24", 9002);
  15. //c1.send.send("connect");
  16. }
  17.  
  18. ClientSend send;
  19. ClientReceive receive;
  20.  
  21. int port;
  22.  
  23. public Client(int myPort) {
  24. port = myPort;
  25. receive = new ClientReceive(myPort);
  26. }
  27.  
  28. public void createConnection(String otherIp, int otherPort) {
  29. send = new ClientSend(otherIp, otherPort);
  30. }
  31.  
  32. private class ClientSend {
  33. private Socket socket;
  34. private BufferedReader fromServer;
  35. private PrintWriter toServer;
  36. public ClientSend(String otherIp, int otherPort) {
  37. try
  38. {
  39. socket = new Socket(otherIp, otherPort);
  40. toServer = new PrintWriter(socket.getOutputStream(), true);
  41. fromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  42. }
  43. catch (IOException e)
  44. {
  45. socket = null;
  46. toServer = null;
  47. fromServer = null;
  48. }
  49.  
  50.  
  51. }
  52.  
  53. private void send(String msg) {
  54. System.out.println("Sending ( "+ port + "): " + msg);
  55. toServer.println(msg);
  56. }
  57. }
  58.  
  59. private class ClientReceive {
  60. private ServerSocket serverSocket;
  61. public ClientReceive(int port) {
  62. try {
  63. serverSocket = new ServerSocket(port);
  64. } catch (IOException e) {
  65. serverSocket = null;
  66. }
  67. new Handler(serverSocket).start();
  68. }
  69.  
  70. private void receive(String msg) {
  71. System.out.println("Received: (" + port + ")" + msg);
  72. if(msg.equals("connect")) {
  73. send = new ClientSend("localhost", 9001);
  74. send.send("lol");
  75. }
  76. }
  77.  
  78. private class Handler extends Thread {
  79.  
  80. private final ServerSocket socket;
  81.  
  82. public Handler(ServerSocket pSocket) {
  83. socket = pSocket;
  84. }
  85.  
  86. public void run()
  87. {
  88. while (true)
  89. {
  90. Socket clientSocket = null;
  91. BufferedReader fromClient = null;
  92. try
  93. {
  94. clientSocket = serverSocket.accept();
  95. }
  96.  
  97. catch (IOException e)
  98. {
  99. /*
  100. * Kann keine Verbindung zum anfragenden Client aufgebaut werden,
  101. * geschieht nichts.
  102. */
  103. }
  104. if(clientSocket!= null) {
  105. try {
  106. fromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  107. } catch (IOException ignored) {
  108.  
  109. }
  110. while (socket != null) {
  111. String msg = null;
  112. try {
  113. msg = fromClient.readLine();
  114. } catch (IOException ignored) {
  115.  
  116. }
  117. if(!Objects.equals(msg, "")) {
  118. ClientReceive.this.receive(msg);
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement