Bra_Hancock 2015-07-26 08:31 采纳率: 0%
浏览 1757

安卓客户端电脑服务器 udp 手机到连接那步就不运行了

eclipse juno 电脑是jre1.8 手机是安卓4.1.2
服务器到接受信息卡住,手机到连接服务器卡住
没报错
我试了手机连电脑开的wifi和手机电脑连一个wifi都不行
不知是哪出了问题?
刚注册的没悬赏见谅

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

    button1=(Button)findViewById(R.id.button1);
    editText1=(EditText)findViewById(R.id.editText1);
    editText2=(EditText)findViewById(R.id.editText2);       
/* // 开启服务器
        ExecutorService exec = Executors.newCachedThreadPool();
        UDPServer server = new UDPServer();
        exec.execute(server);*/
   // int port = 5050;
    new AlertDialog.Builder(MainActivity.this).setTitle("测试").setMessage("创建成功").setPositiveButton("确定", null).show();
    try
    {
        addr = InetAddress.getByName("192.168.191.4");
        new AlertDialog.Builder(MainActivity.this).setTitle("测试").setMessage("ip").setPositiveButton("确定", null).show();
   //   dClient = new DatagramSocket(PORT,addr);
        dClient = new DatagramSocket();   //随机端口
        new AlertDialog.Builder(MainActivity.this).setTitle("测试").setMessage("连接  UDP 成功").setPositiveButton("确定", null).show();
        button1.setOnClickListener(new OnClickListener() 
            { 
            public void onClick(View v) 
            { 

                String str1 = editText1.getText().toString();
                String str2 = editText2.getText().toString();
                Pattern p = Pattern.compile("[^( |\\n)*]");         //匹配  除了空白以外的字符
                Matcher m = p.matcher(str2);
                if(m.find()==true)     //不能只含有空格或者回车
                {                   
                    /*String msg;
                    try
                    {
                        Socket socket = new Socket("202.204.48.135",5554);
                        PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);      
                        out.println(str2);
                        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
                        msg = br.readLine();*/
                    str2="ID1:\n"+str2;
                //  str1="frag";
                //  editText1.setText(str1);
                    DatagramPacket sendPacket = new DatagramPacket(str2.getBytes() ,str2.length() , addr , PORT);
                    try
                    {
                        dClient.send(sendPacket);
                    } catch (IOException e)
                    {
                        // TODO 自动生成的 catch 块
                        e.printStackTrace();
                    }
                    //  str1=str1+"\n"+"ID1:\n"+str2+"\n";
                    //  editText1.setText(str1);
                        editText2.setText(null);
                /*   } catch (IOException e)
                    {
                        // TODO 自动生成的 catch 块
                        e.printStackTrace();
                    }   */  
                }
                else 
                {
                    editText2.setText(null);
                }

            } 
            }); 
    } catch (UnknownHostException e)
    {
        // TODO 自动生成的 catch 块
        e.printStackTrace();
    } catch (SocketException e)
    {
        // TODO 自动生成的 catch 块
        e.printStackTrace();
    }


}


//以下是自动生成的
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}


  • 写回答

6条回答 默认 最新

  • Bra_Hancock 2015-07-26 08:44
    关注

    上面第3个对话框不弹 估计就是datagramSocket出问题了但是没看出来哪错了 因为看网上基本都是这么写的

    评论

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3