import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
public class POST {
private DatagramSocket udpSocket;
private InetAddress serverAddress;
private int port;
private Scanner scanner;
private POST(String destinationAddr, int port) throws IOException {
this.serverAddress = InetAddress.getByName(destinationAddr);
this.port = port;
udpSocket = new DatagramSocket(this.port);
scanner = new Scanner(System.in);
}
public static void main(String[] args) throws NumberFormatException, IOException {
POST sender = new POST("192.168.1.3", 8215);
System.out.println("-- Running UDP Client at " + InetAddress.getLocalHost() + " --");
sender.start();
}
private int start() throws IOException {
String in;
// while (true) {
// in = scanner.nextLine();
String quote="WHAT";
byte[] buffer = quote.getBytes();
DatagramPacket p = new DatagramPacket(quote.getBytes(), quote.getBytes().length, serverAddress, port);
this.udpSocket.send(p);
//}
return 0;
}
}
======================
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class GET {
private DatagramSocket udpSocket;
private int port;
public GET(int port) throws SocketException, IOException {
this.port = port;
this.udpSocket = new DatagramSocket(this.port);
}
private void listen() throws Exception {
System.out.println("-- Running Server at " + InetAddress.getLocalHost() + "--");
String msg;
while (true) {
byte[] buf = new byte[256];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
// blocks until a packet is received
udpSocket.receive(packet);
msg = new String(packet.getData()).trim();
System.out.println(
"Message from " + packet.getAddress().getHostAddress() + ": " + msg);
}
}
public static void main(String[] args) throws Exception {
GET client = new GET(8212);//Integer.parseInt(args[0]));
client.listen();
}
}
Tidak ada komentar:
Posting Komentar