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 常量定义如下:

<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> 1 /*==========ConstantValue==========*/
2publicstaticfinallongIPMSG_COMMASK=0x000000ff;
3publicstaticfinallongIPMSG_OPTMASK=0xffffff00;
4publicstaticfinallongIPMSG_NOOPERATION=0x00000000;
5publicstaticfinallongIPMSG_BR_ENTRY=0x00000001;
6publicstaticfinallongIPMSG_BR_EXIT=0x00000002;
7publicstaticfinallongIPMSG_ANSENTRY=0x00000003;
8publicstaticfinallongIPMSG_BR_ABSENCE=0x00000004;
9
10
11
12publicstaticfinallongIPMSG_BR_ISGETLIST=0x00000018;
13publicstaticfinallongIPMSG_OKGETLIST=0x00000015;
14publicstaticfinallongIPMSG_GETLIST=0x00000016;
15publicstaticfinallongIPMSG_ANSLIST=0x00000017;
16
17publicstaticfinallongIPMSG_SENDMSG=0x00000020;
18publicstaticfinallongIPMSG_RECVMSG=0x00000021;
19
20publicstaticfinallongIPMSG_READMSG=0x00000030;
21publicstaticfinallongIPMSG_DELMSG=0x00000031;
22
23publicstaticfinallongIPMSG_GETINFO=0x00000040;
24publicstaticfinallongIPMSG_SENDINFO=0x00000041;
25
26//otheropt
27publicstaticfinallongIPMSG_ABSENCEOPT=0x00000100;
28publicstaticfinallongIPMSG_SERVEROPT=0x00000200;
29publicstaticfinallongIPMSG_DIALUPOPT=0x00010000;
30
31//sendopt
32publicstaticfinallongIPMSG_SENDCHECKOPT=0x00000100;
33publicstaticfinallongIPMSG_SECRETOPT=0x00000200;
34publicstaticfinallongIPMSG_BROADCASTOPT=0x00000400;
35publicstaticfinallongIPMSG_MULTICASTOPT=0x00000800;
36publicstaticfinallongIPMSG_NOPOPUPOPT=0x00001000;
37publicstaticfinallongIPMSG_AUTORETOPT=0x00002000;
38publicstaticfinallongIPMSG_RETRYOPT=0x00004000;
39publicstaticfinallongIPMSG_PASSWORDOPT=0x00008000;
40publicstaticfinallongIPMSG_NOLOGOPT=0x00020000;
41publicstaticfinallongIPMSG_NEWMUTIOPT=0x00040000;
42
43publicstaticfinalintMAXBUF=8192;
44/*==========end==========*/

IPMSG收发数据包的格式(一行):
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> 1version(IPMSG版本):no(消息编号,可以用系统时间):user(发送消息的用户名):host(发送消息的主机名):command(上述Command常量,可以用|组合多个值):msg(消息内容)

示例(向IPMSG发送消息,需要先打开对方的IPMSG):
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> 1 importjava.io.IOException;
2importjava.net.DatagramPacket;
3importjava.net.DatagramSocket;
4importjava.net.InetAddress;
5importjava.net.SocketException;
6importjava.net.UnknownHostException;
7importjava.util.Date;
8
9/**
10*@author乱78糟http://blog.csdn.net/comstep
11*/
12publicclassTestIPMSG
13{
14publicstaticvoidmain(String[]args)
15{
16DatagramSocketsocket;
17InetAddressaddress;
18
19longIPMSG_SENDMSG=0x00000020;
20
21StringSENDER="乱78糟";
22StringHOST="Localhost";
23StringMSG_CONTENT="HelloWorld!";
24
25try
26{
27socket=newDatagramSocket();
28address=InetAddress.getByName("192.168.1.20");// 发送给消息的地址
29
30/**
31*IPMSG收发数据包的格式(一行):
32*
33*version(IPMSG版本):no(消息编号,可以用系统时间):user(发送消息的用户名):
34*host(发送消息的主机名):command(上述Command常量,可以用|组合多个值):
35*msg(消息内容)
36*
37*/
38byte[]buffer=("1:"+newDate().getTime()+":"+SENDER+":"+HOST
39+":"+IPMSG_SENDMSG+":"+MSG_CONTENT).getBytes();
40
41DatagramPacketpacket=newDatagramPacket(buffer,buffer.length,
42address,2425);
43socket.send(packet);//发送报文
44
45packet=newDatagramPacket(buffer,buffer.length);
46socket.receive(packet);//接收回应
47
48Stringmessage=newString(packet.getData());//得到报文信息
49
50System.out.println(message);//显示对方返回的信息
51}
52catch(UnknownHostExceptione)
53{
54e.printStackTrace();
55}
56catch(SocketExceptione)
57{
58e.printStackTrace();
59}
60
61catch(IOExceptione)
62{
63e.printStackTrace();
64}
65
66}
67
68}
69

你可以在 SourceForge 找到开源的 IP MSG for Java
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值