@lenston

Непонятный баг при получении сообщения. как убрать пустые строки?

Отправка сообщения..

package send;
 
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
 
public class SendAll {
    static DatagramSocket s;
    static DatagramPacket dp;
    public SendAll(String Line){
        
        try{s=new DatagramSocket();
        }catch(Exception ex){if(main.Const.DEBUG){System.out.println(ex.toString());}}
        
        try{sendString("SendAll"+" "+main.Const.NIKNAME+": "+Line);
        }catch(Exception ex){if(main.Const.DEBUG){System.out.println(ex.toString());}}
        
        s.close();
    }
    static void sendString(String str)throws IOException{
        byte[]buf=str.getBytes();
        dp=new DatagramPacket(buf,buf.length,InetAddress.getByName(main.Const.BROADCAST),main.Const.PORT);
        s.send(dp);
        if(main.Const.DEBUG){System.out.println(str+" sended");}
    }
}


Получение сообщения..
package scan;
 
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.StringTokenizer;
 
import send.SendTo;
 
public class StartScan extends Thread{
    static DatagramSocket s;
    static DatagramPacket dp;
    public void run(){
        while(!main.Const.EXIT){
            try{
                s=new DatagramSocket(main.Const.PORT);
                if(main.Const.DEBUG){System.out.println("Scan oppened");}
                while(true){check(recvString(s));}
            }catch(SocketException ex){if(main.Const.DEBUG){System.out.println(ex.toString());};s.close();
            }catch(Exception ex){if(main.Const.DEBUG){System.out.println(ex.toString());};s.close();
            }s.close();
        }
    }
    private void check(String Line) {
        if(main.Const.DEBUG){System.out.println(Line);}
        StringTokenizer stk = new StringTokenizer(Line," ");
        String Command = stk.nextToken();
        
             if(Command.equals("Search")                    ){new SendTo(Line.replaceAll(Command+" ","")+" "+main.Const.NIKNAME+" : connected");}
        else if(Command.equals("SendAll")                   ){System.out.println("-> "+Line.replaceAll(Command+" ",""));}
        else if(Command.equals("SendTo"+main.Const.NIKNAME) ){System.out.println("-> "+Line.replaceAll(Command+" ",""));}
        else if(main.Const.DEBUG)                            {System.out.println("err "+Line);}
    }
    static String recvString(DatagramSocket s)throws IOException{
        byte buf[]=new byte[512];
        s.receive(new DatagramPacket(buf,512));
        return new String(buf);
    }
}

Что получает..
505494d1427280461

По непонятной причине появляются лишние пустые строки..
Что с этим делать?

При изменении на
private void check(String Line) {
        if(main.Const.DEBUG){System.out.println(Line);}
        StringTokenizer stk = new StringTokenizer(Line," ");
        String Command = stk.nextToken();
        
             if(Command.equals("Search")                    ){new SendTo(Line.replaceAll(Command+" ","")+" "+main.Const.NIKNAME+" : connected");}
        else if(Command.equals("SendAll")                   ){System.out.print("-> "+Line.replaceAll(Command+" ",""));}
        else if(Command.equals("SendTo"+main.Const.NIKNAME) ){System.out.print("-> "+Line.replaceAll(Command+" ",""));}
        else if(main.Const.DEBUG)                            {System.out.print("err "+Line);}
    }


получаю..
3bde73296585482398e88b89045fccb2.jpg
  • Вопрос задан
  • 253 просмотра
Пригласить эксперта
Ответы на вопрос 1
@asd111
так попробуйте:
private void check(String Line) {
        if(main.Const.DEBUG){System.out.println(Line);}
        StringTokenizer stk = new StringTokenizer(Line," ");
        String Command = stk.nextToken();
        
             if(Command.equals("Search")                    ){new SendTo(Line.replaceAll(Command+" ","")+" "+main.Const.NIKNAME+" : connected");}
        else if(Command.equals("SendAll")                   ){System.out.print("-> "+Line.replaceAll(Command+" ",""));}
        else if(Command.equals("SendTo"+main.Const.NIKNAME) ){System.out.print("-> "+Line.replaceAll(Command+" ",""));}
        else if(main.Const.DEBUG)                            {System.out.print("err "+Line);}
    }
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы