代码Socket clientSocket = new Socket();即使在单独的线程中,在Android应用程序中崩溃

时间:2016-07-30 22:40:58

标签: java android sockets crash

我是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);
}

1 个答案:

答案 0 :(得分:0)

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