簡體   English   中英

通過套接字將命令從電話發送到計算機

[英]Sending commands from phone to computer via Sockets

我正在嘗試使用套接字將命令從手機發送到計算機。 我在這里嘗試了答案: Android和PC Socket連接

但是經過一番挖掘后,我發現您需要使用Async任務,因此我嘗試了以下操作: 使用AsyncTask進行android網絡連接

但是由於某種原因,我的套接字超時。 有辦法找出原因嗎? 因為從錯誤中我無法分辨:

Logcat的錯誤: 在此處輸入圖片說明

這是客戶端代碼:

public class MainActivity extends AppCompatActivity {

    private Socket client;
    private PrintWriter printwriter;
    private EditText textField;
    private Button button;
    private String message;

    private static final int SERVERPORT = ####;
    private static final String SERVER_IP = "########";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textField = (EditText) findViewById(R.id.editText1); // reference to the text field
        button = (Button) findViewById(R.id.button1); // reference to the send button

    }

    public void onClick(View view) {
        message = textField.getText().toString();
        textField.setText(""); // Reset the text field to blank
        new AsyncAction().execute();
    }

    private class AsyncAction extends AsyncTask<String, Void, String> {
        protected String doInBackground(String... args) {
            try {
                System.out.println("background running");
                System.out.println(message);
                client = new Socket(SERVER_IP, SERVERPORT); // connect to server
                System.out.println(client.isConnected());
                System.out.println("test");
                printwriter = new PrintWriter(client.getOutputStream(), true);
                printwriter.write(message); // write the message to output stream

                printwriter.flush();
                printwriter.close();
                client.close(); // closing the connection

            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

}

我用Java為此編寫了一個服務器,並在模擬器中對其進行了測試。 我有兩件事要做:

  1. 如果您使用的是Android模擬器,則服務器IP為“ 10.0.2.2”。 LocalHost是仿真器虛擬機。
  2. 您的應用程序需要清單中的Permission Internet

這是服務器代碼

/**
 *
 * @author Jean-Pierre
 */
public class SocketServer {

    private static final int portnumber = 4444;
    public static void main(String[] args) {
        SocketServer socketServer = new SocketServer();
        socketServer.run();
    }    

    /**
     * Reads a String from the client
     * Converts it to Uppercase
     * and sends it Back.
     */
    private void run() {
        try {
            ServerSocket serverSocket = new ServerSocket(portnumber);
            Socket clientSocket = serverSocket.accept();
            System.out.println("connected with :" + clientSocket.getInetAddress());
            PrintWriter out = 
                    new PrintWriter(clientSocket.getOutputStream(), true);
            InputStreamReader is = 
                    new InputStreamReader(clientSocket.getInputStream());
            BufferedReader in = 
                    new BufferedReader(is);
            while (true) {
                String line = in.readLine();
                if (line != null) {
                    System.out.println("recieved:" + line);
                    out.println(line.toUpperCase()); 
                }    
            }
        } catch (IOException ex) {
            Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

暫無
暫無

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

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