Untitled

 avatar
unknown
plain_text
3 years ago
2.3 kB
8
Indexable
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package UDP;
import java.io.IOException;
import java.net.*;
import java.util.*;
/**
 *
 * @author nguye
 */

public class EchoClient {
    private DatagramSocket socket;
    private InetAddress address;

    private byte[] buf;

    public EchoClient() throws SocketException, UnknownHostException {
        socket = new DatagramSocket();
        address = InetAddress.getByName("203.162.10.109");
    }

    public String sendEcho(String msg) throws IOException {
        buf = msg.getBytes();
        DatagramPacket packet 
          = new DatagramPacket(buf, buf.length, address, 2208);
        socket.send(packet);
        packet = new DatagramPacket(buf, buf.length);
        socket.receive(packet);
        String received = new String(
          packet.getData(), 0, packet.getLength());
        return received;
    }

    public void close() {
        socket.close();
    }
    public static void main(String[] args) throws SocketException, UnknownHostException, IOException {
        EchoClient client = new EchoClient();
//        String input;
        Scanner input = new Scanner(System.in);
        String tmp = ";B18DCAT204;935";
        String msg = client.sendEcho(tmp);
        
        System.out.println(msg);
        
        int size = msg.length();
        int check = 0;
        for(int i=0;i<msg.length(); i++){
            char c = msg.charAt(i);
            if( c == 59){
                check = i;
            }
        }
        check = 9;
        String idmsg = msg.substring(0, check);
        String data = msg.substring(check);
        String rs = data.replaceAll("[^;\\^\\p{L}\\p{Z}]","");
//        for(int i=0; i<size; i++){
//            char c = msg.charAt(i);
//            System.out.println(c);
//            if((c>65 && c<90) || (c>97 && c<122) || c==59){
//                continue;
//            }
//            msg = msg.substring(0,i) + msg.substring(i+1);
//        }
        client.sendEcho(idmsg + rs);
        System.out.println(idmsg+rs);
        client.close();
    }
}
Editor is loading...