zoukankan      html  css  js  c++  java
  • DatagramSocket总是发送UDP数据后无法接收数据

    ref:http://blog.chinaunix.net/uid-20771867-id-3416509.html
             cmd:telnet localhost 5554
            redir add udp:8002:8001 (将PC 8002端口映射为模拟器 8001端口)
             模拟器-SERVER:ServerSocket server = new ServerSocket(8001);//zcl:7100改为2888
                public static void ReceiveUDP() throws IOException {
                 // TODO Auto-generated method stub
    ReliableUDP udp = new ReliableUDP(null, "172.17.21.231", 8001, 8001);
                 byte[] revBuf = new byte[1024];
                 int res = udp.ReadFixedLength(revBuf, 1);
                }                     
             PC-CLIENT:Socket socket = new Socket("127.0.0.1", 8002); 
              《TCP/UDP测试工具》 UDP:127.0.0.1 port 8002 (注意:8002是PC的映射端口) 

    ok ——》 最终找到原因:是redir add会抢占端口:          
    ok ——》解决方法:环境设置——关键是一定要使用remotePC,单靠本机无法实现调试
      

          +++++++++++++++++++++++++++++++++++++++++          +++++++++++++++++++++++++++++++++++++++++
          +    localPC                            +          +          remotePC                     +
          +    ip:172.17.22.8                     +          +        ip:172.17.21.54                +
          +     redir udp:8002:8001               +          +                                       +
          +                                       +          +                                       +
          +   +++++++++++++++++++++++++++++       +          +                                       +
          +   + android vm                +       +          +                                       +
          +   +                           +       +          +                                       +
          +   +                           +       +          +                                       +
          +   +  send(DestIP=remotePC,    +       +          +         Receive(localPort=8001)       +
          +   +       localPort=8001)     +       +          +                                       +
          +   +       DestPort=8001)      +       +          +                                       +
          +   +       -----8001--------------------------------->  8001                              -
          +   +                           +       +          +                                       +
          +   +                           +       +          +                                       +
          +   +  Receive(localPort=8001), +       +          +        send(  DestIP=localPC          +
          +   +                           +       +          +                localPort=8001)        +
          +   +                           +       +          +                DestPort=8002)         +
          +   +                           +       +          +                                       +
          +   +            8001<----------+---8002<----------+-----8001                              +
          +   +                           +       +          +                                       +
          +   +                           +       +          +                                       +
          +   +++++++++++++++++++++++++++++       +          +                                       +
          +                                       +          +                                       +
          +++++++++++++++++++++++++++++++++++++++++          +++++++++++++++++++++++++++++++++++++++++
            

    ok ——》code:
       public class LEDControlerDriver {
        public static void SendUDP() {
         // TODO Auto-generated method stub
         ReliableUDP udp = new ReliableUDP(null, DestIP, 8001, 8001);//to lydsw 前一个8001是模拟器-SERVER port,后一个8001是pc 接收消息的port,注意!不是pc映射给模拟器的端口(8002)
         String s= "32323213";
         int res = udp.SendFixedLength(s.getBytes(), s.length()); 
         
        }
        
        public static void ReceiveUDP() throws IOException {
         // TODO Auto-generated method stub
         ReliableUDP udp = new ReliableUDP(null, DestIP, 8001, 8001);//to lydsw 前一个8001是模拟器-SERVER port,后一个8001是pc 接收消息的port,注意!不是pc映射给模拟器的端口(8002)
         byte[] revBuf = new byte[1];
         int res = udp.ReadFixedLength(revBuf, 1);
        }
                     
       public class ReliableUDP {
        public int SendFixedLength(byte[] sendBuf,int nSendLen){
         Log.i("UDP Demo", "发送数据(1):");
         // check
            int res = 1;
            if (sendBuf.length!=nSendLen) {
          return -2;
         } 
           
            // create socket
            DatagramSocket socket = null;
         try {
          socket = new DatagramSocket(null);   // 指定Null很重要,否则Java会自动随机选个可用端口来绑定
          socket.setReuseAddress(true); // DatagramSocket的setReuseAddress(true)方法执行后,可以允许多个DatagramSocket
                  // 绑定到相同的IP地址和端口,那么发送到此IP地址和端口的数据能够被复制到多个DatagramSocket
          socket.setSoTimeout(10000);
          socket.bind(new InetSocketAddress(this.LocalPort)); 
         } catch (SocketException e) {
          Log.i("UDP Demo", "SendFixedLength:socket create failed1:"+e.getMessage());
          return -3;
         }     
           
            Log.i("UDP Demo", "发送数据(2):");
            // 定义一个用于发送的DatagramPacket对象 
            DatagramPacket outPacket = null;
            try {
                    outPacket = new DatagramPacket(sendBuf , nSendLen  , InetAddress.getByName(this.IPaddr) , this.TargetPort); 
                       socket.send(outPacket); 
                       Log.i("UDP Demo", "SendData:UDP发送数据:"+StringHexbyteTransform.bytesToHexString(sendBuf));
         } catch (IOException e2) {
          Log.i("UDP Demo", "SendData:io failed:"+e2.toString()+":"+e2.getMessage());
          res = -52;
         }  
           
            socket.close();
            return res;
        }
                                                       
                                                      
        public int ReadFixedLength(byte[] revBuf,int nRecvLen, int timeout)
        {
         Log.i("UDP Demo", "Read数据(1):");
         // check
            int res = 1;
            int len = nRecvLen;
            if (-1 == nRecvLen) {
             len = revBuf.length;
         } 
           
            // create socket
            DatagramSocket socket = null;
         try {
          socket = new DatagramSocket(null);   // 指定Null很重要,否则Java会自动随机选个可用端口来绑定
          socket.setReuseAddress(true); // DatagramSocket的setReuseAddress(true)方法执行后,可以允许多个DatagramSocket
                  // 绑定到相同的IP地址和端口,那么发送到此IP地址和端口的数据能够被复制到多个DatagramSocket
          socket.setSoTimeout(timeout);
          socket.bind(new InetSocketAddress(this.LocalPort)); 
         } catch (SocketException e) {
          Log.i("UDP Demo", "SendFixedLength:socket create failed1:"+e.getMessage());
          return -3;
         }     
           
            Log.i("UDP Demo", "Read数据(2):");
            // 定义一个用于发送的DatagramPacket对象 
            DatagramPacket inPacket = null;
            try {
              inPacket = new DatagramPacket(revBuf , len);
              socket.receive(inPacket); 
              ExpandRevBuffer(revBuf);
                       Log.i("UDP Demo", "receive:"+StringHexbyteTransform.bytesToHexString(revBuf)); 
         } catch (IOException e2) {
          Log.i("UDP Demo", "SendData:io failed:"+e2.toString()+":"+e2.getMessage());
          res = -52;
         }  
           
            socket.close();
            return res;
        }

  • 相关阅读:
    这篇是Mark刚写的文档,原文为http://blogs.technet.com/markrussinovich/archive/2009/11/03/3291024.aspx
    自动加域批处理脚本[转]
    一次moveuser的使用经历[转]
    How to create fully custom Role, User, Event, Resource classes for use with the Security and Scheduler modules
    VBS脚本批处理创建域用户【可自动设置用户密码,创建OU】[转]
    eXpress App Framework Team
    客户端【脚本】自动加入域[转]
    XAF 如何控制自定义按钮的使用权限[转]
    How to make crossthread calls. (多线程操控窗体控件之不可行)
    改变TFS本地映射路径.
  • 原文地址:https://www.cnblogs.com/carl2380/p/4212578.html
Copyright © 2011-2022 走看看