L
L
lenston2015-03-27 12:44:50
Java
lenston, 2015-03-27 12:44:50

An incomprehensible bug when receiving a message. how to remove blank lines?

Sending a message..

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");}
    }
}

Receiving a message..
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);
    }
}

What gets ..
505494d1427280461
For some unknown reason, extra empty lines appear ..
What to do with it?
When changed to
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);}
    }

get..
3bde73296585482398e88b89045fccb2.jpg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
asd111, 2015-03-27
@asd111

so try:

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);}
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question