title: client Windows and Android server over USB
body: <p>I'm making an Android App to communicate with a computer windows.
  I found that it must be easy to do that by forwarding the port with ADB commands.</p>
  
  <p>So i try to make a client/server connection over USB but i have some problems</p>
  
  <p>my server on Windows:</p>
  
  <pre><code>public class Server {
  
      public static void main(String[] args) throws IOException {
  
             System.out.println("EchoClient.main()");
  
  
             Socket client = null;
             // initialize server socket
             try {
                 server = new ServerSocket(38300);
                 server.setSoTimeout(TIMEOUT * 1000);
  
                 // attempt to accept a connection
                 client = server.accept();
                 Globals.socketOut  = new PrintStream(client.getOutputStream());
                 Globals.socketIn = new BufferedReader(new InputStreamReader(client.getInputStream()));
  
  
                 // Globals.socketIn.YY
             } catch (SocketTimeoutException e) {
                 // print out TIMEOUT
                 connectionStatus = "Connection has timed out! Please try again";
                 System.out.println(connectionStatus);    
             } catch (IOException e) {
                 System.out.println("error"+ e);
             } finally {
                 // close the server socket
                 try {
                     if (server != null)
                         server.close();
                 } catch (IOException ec) {
                     System.out.println("Cannot close server socket"+ ec);
                 }
             }
  
             if (client != null) {
                 System.out.println("connected"); 
  
             }
         }
  
         public static class Globals {
  
          private static String typeOfTransmission ;
  
             static PrintStream socketOut = null;
             static BufferedReader socketIn  = null;
  
  
             public static synchronized String getTypeTransmission(){
              return typeOfTransmission;
             }
             public static synchronized void setTypeTransmission(String s){
               typeOfTransmission = s;
             }
         }
      }
  </code></pre>
  
  <p>android APP client</p>
  
  <pre><code>private Runnable initializeConnection = new Thread() {
      public void run() {
  
          Socket client = null;
          // initialize server socket
          try {
              Log.d("ip",getLocalIpAddress());
             client = new Socket(getLocalIpAddress(), 38300);
  
              Globals.socketIn = new Scanner(new InputStreamReader(
                      client.getInputStream()));
              Globals.socketOut = new PrintWriter(client.getOutputStream());
  
              // Globals.socketIn.YY
          } catch (SocketTimeoutException e) {
              // print out TIMEOUT
              connectionStatus = "Connection has timed out! Please try again";
              mHandler.post(showConnectionStatus);
          } catch (IOException e) {
              Log.e(TAG, "" + e);
          } finally {
              // close the server socket
              try {
                  if (server != null)
                      server.close();
              } catch (IOException ec) {
                  Log.e(TAG, "Cannot close server socket"+ ec);
              }
          }
  
          if (client != null) {
              Globals.connected = true;
              // print out success
              connectionStatus = "Connection was succesful!";
              Log.d(TAG, "connected!");
              mHandler.post(showConnectionStatus);
              while (Globals.socketIn.hasNext()) {
                  socketData = Globals.socketIn.next();
                  mHandler.post(socketStatus);
  
              }
          }
      }
  };
  public String getLocalIpAddress(){
          try{
              for(Enumeration&lt;NetworkInterface&gt; en =NetworkInterface.getNetworkInterfaces();en.hasMoreElements();){
                  NetworkInterface intf = en.nextElement();
                  for(Enumeration&lt;InetAddress&gt; enumIpAddress= intf.getInetAddresses();enumIpAddress.hasMoreElements();){
                      InetAddress inetAddress = enumIpAddress.nextElement();
                      if (!inetAddress.isLoopbackAddress()){
                          return inetAddress.getHostAddress().toString();
                      }
                  }
              }
          }catch(SocketException ex){
              Log.e("ServerActivity",ex.toString());
          }   
          return null;    
      }
  </code></pre>
  
  <p>Logger</p>
  
  <pre><code>01-29 11:43:49.640: D/ip(2981): fe80::a806:ff:fec6:d3d%p2p0
  01-29 11:43:49.650: E/Connection(2981): java.net.ConnectException: failed to connect to /fe80::a806:ff:fec6:d3d%p2p0%4 (port 38300): connect failed: ECONNREFUSED (Connection refused)
  </code></pre>
  
  <p>thank you</p>