飛鴿傳書(IP Messenger,簡為IPMsg)是一個小巧方便的即時通信軟件,它適合用于局域網內甚至廣域網間進行實時通信和文檔共享。特別是在局域網內傳送文件/文件夾的速度非常快!
本文演示了如何使用Java的net包,向IPMSG客戶端發送消息。
IPMSG Command 常量定義如下:
IPMSG收發數據包的格式(一行):
示例(向IPMSG發送消息,需要先打開對方的IPMSG):
你可以在 SourceForge 找到開源的 IP MSG for Java
從本Blog 下載
http://www.aygfsteel.com/tripper
- IPMsg 是一款局域網內即時通信軟件, 基于 TCP/IP(UDP).
- 可運行于多種操作平臺(Win/Mac/UNIX/Java), 并實現跨平臺信息交流.
- 不需要服務器支持.
- 支持文件/文件夾的傳送 (2.00版以上)
- 通訊數據采用 RSA/Blofish 加密 (2.00版以上)
- 十分小巧, 簡單易用, 而且你可以完全免費使用它
- 目前已有的版本包括: Win32, Win16, MacOS, MacOSX, X11, GTK, GNOME,Java 等, 并且公開源代碼。
本文演示了如何使用Java的net包,向IPMSG客戶端發送消息。
IPMSG Command 常量定義如下:
1 /*========== Constant Value ==========*/
2 public static final long IPMSG_COMMASK = 0x000000ff;
3 public static final long IPMSG_OPTMASK = 0xffffff00;
4 public static final long IPMSG_NOOPERATION = 0x00000000;
5 public static final long IPMSG_BR_ENTRY = 0x00000001;
6 public static final long IPMSG_BR_EXIT = 0x00000002;
7 public static final long IPMSG_ANSENTRY = 0x00000003;
8 public static final long IPMSG_BR_ABSENCE = 0x00000004;
9
10
11
12 public static final long IPMSG_BR_ISGETLIST = 0x00000018;
13 public static final long IPMSG_OKGETLIST = 0x00000015;
14 public static final long IPMSG_GETLIST = 0x00000016;
15 public static final long IPMSG_ANSLIST = 0x00000017;
16
17 public static final long IPMSG_SENDMSG = 0x00000020;
18 public static final long IPMSG_RECVMSG = 0x00000021;
19
20 public static final long IPMSG_READMSG = 0x00000030;
21 public static final long IPMSG_DELMSG = 0x00000031;
22
23 public static final long IPMSG_GETINFO = 0x00000040;
24 public static final long IPMSG_SENDINFO = 0x00000041;
25
26 // other opt
27 public static final long IPMSG_ABSENCEOPT = 0x00000100;
28 public static final long IPMSG_SERVEROPT = 0x00000200;
29 public static final long IPMSG_DIALUPOPT = 0x00010000;
30
31 // send opt
32 public static final long IPMSG_SENDCHECKOPT = 0x00000100;
33 public static final long IPMSG_SECRETOPT = 0x00000200;
34 public static final long IPMSG_BROADCASTOPT = 0x00000400;
35 public static final long IPMSG_MULTICASTOPT = 0x00000800;
36 public static final long IPMSG_NOPOPUPOPT = 0x00001000;
37 public static final long IPMSG_AUTORETOPT = 0x00002000;
38 public static final long IPMSG_RETRYOPT = 0x00004000;
39 public static final long IPMSG_PASSWORDOPT = 0x00008000;
40 public static final long IPMSG_NOLOGOPT = 0x00020000;
41 public static final long IPMSG_NEWMUTIOPT = 0x00040000;
42
43 public static final int MAXBUF = 8192;
44 /*========== end ==========*/
2 public static final long IPMSG_COMMASK = 0x000000ff;
3 public static final long IPMSG_OPTMASK = 0xffffff00;
4 public static final long IPMSG_NOOPERATION = 0x00000000;
5 public static final long IPMSG_BR_ENTRY = 0x00000001;
6 public static final long IPMSG_BR_EXIT = 0x00000002;
7 public static final long IPMSG_ANSENTRY = 0x00000003;
8 public static final long IPMSG_BR_ABSENCE = 0x00000004;
9
10
11
12 public static final long IPMSG_BR_ISGETLIST = 0x00000018;
13 public static final long IPMSG_OKGETLIST = 0x00000015;
14 public static final long IPMSG_GETLIST = 0x00000016;
15 public static final long IPMSG_ANSLIST = 0x00000017;
16
17 public static final long IPMSG_SENDMSG = 0x00000020;
18 public static final long IPMSG_RECVMSG = 0x00000021;
19
20 public static final long IPMSG_READMSG = 0x00000030;
21 public static final long IPMSG_DELMSG = 0x00000031;
22
23 public static final long IPMSG_GETINFO = 0x00000040;
24 public static final long IPMSG_SENDINFO = 0x00000041;
25
26 // other opt
27 public static final long IPMSG_ABSENCEOPT = 0x00000100;
28 public static final long IPMSG_SERVEROPT = 0x00000200;
29 public static final long IPMSG_DIALUPOPT = 0x00010000;
30
31 // send opt
32 public static final long IPMSG_SENDCHECKOPT = 0x00000100;
33 public static final long IPMSG_SECRETOPT = 0x00000200;
34 public static final long IPMSG_BROADCASTOPT = 0x00000400;
35 public static final long IPMSG_MULTICASTOPT = 0x00000800;
36 public static final long IPMSG_NOPOPUPOPT = 0x00001000;
37 public static final long IPMSG_AUTORETOPT = 0x00002000;
38 public static final long IPMSG_RETRYOPT = 0x00004000;
39 public static final long IPMSG_PASSWORDOPT = 0x00008000;
40 public static final long IPMSG_NOLOGOPT = 0x00020000;
41 public static final long IPMSG_NEWMUTIOPT = 0x00040000;
42
43 public static final int MAXBUF = 8192;
44 /*========== end ==========*/
IPMSG收發數據包的格式(一行):
1 version(IPMSG版本):no(消息編號,可以用系統時間):user(發送消息的用戶名):host(發送消息的主機名):command(上述 Command 常量,可以用 | 組合多個值):msg(消息內容)
示例(向IPMSG發送消息,需要先打開對方的IPMSG):
1 import java.io.IOException;
2 import java.net.DatagramPacket;
3 import java.net.DatagramSocket;
4 import java.net.InetAddress;
5 import java.net.SocketException;
6 import java.net.UnknownHostException;
7 import java.util.Date;
8
9 /**
10 * @author 亂 7 8 糟 http://www.fadesky.com
11 */
12 public class TestIPMSG
13 {
14 public static void main(String[] args)
15 {
16 DatagramSocket socket;
17 InetAddress address;
18
19 long IPMSG_SENDMSG = 0x00000020;
20
21 String SENDER = "亂 7 8 糟";
22 String HOST = "Localhost";
23 String MSG_CONTENT = "Hello World!";
24
25 try
26 {
27 socket = new DatagramSocket();
28 address = InetAddress.getByName("192.168.1.20");// 發送給消息的地址
29
30 /**
31 * IPMSG收發數據包的格式(一行):
32 *
33 * version(IPMSG版本):no(消息編號,可以用系統時間):user(發送消息的用戶名):
34 * host(發送消息的主機名):command(上述 Command 常量,可以用 | 組合多個值):
35 * msg(消息內容)
36 *
37 */
38 byte[] buffer = ("1:" + new Date().getTime() + ":" + SENDER + ":" + HOST
39 + ":" + IPMSG_SENDMSG + ":" + MSG_CONTENT).getBytes();
40
41 DatagramPacket packet = new DatagramPacket(buffer, buffer.length,
42 address, 2425);
43 socket.send(packet); // 發送報文
44
45 packet = new DatagramPacket(buffer, buffer.length);
46 socket.receive(packet);// 接收回應
47
48 String message = new String(packet.getData()); // 得到報文信息
49
50 System.out.println(message); // 顯示對方返回的信息
51 }
52 catch (UnknownHostException e)
53 {
54 e.printStackTrace();
55 }
56 catch (SocketException e)
57 {
58 e.printStackTrace();
59 }
60
61 catch (IOException e)
62 {
63 e.printStackTrace();
64 }
65
66 }
67
68 }
69
2 import java.net.DatagramPacket;
3 import java.net.DatagramSocket;
4 import java.net.InetAddress;
5 import java.net.SocketException;
6 import java.net.UnknownHostException;
7 import java.util.Date;
8
9 /**
10 * @author 亂 7 8 糟 http://www.fadesky.com
11 */
12 public class TestIPMSG
13 {
14 public static void main(String[] args)
15 {
16 DatagramSocket socket;
17 InetAddress address;
18
19 long IPMSG_SENDMSG = 0x00000020;
20
21 String SENDER = "亂 7 8 糟";
22 String HOST = "Localhost";
23 String MSG_CONTENT = "Hello World!";
24
25 try
26 {
27 socket = new DatagramSocket();
28 address = InetAddress.getByName("192.168.1.20");// 發送給消息的地址
29
30 /**
31 * IPMSG收發數據包的格式(一行):
32 *
33 * version(IPMSG版本):no(消息編號,可以用系統時間):user(發送消息的用戶名):
34 * host(發送消息的主機名):command(上述 Command 常量,可以用 | 組合多個值):
35 * msg(消息內容)
36 *
37 */
38 byte[] buffer = ("1:" + new Date().getTime() + ":" + SENDER + ":" + HOST
39 + ":" + IPMSG_SENDMSG + ":" + MSG_CONTENT).getBytes();
40
41 DatagramPacket packet = new DatagramPacket(buffer, buffer.length,
42 address, 2425);
43 socket.send(packet); // 發送報文
44
45 packet = new DatagramPacket(buffer, buffer.length);
46 socket.receive(packet);// 接收回應
47
48 String message = new String(packet.getData()); // 得到報文信息
49
50 System.out.println(message); // 顯示對方返回的信息
51 }
52 catch (UnknownHostException e)
53 {
54 e.printStackTrace();
55 }
56 catch (SocketException e)
57 {
58 e.printStackTrace();
59 }
60
61 catch (IOException e)
62 {
63 e.printStackTrace();
64 }
65
66 }
67
68 }
69
你可以在 SourceForge 找到開源的 IP MSG for Java
從本Blog 下載
http://www.aygfsteel.com/tripper