java 调用飞鸽传书源码

package qdcl.base.module.ipmsg;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Date;

public class IpMSG {
    /*========== Constant Value ==========*/
    public static final long IPMSG_COMMASK = 0x000000ff;
    public static final long IPMSG_OPTMASK = 0xffffff00;
    public static final long IPMSG_NOOPERATION = 0x00000000;
    public static final long IPMSG_BR_ENTRY = 0x00000001;
    public static final long IPMSG_BR_EXIT = 0x00000002;
    public static final long IPMSG_ANSENTRY = 0x00000003;
    public static final long IPMSG_BR_ABSENCE = 0x00000004;    

    public static final long IPMSG_BR_ISGETLIST = 0x00000018;
    public static final long IPMSG_OKGETLIST = 0x00000015;
    public static final long IPMSG_GETLIST = 0x00000016;
    public static final long IPMSG_ANSLIST = 0x00000017;

    public static final long IPMSG_SENDMSG = 0x00000020;
    public static final long IPMSG_RECVMSG = 0x00000021;

    public static final long IPMSG_READMSG = 0x00000030;
    public static final long IPMSG_DELMSG = 0x00000031;

    public static final long IPMSG_GETINFO = 0x00000040;
    public static final long IPMSG_SENDINFO = 0x00000041;

    // other opt
    public static final long IPMSG_ABSENCEOPT = 0x00000100;
    public static final long IPMSG_SERVEROPT = 0x00000200;
    public static final long IPMSG_DIALUPOPT = 0x00010000;

    // send opt
    public static final long IPMSG_SENDCHECKOPT = 0x00000100;
    public static final long IPMSG_SECRETOPT = 0x00000200;
    public static final long IPMSG_BROADCASTOPT = 0x00000400;
    public static final long IPMSG_MULTICASTOPT = 0x00000800;
    public static final long IPMSG_NOPOPUPOPT = 0x00001000;
    public static final long IPMSG_AUTORETOPT = 0x00002000;
    public static final long IPMSG_RETRYOPT = 0x00004000;
    public static final long IPMSG_PASSWORDOPT = 0x00008000;
    public static final long IPMSG_NOLOGOPT = 0x00020000;
    public static final long IPMSG_NEWMUTIOPT = 0x00040000;

    public static final int MAXBUF = 8192;

    public static void main(String[] args) {
        DatagramSocket socket;
        InetAddress address;

        long IPMSG_SENDMSG = 0x00000020;

        String SENDER = "王国强";
        String HOST = "11.66.244.166";
        String MSG_CONTENT = "真能调用飞鸽来。。。。。。";

        try {
            socket = new DatagramSocket();
            address = InetAddress.getByName("11.66.244.166");// 发送给消息的地址

            /**
             * IPMSG收发数据包的格式(一行):
             *
             * version(IPMSG版本):no(消息编号,可以用系统时间):user(发送消息的用户名):
             * host(发送消息的主机名):command(上述 Command 常量,可以用 | 组合多个值):
             * msg(消息内容)
             *
             */
            byte[] buffer = ("1:" + new Date().getTime() + ":" + SENDER + ":"
                    + HOST + ":" + IPMSG_SENDMSG + ":" + MSG_CONTENT)
                    .getBytes();

            DatagramPacket packet = new DatagramPacket(buffer, buffer.length,
                    address, 2425);
            socket.send(packet); // 发送报文

            packet = new DatagramPacket(buffer, buffer.length);
            socket.receive(packet);// 接收回应

            String message = new String(packet.getData()); // 得到报文信息

            System.out.println(message); // 显示对方返回的信息
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (SocketException e) {
            e.printStackTrace();
        }

        catch (IOException e) {
            e.printStackTrace();
        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值