Send out UDP pockets
import java.io.*;
import java.net.*;
public class UDPSend {
public static void main(String args[]) {
try {
String host = "www.java2s.com";
int port = 90;
byte[] message = "Java Source and Support".getBytes();
// Get the internet address of the specified host
InetAddress address = InetAddress.getByName(host);
// Initialize a datagram packet with data and address
DatagramPacket packet = new DatagramPacket(message, message.length,
address, port);
// Create a datagram socket, send the packet through it, close it.
DatagramSocket dsocket = new DatagramSocket();
dsocket.send(packet);
dsocket.close();
} catch (Exception e) {
System.err.println(e);
}
}
}
Receive UDP pockets
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UDPReceive {
public static void main(String args[]) {
try {
int port = 90;
// Create a socket to listen on the port.
DatagramSocket dsocket = new DatagramSocket(port);
// Create a buffer to read datagrams into. If a
// packet is larger than this buffer, the
// excess will simply be discarded!
byte[] buffer = new byte[2048];
// Create a packet to receive data into the buffer
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
// Now loop forever, waiting to receive packets and printing them.
while (true) {
// Wait to receive a datagram
dsocket.receive(packet);
// Convert the contents to a string, and display them
String msg = new String(buffer, 0, packet.getLength());
System.out.println(packet.getAddress().getHostName() + ": "
+ msg);
// Reset the length of the packet before reusing it.
packet.setLength(buffer.length);
}
} catch (Exception e) {
System.err.println(e);
}
}
}
Using Datagrams to Get the Date
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class GetDate {
final static int PortDayTime = 13; // well-known daytime port
public static void main(String args[]) throws Exception {
byte msg[] = new byte[256];
DatagramSocket dgSocket = new DatagramSocket();
InetAddress destination = InetAddress.getByName("web.mit.edu");
DatagramPacket datagram = new DatagramPacket(msg, msg.length,
destination, PortDayTime);
dgSocket.send(datagram);
datagram = new DatagramPacket(msg, msg.length);
dgSocket.receive(datagram);
String received = new String(datagram.getData());
System.out.println("The time in Cambridge is now: " + received);
dgSocket.close();
}
}
An echo server using UDP sockets
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class UdpEchoServer {
static final int BUFFERSIZE = 256;
public static void main(String[] args) {
DatagramSocket sock;
DatagramPacket pack = new DatagramPacket(new byte[BUFFERSIZE],
BUFFERSIZE);
try {
sock = new DatagramSocket(7);
} catch (SocketException e) {
System.out.println(e);
return;
}
// echo back everything
while (true) {
try {
sock.receive(pack);
sock.send(pack);
} catch (IOException ioe) {
System.out.println(ioe);
}
}
}
}
java UDP example
最新推荐文章于 2024-09-10 10:21:38 发布