【问题标题】:Sending commands from phone to computer via Sockets通过 Socket 将命令从手机发送到计算机
【发布时间】:2018-06-25 17:52:15
【问题描述】:

我正在尝试使用套接字将命令从我的手机发送到我的计算机。 我在这里尝试了答案: Android and PC Socket connection

但经过一番挖掘后,我发现您需要使用异步任务,所以我尝试了这个: Using AsyncTask for android network connection

但由于某种原因,我的套接字超时。有没有办法找出原因?因为从错误中我无法判断:

来自 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;
        }
    }

}

【问题讨论】:

  • 你在 catch 块中什么都不做。返回带有错误消息和 e.getMessage() 的字符串。在 onPostExecute() 中显示带有 Toast 的字符串。然后您的应用的用户就会知道会发生什么。

标签: java android sockets


【解决方案1】:

我为此用 Java 编写了一个服务器,并在模拟器中对其进行了测试。 我有两件事要做:

  1. 如果您使用的是 Android 模拟器,则服务器 IP 为“10.0.2.2”。 LocalHost 是 de Emulator 虚拟机。
  2. 您的应用程序需要 Permission Internet in manifest

这是服务器代码

/**
 *
 * @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);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-30
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    • 2012-02-20
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多