dxt_1515
dxt_1515
采纳率8.5%
2016-06-07 12:33 阅读 2.1k

java中,socket拿到字符串如何处理问题!

UDP通信之后拿到字符串,我要对他进行截取处理!
byte[] inBuf=new byte[1024];
DatagramPacket inPacket=new DatagramPacket(inBuf,inBuf.length);

socket.receive(inPacket);

receiveInfo= new String(inPacket.getData());
我要写一个方法字符串截取

private String stringTopoint(String str,int len){
String ret = "";
StringBuilder sb = new StringBuilder();
for(int i=0;i<len;i++){
ret=sb.substring(3, 6);
}
return ret;
}但是我拿到数据是字节的,我又要对字符串处理,我要怎么调用我写的方法呢?求大神赐教

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

12条回答 默认 最新

  • yinbucheng yinbucheng 2016-06-07 12:37

    你可以先把它变成字符String str= new String(inPacket.getData(),inPacket.getLength());然后你就可以 这样调用 toPoint(str,str.length())

    点赞 1 评论 复制链接分享
  • yinbucheng yinbucheng 2016-06-07 12:39

    你的方法命名有问题应该是toPoint(...),还有你可以不用传入length,java是面向对象编程,你直接用str.length()就可以获取到它的大小了

    点赞 评论 复制链接分享
  • dxt_1515 dxt_1515 2016-06-07 13:00

    是不是我截取的程序写的有问题呢?大神求赐教!

    点赞 评论 复制链接分享
  • yinbucheng yinbucheng 2016-06-07 13:09

    把你整个代码发来

    点赞 评论 复制链接分享
  • yinbucheng yinbucheng 2016-06-07 13:12

    可能是别的问题,你Ip写对了吗?端口是否相同

    点赞 评论 复制链接分享
  • yinbucheng yinbucheng 2016-06-07 13:13

    它没问题,receive本身就是个阻塞方法它没接受到数据时它会一直阻塞等待中

    点赞 评论 复制链接分享
  • dxt_1515 dxt_1515 2016-06-07 13:14

    /UDP数据接收线程

    public class udpReceiveThread extends Thread

    {

    @Override

    public void run()

    {

    try

    {

    socket = new DatagramSocket(8081);

    listenStatus=true;

    while(listenStatus)

    {

    // byte[] inBuf= new byte[1024];

    byte[] inBuf=new byte[1024];
    DatagramPacket inPacket=new DatagramPacket(inBuf,inBuf.length);

    socket.receive(inPacket);

                  //  String str= new String(inPacket.getData(),inPacket.getLength());
    
                     String str = new String (inPacket.getData()); 
                     receiveInfo= stringTopoint(str,inPacket.getLength()).trim();
    
                   //receiveInfo = stringTopoint(receiveInfo, inPacket.getLength()).trim();  
                    // receiveInfo= bytes2HexString(inBuf,inPacket.getLength());
                    //  receiveInfo = recvHexData(inPacket.getData(), inPacket.getLength());
    
                   //  receiveInfo=recvHexData(inPacket.getLength()).trim();
                    Message msg = new Message();  
                    receiveHandler  
                    .sendMessage(msg);                
                }                 
            } catch (Exception e)   
            {   
                e.printStackTrace();
                // TODO Auto-generated catch block   
            }    
        }
    
    
    
    }
    //发送按钮单击事件  
    public void SendButtonClick(View source)  
    {  
        new udpSendThread().start();    
    }  
    
    //监听按钮点击事件  
    public void ListenButtonClick(View source)  
    {     
        if(listenStatus==false)  
        {  
            stop.setText("停止监听");  
            new udpReceiveThread().start();  
        }  
        else  
        {  
            stop.setText("开始监听");    
            socket.close();  
            listenStatus=false;  
            new udpReceiveThread().interrupt();  
        }  
    } 
     private String stringTopoint(String ret,int len){
    
         StringBuilder sb = new StringBuilder();
         for(int i=0;i<len;i++){
             ret=sb.substring(3, 6);
         }
         return ret;
     }
    
    
    private String recvHexData(int len)
    {
        fBuffer = new byte[len];
        System.arraycopy(inBuf, 0, fBuffer, 0, len);//复制数组
        StringBuilder sb = new StringBuilder(fBuffer.length);
        String rHex = "";
        for (int i = 0; i < fBuffer.length; i++)
        {
            rHex = Integer.toHexString(fBuffer[i] & 0xFF);
            if (rHex.length() == 1)
                rHex = "0" + rHex;
            sb.append(rHex.toUpperCase());
        }
        return sb.toString().trim();
    }
    
    /*
    public static  int bytetopoint(byte[] b,int len){
         int ret = 0;
         for(int i=0;i<len;i++){
             int hex=(b[i]<<3
                     )|(b[i]<<6);
             ret=Integer.valueOf(hex);
    
         }
        return ret;
    
    }
    */
    

    // byte[]转16进制字符串

    public static String bytes2HexString(byte[] b,int len)

    {

    String ret = "";

    for (int i = 0; i < len; i++)

    {

    String hex = Integer.toHexString(b[ i ] & 0xFF);

    if (hex.length() == 1)

    {

    hex = '0' + hex;

    }

    ret += hex.toUpperCase()+" ";

    }

    return ret;

    }

    private static byte charToByte(char c)   
    {    
        return (byte) "0123456789ABCDEF".indexOf(c);    
    }  
    

    /* //bye[] 转为字符串
    public static String bytesToString(byte[] b,int len){
    String ret="" ;
    for(int i=0;i<len;i++){
    ret=b.substring(3, 6)+"";
    String hex = Integer.toHexString(b[ i ] & 0xFF);
    String hex = String.valueOf(b[i]);//将字节转化为字符串
    string hex=b[i].substring(3, 6)+"";
    ret+=hex.substring(3, 6)+" ";//截取3-6位
    ret += hex.toUpperCase()+" ";
    }
    return ret;
    }*/
    //报文判断
    //
    // ReceiveChar()

      private boolean ReceiveChar(char Char,int len)
      { byte[] RBuffer = new byte[len]; 
       System.arraycopy(inBuf,0, RBuffer, 0, len);
       if(Char != 'p' && RBuffer.length == 0) 
           return false;
    
    ScanResult __RxBuffer = null;
    // 将字符写入缓冲 //byte[] inbuf=new byte[256]; //
      __RxBuffer.writeToParcel((Parcel) inBuf, Char); // 如果收到的字符不是结束符,则退出 if(Char !='!') 
      { if(RBuffer.length > 10) //Buffer.clear(); return false; }
    
     // 收到结束符,对报文加以解析处理 // 首先检查报文格式 
      if((RBuffer[0]=='p')&&(RBuffer[4]=='!'))
      if(RBuffer.length != 10) { // __RxBuffer.clear(); return false; }
    
     // 解析应答报文 Object __LPosition; Object __RPosition;
    
     int __LPosition;
    int __RPosition;
    if(RBuffer[1] == '1') __LPosition = CharToWord(RBuffer, 7);
    
      else if(RBuffer[1] == '2') __RPosition = CharToWord(RBuffer, 7);
    }}
    return true;}
    
      //RBuffer.substring(3,RBuffer[6]);
    
      // Char=CharToWord(RBuffer[4],6); return true; }
    
    
    
     // CharToHex()
    
      Byte CharToHex(Byte Char)
       { if(Char >= '0' && Char <= '9')
        return (byte) (Char - '0'); 
      else if(Char >= 'A' && Char <= 'F')
       return (byte) (Char - 'A' + 10); 
        else if(Char >= 'a' && Char <='f')
        return (byte) (Char - 'a' + 10); 
         else return 0; }
      //------------------
    
     int CharToWord(byte[] Buffer, int Begin)
       { return (CharToHex(Buffer[Begin]) << 12) | (CharToHex(Buffer[Begin + 1]) << 8) |
        (CharToHex(Buffer[Begin + 2]) << 4) | CharToHex(Buffer[Begin + 3]); }
    
    
    @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;  
    }  
    

    }

    点赞 评论 复制链接分享
  • yinbucheng yinbucheng 2016-06-07 13:19

    你得发送端了,你要先点击监听,再点发送。。。不然就不会接受到数据

    点赞 评论 复制链接分享
  • dxt_1515 dxt_1515 2016-06-07 13:25
                     receiveInfo= stringTopoint(str,inPacket.getLength()).trim();,现在是这句话有问题
    
    点赞 评论 复制链接分享
  • yinbucheng yinbucheng 2016-06-07 13:52

    你是先点监听再点的发送吗

    点赞 评论 复制链接分享
  • yinbucheng yinbucheng 2016-06-07 13:55

    private String stringTopoint(String ret,int len){

     StringBuilder sb = new StringBuilder();
     for(int i=0;i<len;i++){
         ret=sb.substring(3, 6);
     }
     return ret;
    

    }
    这有问题sb时StringBuffer你是搞错了吧

    点赞 评论 复制链接分享
  • yinbucheng yinbucheng 2016-06-07 13:56

    你是想sb.append(ret.substring(3,6));吗

    点赞 评论 复制链接分享

相关推荐