您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

为什么我的UDP客户端/服务器数据报无法进行双向通信?

为什么我的UDP客户端/服务器数据报无法进行双向通信?

网络代码有效,问题在于您没有在等待响应,即该SendUdpMsg方法无法立即返回响应,应该由后台线程推送该响应。例如 :

dsocket.receive(packet2);
Response = new String(buffer, 0, packet2.getLength());

// we received the response
// but since we are running on a background thread, we can not touch the UI
runOnUiThread(new Runnable() {
    @Override
    public void run() {
        // this is executed on the main (UI) thread
        TextOne.setText(Response);                        }
});

dsocket.close();

通常,必须在后台线程上工作并在主线程上处理结果。Android提供了一些工具,例如HandlerAsyncTask

其他 2022/1/1 18:25:19 有485人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶