Home  >  Q&A  >  body text

java - 安卓利用Socket和PC通信无法连接?

  1. 安卓利用Socket和PC通信无法连接

  2. 已经设置了 <uses-permission android:name="android.permission.INTERNET"/>

  3. 相同代码PC端和PC端通信没有问题

  4. 会在new Socket(ip,port)这里一直堵塞;

服务器代码

public ServiceThread(Socket s) throws IOException {
        socket = s;
        out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
                .getOutputStream())), true);
        in=socket.getInputStream();
    }

    public void run() {
        try {
            out.println("SERVER DATA");
            System.out.println("closing...");
            byte[] temp = new byte[1024];
            int length=-1;
            while((length=in.read(temp))>0){
                System.out.println(temp);
            }
            out.close();
            socket.close();
        } catch (Exception e) {
        }
    }
}

public class MyServer {
    static final int PORT = 8778;

    public static void main(String[] args) throws IOException {
        ServerSocket s = new ServerSocket(PORT);
        System.out.println("Server 启动");
        Socket socket = s.accept();
        ServiceThread t = new ServiceThread(socket);
        t.start();
    }
}

客户端代码

Socket socket = null;
                BufferedWriter out = null;
                try {
                    socket = new Socket(ip, 8778);
                    out = new BufferedWriter(new OutputStreamWriter(socket
                            .getOutputStream()));
                    out.write(sendViewA.getText().toString());
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                Socket socket = null;
                BufferedReader in = null;
                try {
                    socket = new Socket(ip, 8778);
                    in = new BufferedReader(new InputStreamReader(socket
                            .getInputStream()));
                    String fromServerStr = in.readLine();
                    Toast.makeText(OUTActivity.this,fromServerStr,Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
PHP中文网PHP中文网2716 days ago837

reply all(6)I'll reply

  • 迷茫

    迷茫2017-04-18 10:55:50

    This should be an IP problem:

    1. If your device is an emulator: Please check the IP address of the corresponding emulator to access the computer host. Generally, it is available on the official website of the corresponding emulator (the specific IP addresses vary depending on the emulator)

    1. Genymotion: 10.0.3.2

    2. Android emulator: 10.0.2.2

    2. If your device is a real machine, it is best to ensure that the mobile phone and the real machine are in the same LAN (if it is a laptop, it is recommended to use the mobile phone to connect to the computer WiFi before accessing). You can check the IP address in cmd yourself

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:55:50

    I encountered this once before. Try changing the client’s socket connection code to the following one.
    Client:

    socket = new Socket();
    socket.connect(new InetSocketAddress("1.1.9.30",8080), 5000);
    

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:55:50

    If the mobile phone and PC are both on the same LAN, you need to pay attention to whether wireless isolation is enabled on the router. This may also be a pitfall.

    reply
    0
  • PHPz

    PHPz2017-04-18 10:55:50

    Looking at your error report, did you let the Socket run in the UI thread? ——On Android, after Android 4.0, Socket is not allowed to be used in the UI thread, and a new thread needs to be started to use Socket

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:55:50

    NetworkOnMainThreadException, does it involve Socket-related operations in the main thread?
    Let the code run first. Assuming that your current client code is written in startClient(), you need to put this code in the child thread for execution:

    
    new Thread(new Runnable() {
            @Override
            public void run() {
                startClient();
            }
        }).start();
        

    In fact, you also have to deal with the interaction between this thread and the main thread (UI thread), which is related to the business code.
    It is recommended to first understand the difference/relationship between the main thread (UI thread) and the sub-thread, and then take a look Implementation of thread communication.

    reply
    0
  • 阿神

    阿神2017-04-18 10:55:50

    The log clearly says "network on main thread exception". Let's use a newly opened thread to perform network operations as mentioned above

    reply
    0
  • Cancelreply