Advertisement
Guest User

Untitled

a guest
Jan 25th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. package com.tcp.passwordkeeper;
  2.  
  3. import android.util.Log;
  4.  
  5. import java.io.IOException;
  6. import java.io.OutputStreamWriter;
  7. import java.io.PrintWriter;
  8. import java.net.InetAddress;
  9. import java.net.ServerSocket;
  10. import java.net.Socket;
  11. import java.util.Set;
  12.  
  13. import static java.lang.System.exit;
  14.  
  15. public class Connection {
  16. public Thread con;
  17. Thread send;
  18. private Socket socket = null;
  19. private String ip;
  20.  
  21.  
  22. private int port;
  23.  
  24.  
  25. public Connection(String ip, int port) {
  26. this.ip = ip;
  27. this.port = port;
  28.  
  29.  
  30.  
  31. }
  32.  
  33. public void connect() {
  34.  
  35.  
  36.  
  37.  
  38. con = new Thread(new Runnable() {
  39. @Override
  40. public void run() {
  41. try {
  42. socket = new Socket(ip, port);
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. });
  48.  
  49. con.start();
  50.  
  51.  
  52. }
  53.  
  54.  
  55.  
  56. public void disconnect(){
  57.  
  58. while(send.isAlive()){
  59.  
  60. }
  61.  
  62. if(socket != null ){
  63. try {
  64. socket.close();
  65. } catch (IOException e) {
  66. e.printStackTrace();
  67. }
  68.  
  69.  
  70. }
  71. }
  72.  
  73. public void send(final String text) {
  74.  
  75.  
  76.  
  77.  
  78. send = new Thread(new Runnable() {
  79. @Override
  80. public void run() {
  81. try {
  82.  
  83. while (con.isAlive()) {
  84.  
  85. }
  86.  
  87.  
  88.  
  89.  
  90.  
  91. PrintWriter p = new PrintWriter(socket.getOutputStream());
  92. p.write(text);
  93. p.close();
  94.  
  95.  
  96. } catch (IOException e) {
  97. e.printStackTrace();
  98. }
  99.  
  100. }
  101. });
  102. send.start();
  103.  
  104.  
  105.  
  106. }
  107.  
  108.  
  109.  
  110.  
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement