• JAVA SOCKET


    1.私聊形似

    发送放: 

                      InetAddress address = InetAddress.getByName(ad);//ad为String,接收方IP地址

                      byte[] message = msg.getBytes();//将发送形象转换为Byte类型

                      DatagramPacket packet = new DatagramPacket(message, message.length,address,9999);//实例化数据包

                      DatagramSocket socket = new DatagramSocket();//实例化套接字

                      socket.send(packet);//发送数据包

    接收放:

                     byte[] buffer = new buffer[1024];

                     DatagramPacket packet = new DatagramPacket(buffer, buffer.length);//接收数据包

                     DatagramSocket socket = new DatagramSocket(9999);//端口与发送方一致

                     socket.receive(packet);

                     String s = new String(packet.getData(), 0, packet.getLength());//转为String类型

    2.群聊形式:

    发送方:

                     InetAddress group = InetAddress.getByName("239.255.8.0");//创建群聊IP

                     byte[] message = msg.getBytes();//将发送形象转换为Byte类型

                     DatagramPacket packet = new DatagramPacket(date, date.length, group, port);//实例化数据包

                     MulticastSocket socket = new MulticastSocket(port);//实例化组播套接字

                     socket.setTimeToLive(1);//指定发送范围是本地网络      

                     socket.joinGroup(group);//加入广播组

                     socket.send(packet);//发送数据

    接收方:

                     InetAddress group = InetAddress.getByName("239.255.8.0");//实例化

                     byte[] buffer = new byte[8192];

                     DatagramPacket packet = new DatagramPacket(buffer, buffer.length, group, port);

                     MulticastSocket socket = new MulticastSocket(port);//实例化组播套接字

                     socket.joinGroup(group);//加入广播组

                     socket.receive(packet);

                     String message = new String(packet.getData(), 0, packet.getLength());

                    

  • 相关阅读:
    PHP a[n+1]共有n+1个数,数值范围是1~n,a中有1个数重复,其他各不相同,找出这个重复的数
    PHP 逆转字符串与逆转句子
    根据上排给出十个数,在其下排填出对应的十个数 要求下排每个数都是先前上排那十个数在下排出现的次数
    PHP 将数组转换为完全二叉树
    PHP 查找随即数组中的最小的k个数
    PHP 逆转单链表
    TSQL 存储过程创建 PDF 格式文件(报表)
    TSQL: 三个通用的与日期相关的,辅助按周(星期日是周的最后一天)汇总的自定义函数
    收藏夹
    十行代码搞定 "冒泡排序"
  • 原文地址:https://www.cnblogs.com/chengxuyuandashu/p/3448443.html
Copyright © 2020-2023  润新知