public class UdpServerTest { /**
* @param args
* @throws SocketException
*/
public static void main(String[] args) throws SocketException { final DatagramSocket server = new DatagramSocket(41017);
new Thread(new Runnable() { @Override
public void run() { try {
int port = 41216;
InetAddress addr = InetAddress.getByName("10.1.31.156");
int i = 1;
while (true) {
// TODO Auto-generated method stub
String sendStr = "Hello ! I'm Server";
sendStr = sendStr + " " + i;
byte[] sendBuf;
sendBuf = sendStr.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendBuf,
sendBuf.length, addr, port);
server.send(sendPacket);
i++;
Thread.sleep(1000);
}
}
catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start(); new Thread(new Runnable() { @Override
public void run() { // TODO Auto-generated method stub
try {
byte[] recvBuf = new byte[1000];
DatagramPacket recvPacket = new DatagramPacket(recvBuf,
recvBuf.length);
while (true) {
server.receive(recvPacket);
String recvStr = new String(recvPacket.getData(), 0,
recvPacket.getLength());
System.out.println("收到:" + recvStr);
Thread.sleep(1000);
}
}
catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
}
public class UdpClientTest { /**
* @param args
* @throws SocketException
*/
public static void main(String[] args) throws SocketException { final DatagramSocket client = new DatagramSocket(41216);
// TODO Auto-generated method stub
new Thread(new Runnable() { @Override
public void run() { try {
int port = 41017;
InetAddress addr = InetAddress.getByName("10.1.31.156");
int i = 1;
while (true) {
// TODO Auto-generated method stub
String sendStr = "Hello ! I'm Client";
sendStr = sendStr + " " + i;
byte[] sendBuf;
sendBuf = sendStr.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendBuf,
sendBuf.length, addr, port);
client.send(sendPacket);
i++;
Thread.sleep(1000);
}
}
catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start(); new Thread(new Runnable() { @Override
public void run() { // TODO Auto-generated method stub
try {
byte[] recvBuf = new byte[1000];
DatagramPacket recvPacket = new DatagramPacket(recvBuf,
recvBuf.length);
while (true) {
client.receive(recvPacket);
String recvStr = new String(recvPacket.getData(), 0,
recvPacket.getLength());
System.out.println("收到:" + recvStr);
Thread.sleep(1000);
}
}
catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start(); }
}