Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetAddress;
- import java.net.InetSocketAddress;
- import java.net.SocketException;
- public class UdpClient {
- public static void main(String args[]) {
- byte[] buf;
- byte[] buf2 = new byte[1024];
- InetSocketAddress address= new InetSocketAddress("127.0.0.1",9999);
- try {
- DatagramSocket client= new DatagramSocket();
- String msg=" --Your name goes here-- ";
- buf = msg.getBytes();
- DatagramPacket packet = new DatagramPacket(buf, buf.length, address);
- client.send(packet);
- DatagramPacket packet2 = new DatagramPacket(buf2, buf2.length);
- client.receive(packet2);
- String received = new String(
- packet2.getData(), 0, packet2.getLength());
- System.out.println("Receive msg "+ received+"from "+packet2.getSocketAddress());
- client.close();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement