Untitled
5 years ago in Plain Text
import java.io.*;
import java.net.*;
import java.util.*;
class DgramUtils2 {
public DatagramPacket buildPkg(InetAddress addr, int port, String msg) {
ByteArrayOutputStream bstream = new ByteArrayOutputStream();
DataOutputStream dstream = new DataOutputStream(bstream);
byte[] data;
try {
dstream.writeUTF(msg);
} catch (IOException e) {
System.out.println("dgramutils: " + e.getMessage());
}
data = bstream.toByteArray();
return new DatagramPacket(data, data.length, addr, port);
return new DatagramPacket(data, data.length, addr, port);
}
}
public class client {
public static final int port = 3333;
public static void main(String[] args) {
System.setProperty("java.net.preferIPv4Stack" , "true");
InetAddress saddr = null;
BufferedReader in = null;
DatagramSocket sock = null;
if (args.length == 0) {
System.out.println("client [address]");
System.exit(1);
}
}
try {
sock = new DatagramSocket();
saddr = InetAddress.getByName(args[0]);
} catch (SocketException e) {
System.out.println("sock err: " + e.getMessage());
System.exit(1);
} catch (UnknownHostException e) {}
in = new BufferedReader(new InputStreamReader(System.in));
try {
while (true) {
int n = Integer.parseInt(in.readLine());
System.out.println("n "+n);
DgramUtils2 du = new DgramUtils2();
DatagramPacket pkg = du.buildPkg(saddr, port, Integer.toString(n));
sock.send(pkg);
DatagramPacket pkg = du.buildPkg(saddr, port, Integer.toString(n));
sock.send(pkg);
}
} catch (IOException e) {
System.out.println("IO: " + e.getMessage());
sock.close();
System.exit(1);
}
}
}