Advertisement
Guest User

Untitled

a guest
Jan 24th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. ublic class Connection {
  2. Thread con;
  3. Thread send;
  4. private Socket socket = null;
  5. private String ip;
  6. private int port;
  7.  
  8.  
  9. public Connection(String ip, int port) {
  10. this.ip = ip;
  11. this.port = port;
  12.  
  13.  
  14. }
  15.  
  16. public void connect() {
  17.  
  18. if (con != null) {
  19. con.stop();
  20. }
  21.  
  22. con = new Thread(new Runnable() {
  23. @Override
  24. public void run() {
  25. try {
  26. socket = new Socket(ip, port);
  27. } catch (IOException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. });
  32.  
  33. con.start();
  34.  
  35.  
  36. }
  37.  
  38.  
  39. public void send(final String text) {
  40.  
  41. try {
  42.  
  43. PrintWriter p = new PrintWriter(socket.getOutputStream());
  44. p.write(text);
  45. p.flush();
  46. p.close();
  47.  
  48. } catch (IOException e) {
  49. e.printStackTrace();
  50. }
  51.  
  52. }
  53.  
  54.  
  55.  
  56.  
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement