Demo
package com.test.java;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
public class DatagramTestMain {
private static final int port = 30006;
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Thread server = new ServerThread();
server.start();
Thread client = new ClientThread();
client.start();
Thread.sleep(3000);
}
static class ServerThread extends Thread {
@Override
public void run() {
try {
DatagramSocket socket = new DatagramSocket(null);// null is
InetSocketAddress address = new InetSocketAddress(port);//server端bind到本地端口
socket.bind(address);
//read
DatagramPacket rp = new DatagramPacket(new byte[1024], 1024);
socket.receive(rp);
//right style
//byte[] data = Arrays.copyOfRange(rp.getData(),rp.getOffset(), rp.getLength());
System.out.println("server Receive :" + new String(rp.getData()));
//reply
InetAddress from = rp.getAddress();
rp.setAddress(from);
String message = "--server--" + System.currentTimeMillis();
rp.setData(message.getBytes());
socket.send(rp);
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
static class ClientThread extends Thread {
@Override
public void run() {
try {
//send
DatagramSocket socket = new DatagramSocket();
InetSocketAddress address = new InetSocketAddress(
InetAddress.getLocalHost(), port);
DatagramPacket sp = new DatagramPacket(new byte[1024], 1024);
String data = "--client--" + System.currentTimeMillis();
sp.setData(data.getBytes());
sp.setSocketAddress(address);
socket.send(sp);
//block for receiving.
socket.receive(sp);
System.out.println("client receive:" + new String(sp.getData()));
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/DatagramChannel:
public class DatagramChannelTestMain {
private static final int port = 30006;
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Thread server = new ServerThread();
server.start();
Thread client = new ClientThread();
client.start();
Thread.sleep(3000);
}
static class ServerThread extends Thread {
@Override
public void run() {
try {
Selector selector = Selector.open();
InetSocketAddress address = new InetSocketAddress(port);
// /socket.setSoTimeout(10000);//10ms
DatagramChannel channel = DatagramChannel.open();
channel.configureBlocking(false);
channel.socket().bind(address);// null is
//channel.register(selector, SelectionKey.OP_READ);//DatagramChannel无需注册selector.
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (true) {
InetSocketAddress from = (InetSocketAddress)channel.receive(buffer);
if(from == null){//如果from为null,则表示没有收到数据
Thread.sleep(1000);
continue;
}
buffer.flip();
System.out.println("///Receive");
System.out.println("data : " + new String(buffer.array()));
System.out.println("time : " + System.currentTimeMillis());
System.out.println("from : "
+ from.getHostName());
buffer.clear();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
static class ClientThread extends Thread {
@Override
public void run() {
try {
InetSocketAddress address = new InetSocketAddress(
InetAddress.getLocalHost(), port);
Selector selector = Selector.open();
DatagramChannel channel = DatagramChannel.open();
channel.configureBlocking(false);
//channel.socket().connect(address);
int count = 0;
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (count < 100) {
String data = "Send Time : " + System.currentTimeMillis();
buffer.put(data.getBytes());
buffer.flip();
channel.send(buffer, address);//通过send指定address
System.out.println("///Send");
System.out.println(data);
Thread.sleep(2000);
buffer.clear();
count ++;
}
channel.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}