Untitled

 avatar
unknown
plain_text
3 years ago
1.4 kB
4
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
 */

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 msg = client.sendEcho(tmp);//get msg form sv
        
        System.out.println(msg);

        client.sendEcho(msg);
        client.close();
    }
}
Editor is loading...