鍍金池/ 問(wèn)答/Java  Android  HTML/ Android socket代碼無(wú)法運(yùn)行

Android socket代碼無(wú)法運(yùn)行

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Thread thread=new Thread(new Runnable()  
        {  
            @Override  
            public void run()  
            {
                System.out.println("Hello World!");
                try {
                    ServerSocket ss = new ServerSocket(8888);
                    System.out.println("啟動(dòng)服務(wù)器....");
                    Socket s = ss.accept();
                    System.out.println("客戶端:"+s.getInetAddress().getLocalHost()+"已連接到服務(wù)器");

                    BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
                    //讀取客戶端發(fā)送來(lái)的消息
                    String mess = br.readLine();
                    System.out.println("客戶端:"+mess);
                    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
                    bw.write(mess+"\n");
                    bw.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });  
        thread.start(); 
    }

以上是一段網(wǎng)上找來(lái)的代碼,在IntelliJ IDEA下可以正常運(yùn)行,可以觀察到8888端口處于listen狀態(tài)。
最近在學(xué)習(xí)Android編程,用Android Studio把以上代碼核心部分復(fù)制到MainActivity.java中,然后可以運(yùn)行,以下為控制臺(tái)輸出內(nèi)容:

Client not ready yet..Waiting for process to come online
Connected to process 4234 on device Nexus_5X_API_27_x86 [emulator-5554]
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/zygote: Not late-enabling -Xcheck:jni (already on)
W/zygote: Unexpected CPU variant for X86 using defaults: x86
I/InstantRun: starting instant run server: is main process
I/System.out: Hello World!
I/System.out: 啟動(dòng)服務(wù)器....
D/OpenGLRenderer: HWUI GL Pipeline
I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/OpenGLRenderer: Swap behavior 0
D/EGL_emulation: eglCreateContext: 0x9d4050c0: maj 3 min 1 rcv 4
D/EGL_emulation: eglMakeCurrent: 0x9d4050c0: ver 3 1 (tinfo 0x9d403300)
E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
D/EGL_emulation: eglMakeCurrent: 0x9d4050c0: ver 3 1 (tinfo 0x9d403300)

此時(shí)發(fā)現(xiàn)8888端口并沒(méi)有處于Listen狀態(tài),而且也無(wú)法與client端進(jìn)行通信。
請(qǐng)問(wèn)各位,問(wèn)題可能會(huì)出在哪里?多謝了。

回答
編輯回答
心悲涼

沒(méi)寫IP地址

2018年5月22日 21:36
編輯回答
焚音

簡(jiǎn)而言之, 舉個(gè)例子.
假設(shè)你在你朋友的手機(jī)上運(yùn)行你的這個(gè)程序,監(jiān)聽(tīng) 8888 端口, 運(yùn)行一切正常.
現(xiàn)在問(wèn)題來(lái)了: 請(qǐng)問(wèn),你電腦上的 8888 端口會(huì)處于 Listen 狀態(tài)嗎?

2018年3月11日 19:54