簡體   English   中英

NetBeans服務器(在PC上)客戶端(在Android上)

[英]NetBeans Server(On PC) Client(On Android)

我正在嘗試將字符串從真實的android設備(客戶端)發送到pc(服務器),其中服務器在Netbeans 8.0上運行並且設備通過wifi連接到pc(通過connectify使用pc wifi熱點)。 服務器代碼:

try {

            clientSocket = serverSocket.accept(); // accept the client connection
            inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
            bufferedReader = new BufferedReader(inputStreamReader); // get the client message
            message = bufferedReader.readLine();

            System.out.println(message);
            inputStreamReader.close();
            clientSocket.close();

        } catch (IOException ex) {
            System.out.println("Problem in message reading");
        }

和客戶端代碼:

try{
            client = new Socket("192.168.207.1", 4444); 
            printwriter = new PrintWriter(client.getOutputStream(), true);
            printwriter.write("SENTTTTTTTTT");
            printwriter.flush();
            printwriter.close();
            client.close(); 

    } catch (Exception e) {Toast.makeText(getApplicationContext(), "Exception-->"+e.getMessage(), Toast.LENGTH_SHORT).show();}

我已經測試(使用PING應用)該IP可達。 但是,當我在設備上運行應用程序時,它會給出Exception-> null。 但是,當我同時在PC和JAVA SE應用程序上測試服務器和客戶端時,效果很好。 但它在Android設備上不起作用。

如果在Android的主線程上執行了網絡操作,則會出現異常

確保您未在​​Android的主線程上執行網絡操作。

在一個單獨的線程中做同樣的事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM