import java.io.IOException;
import java.net.*;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
/**
* User: yfzhangbin
* Date: 13-7-23
* Time: 下午12:47
*/
public class UDPServer {
public static final int SERVER_PORT = 20202;
private static final int BUFFER_SIZE = 1024;
private static final List<InetSocketAddress> clientAddressList = new LinkedList<InetSocketAddress>();
static {
clientAddressList.add(new InetSocketAddress("localhost", 9001));
clientAddressList.add(new InetSocketAddress("localhost", 9002));
clientAddressList.add(new InetSocketAddress("localhost", 9003));
}
public static void main(String[] args) {
System.out.println("服务器已经启动,监听端口:"+SERVER_PORT);
try {
DatagramSocket ds = new DatagramSocket(SERVER_PORT);
while (true) {
// 从客户端接收到的内容,即客户端的请求
DatagramPacket dp = new DatagramPacket(new byte[BUFFER_SIZE], BUFFER_SIZE);
ds.receive(dp);
String msg = new String(dp.getData(), 0, dp.getLength());
System.out.println("收到客户端消息:" + msg);
if ("shutdown server".equals(msg)) {
System.out.println("服务器已经被关闭!");
break;
}
sendToClients(msg, dp.getSocketAddress());
}
ds.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 服务器端向客户端群发消息
* @param msg 消息
* @throws IOException
*/
private static void sendToClients(String msg, SocketAddress fromAddress) throws IOException {
DatagramSocket ds = new DatagramSocket();
byte[] bytes = (msg+", 经服务器转发,服务器时间 "+new Date()).getBytes();
for (InetSocketAddress clientAddress : clientAddressList) {
// todo 判断如果是发送者,可以跳过消息转发,根据ip地址判断
ds.send(new DatagramPacket(bytes, bytes.length, clientAddress));
}
ds.close();
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
/**
* User: yfzhangbin
* Date: 13-7-23
* Time: 下午1:14
*/
public class UDPClient {
private static final int BUFFER_SIZE = 1024;
private static final String SERVER_IP = "localhost";
// todo Change Run 为客户端的监听端口,取值9001,9002,9003
private static int clientListenPort = 9003;
public static void main(String[] args) {
new Thread(new ReceiveMsgThread(clientListenPort)).start();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true) {
String msg = br.readLine();
if ("shutdown".equals(msg)) {
System.out.println("客户端已退出!");
System.exit(-1);
}
sendToServer(msg);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 客户端向服务端发送消息
* @param msg
* @throws IOException
*/
private static void sendToServer(String msg) throws IOException {
DatagramSocket ds = new DatagramSocket();
byte[] bytes = msg.getBytes();
ds.send(new DatagramPacket(bytes, bytes.length, new InetSocketAddress(SERVER_IP, UDPServer.SERVER_PORT)));
ds.close();
}
/**
* 用来监听服务器端发送的信息
*/
private static class ReceiveMsgThread implements Runnable {
private int listenPort;
private ReceiveMsgThread(int listenPort) {
this.listenPort = listenPort;
}
@Override
public void run() {
System.out.println("客户端已经启动,监听端口:"+clientListenPort);
try {
DatagramSocket ds = new DatagramSocket(listenPort);
while (true) {
DatagramPacket recv = new DatagramPacket(new byte[BUFFER_SIZE], BUFFER_SIZE);
ds.receive(recv);
System.out.println("收到:" + new String(recv.getData(), 0, recv.getLength()));
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}