【问题标题】:The code Socket clientSocket = new Socket(); crashes in android app even in separate thread代码 Socket clientSocket = new Socket();即使在单独的线程中,Android应用程序也会崩溃
【发布时间】:2016-07-30 22:40:57
【问题描述】:

我是 Android 新手,我一直在研究这个错误,但仍然没有弄清楚。我正在尝试从智能手机向笔记本电脑上运行的服务器发送消息。问题是由于某种原因它在创建套接字时崩溃(不是立即而是几秒钟后)。即使我拥有权限 (.INTERNET) 并在单独的线程中运行代码,它仍然会失败。

public class Communcation extends Activity{
private Socket s = null;
short REDIRECTED_SERVERPORT = 5000;

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

    final EditText msg = (EditText) findViewById(R.id.etMsg);
    Button send = (Button) findViewById(R.id.bSend);
    final TextView convo = (TextView) findViewById(R.id.tvConvo);
    convo.setText("");
    final TextView status = (TextView) findViewById(R.id.tvStatus);
    status.setText("");
    Button connect = (Button) findViewById(R.id.btnConnect);
    final EditText ipaddress = (EditText) findViewById(R.id.address);

    connect.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            status.setText("Estabilishing the connection to " + ipaddress.getText().toString());
            new Thread() {
                public void run() {
                    try {
                        s = new Socket(InetAddress.getByName(ipaddress.getText().toString()), REDIRECTED_SERVERPORT);
                        status.setText("Established connection..");
                    } catch (IOException e) {
                        status.setText("Failed the connection" + e.getMessage());
                    }
                }
            }.start();
        }
    });

    send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String message = msg.getText().toString();
            new Thread() {
                public void run() {
                    PrintWriter outp = null;
                    BufferedReader inp = null;
                    String serverMsg = null;

                    try {
                        outp = new PrintWriter(s.getOutputStream(), true);
                        inp = new BufferedReader(new InputStreamReader(s.getInputStream()));
                    } catch (IOException e) {
                        status.setText("Couldn't initate the buffers ");
                    }

                    outp.write(message);
                }
            }.start();
        }
    });
}

}

在我的服务器端代码上,我等待连接并打印一条消息,但从测试代码至今,它从未通过接受部分。

    while (1)
{   int clientfd;
    struct sockaddr_in client_addr;
    int addrlen=sizeof(client_addr);

    /*---accept a connection (creating a data pipe)---*/
    printf("[SERVER] Waiting for clients\n");
    clientfd = accept(sockfd, (struct sockaddr*)&client_addr, &addrlen);
    printf("%s:%d connected\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));

    recv(clientfd, buffer, MAXBUF, 0);
    printf("[SERVER] Received from the client: [%s]\n", buffer);

    /*---Close data connection---*/
    close(clientfd);
}

【问题讨论】:

  • 发布您的堆栈跟踪。
  • '代码崩溃'不是问题描述。

标签: java android sockets crash


【解决方案1】:

实际上我可以在没有它的情况下判断。您在同一线程上设置文本视图的文本。您只能在 UI 线程中触摸视图。您需要将 Runnable 发布到 Handler,或者在 UI 代码上使用 runOnUiThread。

【讨论】:

    猜你喜欢
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    • 2019-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多