简体   繁体   中英

Network connection from PC to Android Device

I used search for hours now but couldn't find an answer which fit to my question. I simply want to create a TCP network connection between a PC and an Android device. Sending data (let's say a simple String) from Android to PC actually works without any trouble with Socket and ServerSocket. However, sending data from PC to Android doesn't work. There is nothing received. Here is the code:

Server at Android device:

    try {
        ServerSocket serverSocket = new ServerSocket( 1234 );

        //tell logcat the server is online
        Log.d("TCP", "C: Server Online...");

        while ( true ) {

            Socket client = serverSocket.accept();

            BufferedReader in = new BufferedReader( new InputStreamReader(    client.getInputStream() ) );
            String input = in.readLine();
            // Do what ever you want with input String

            client.close();
        }
    } catch ( Exception e ) {
        Log.d( "TCP", "C: " + e );
    }

Client at PC:

    try {

        InetAddress serverAddr;

        try {
            serverAddr = InetAddress.getByName(IPAdresse);

            socket = new java.net.Socket(serverAddr,portAdresse);
            socket.setSoTimeout(1000);
        }catch (ConnectException e) { /*e.printStackTrace();*/ }

        PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
        out.println(nachricht);

        socket.close();

    }catch (SocketException e) { 

// e.printStackTrace(); try { socket.close(); } catch (Exception e1) { }

    }catch (IOException e) {

// e.printStackTrace(); }try { socket.close(); } catch (Exception e1) { }

    }catch (NullPointerException e) {

// e.printStackTrace(); try { socket.close(); } catch (Exception e1) { / e1.printStackTrace(); / }

    }

I have absolutely no idea why it doesn't work. I guess there is a problem in my Android code or some setting in the Android device. Maybe someone has already had the same trouble and a fitting soultion for me. Would help a lot. Thanks

I can think of a couple things that can be happening here:

  1. Android is only binding the socket to 127.0.0.1
  2. There is a missing permission on the phone
  3. A firewall somewhere is preventing communications
  4. You are not connecting to the correct IP address

For problems like this I put in a lot of print statements or step through the code while sniffing the data with Wireshark .

Change this line:

PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);

to this:

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

on your PC client and see if it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM