Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过USB的客户端Windows和Android服务器_Android_Sockets_Usb_Client_Server - Fatal编程技术网

通过USB的客户端Windows和Android服务器

通过USB的客户端Windows和Android服务器,android,sockets,usb,client,server,Android,Sockets,Usb,Client,Server,我正在制作一个Android应用程序来与windows计算机通信。 我发现用ADB命令转发端口一定很容易做到这一点 所以我尝试通过USB建立客户机/服务器连接,但我遇到了一些问题 Windows上的我的服务器: public class Server { public static void main(String[] args) throws IOException { System.out.println("EchoClient.main()");

我正在制作一个Android应用程序来与windows计算机通信。 我发现用ADB命令转发端口一定很容易做到这一点

所以我尝试通过USB建立客户机/服务器连接,但我遇到了一些问题

Windows上的我的服务器:

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;
           }
       }
    }
安卓应用程序客户端

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<NetworkInterface> en =NetworkInterface.getNetworkInterfaces();en.hasMoreElements();){
                NetworkInterface intf = en.nextElement();
                for(Enumeration<InetAddress> 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;    
    }
多谢各位

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)