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

标签 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);
}

最佳答案

实际上,在这种情况下我可以在没有它的情况下分辨出来。您可以在同一线程上设置 TextView 的文本。您只能触摸 UI 线程中的 View 。您需要将 Runnable 发布到处理程序,或者在 UI 代码上使用 runOnUiThread。

关于java - 代码 Socket clientSocket = new Socket();即使在单独的线程中,Android 应用也会崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38679138/

相关文章:

java - 速度测试应用程序 下载 上传 Ping

java - 我如何使 JTIdy 使 HTML 文档格式正确?

java - 简单日期格式间歇性地返回错误日期

android - 从 ExifInterface 获取旋转总是返回 0

java - 从任何网络Flutter访问HTTP服务器

java - 在 TreeMap 中键入返回 null

android - 在 ExpandableListView 中的组项之间添加间隙

python - 套接字错误 "[Errno 9] Bad file descriptor"可能是什么原因

sockets - 在执行 docker stop 时尝试连接到 Docker 守护程序套接字时获得权限被拒绝

c# - udp收不到任何数据