天天看點

用Java向IPMSG發送消息

飛鴿傳書(ip messenger,簡為ipmsg)是一個小巧友善的即時通信軟體,它适合用于區域網路内甚至廣域網間進行實時通信和文檔共享。特别是在區域網路内傳送檔案/檔案夾的速度非常快!

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 ==========*/

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