public class SendMes extends Thread {
public void run() {// 覆写Thread类中的run方法
try {
while (true) {
DatagramSocket socket = new DatagramSocket(2425);
byte[] buffer = new byte[65508];
DatagramPacket packet = new DatagramPacket(buffer, 0,
buffer.length);
socket.receive(packet);
socket.close();
String s = new String(packet.getData(), 0, packet.getLength());
String[] strs = s.split(":");
System.out.println(strs[4]);
if (strs.length > 4 && strs[4].equals("6291457")) {
// System.out.println("用户上线了");
long IPMSG_SENDMSG = 0x00000020;
String address = packet.getAddress().toString().substring(1);
String HOST = "系统管理员";
String SENDER = "系统管理员";
String MSG_CONTENT = "您好,您有一封邮件未处理";
DatagramSocket socket1 = new DatagramSocket();
byte[] buffer1 = ("1:" + new Date().getTime() + ":"
+ SENDER + ":" + HOST + ":" + IPMSG_SENDMSG + ":" + MSG_CONTENT)
.getBytes("gbk");
// 版本号:报文编号:发送者:ip:命令:内容
DatagramPacket packet1 = new DatagramPacket(buffer1,
buffer1.length, packet.getAddress(), 2425);
socket1.send(packet1); // 发送报文
socket1.close();
System.out.println("send message is ok.");
}
if (strs.length > 4 && strs[4].equals("6291458")) {
System.out.println("用户下线了");
}
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
发现局域网内飞秋用户上线,自动发送自定义聊天内容!