package com.tcp.passwordkeeper; import android.util.Log; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.util.Set; import static java.lang.System.exit; public class Connection { public Thread con; Thread send; private Socket socket = null; private String ip; private int port; public Connection(String ip, int port) { this.ip = ip; this.port = port; } public void connect() { con = new Thread(new Runnable() { @Override public void run() { try { socket = new Socket(ip, port); } catch (IOException e) { e.printStackTrace(); } } }); con.start(); } public void disconnect(){ while(send.isAlive()){ } if(socket != null ){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } public void send(final String text) { send = new Thread(new Runnable() { @Override public void run() { try { while (con.isAlive()) { } PrintWriter p = new PrintWriter(socket.getOutputStream()); p.write(text); p.close(); } catch (IOException e) { e.printStackTrace(); } } }); send.start(); } }