title: Connect phone and computer android
body: <p>So, i want to make an android application who works like a remote, so i can execute a command from my android app on my computer. So far i have looked a the Socket framework, and it works fine if the server and client is on the same computer, but if i move the client to my phone i get the error, that the host can't be found. I'm pretty sure it is something about how i connect, but i have no clue, how to solve it. Is it possible to connect to my computer from my phone via a Socket, if they are on same wifi-network?</p>
  
  <p>Edit:
  Sorry, thought i had linked. I have just been trying to make the java tutorial on the subject working. </p>
  
  <h2>Client:  KnockKnockClient.java</h2>
  
  <pre><code>public class KnockKnockClient {
      public static void main(String[] args) throws IOException {
  
          Socket kkSocket = null;
          PrintWriter out = null;
          BufferedReader in = null;
  
          try {
              kkSocket = new Socket("taranis", 4444);
              out = new PrintWriter(kkSocket.getOutputStream(), true);
              in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
          } catch (UnknownHostException e) {
              System.err.println("Don't know about host: taranis.");
              System.exit(1);
          } catch (IOException e) {
              System.err.println("Couldn't get I/O for the connection to: taranis.");
              System.exit(1);
          }
  
          BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
          String fromServer;
          String fromUser;
  
          while ((fromServer = in.readLine()) != null) {
              System.out.println("Server: " + fromServer);
              if (fromServer.equals("Bye."))
                  break;
  
              fromUser = stdIn.readLine();
          if (fromUser != null) {
                  System.out.println("Client: " + fromUser);
                  out.println(fromUser);
          }
          }
  
          out.close();
          in.close();
          stdIn.close();
          kkSocket.close();
      }
  }
  </code></pre>
  
  <h2>Server:KnockKnockServer.java</h2>
  
  <pre><code>public class KnockKnockServer {
      public static void main(String[] args) throws IOException {
  
          ServerSocket serverSocket = null;
          try {
              serverSocket = new ServerSocket(4444);
          } catch (IOException e) {
              System.err.println("Could not listen on port: 4444.");
              System.exit(1);
          }
  
          Socket clientSocket = null;
          try {
              clientSocket = serverSocket.accept();
          } catch (IOException e) {
              System.err.println("Accept failed.");
              System.exit(1);
          }
  
          PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
          BufferedReader in = new BufferedReader(
                  new InputStreamReader(
                  clientSocket.getInputStream()));
          String inputLine, outputLine;
          KnockKnockProtocol kkp = new KnockKnockProtocol();
  
          outputLine = kkp.processInput(null);
          out.println(outputLine);
  
          while ((inputLine = in.readLine()) != null) {
               outputLine = kkp.processInput(inputLine);
               out.println(outputLine);
               if (outputLine.equals("Bye."))
                  break;
          }
          out.close();
          in.close();
          clientSocket.close();
          serverSocket.close();
      }
  }
  </code></pre>
  
  <p>I followed <a href="http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html" rel="nofollow">this</a> tutorial.</p>