UDP简单例子

最原始:

server

package com.udptest;
import java.io.*;
import java.net.*;
/**
 * Created by jiajia on 2017/7/21/021.
 */
public class UDPServer {
    public static void main(String[] args) throws IOException {
        DatagramSocket server = new DatagramSocket(8080);
        byte[] recvBuf = new byte[100];
        DatagramPacket recvPacket = new DatagramPacket(recvBuf, recvBuf.length);
        server.receive(recvPacket);
        String recvStr = new String(recvPacket.getData(), 0, recvPacket    .getLength());
        System.out.println("客户端传来消息:" + recvStr);
        int port = recvPacket.getPort();
        InetAddress addr = recvPacket.getAddress();
        String sendStr = "Hello ! I'm Server";
        byte[] sendBuf;
        sendBuf = sendStr.getBytes();
        DatagramPacket sendPacket = new DatagramPacket(sendBuf, sendBuf.length,addr, port);
        server.send(sendPacket);
        server.close();
    }
}

client

package com.udptest;
import java.io.*;
import java.net.*;
/**
 * Created by jiajia on 2017/7/21/021.
 */
public class UDPClient {
    public static void main(String[] args) throws IOException
    {
        DatagramSocket client = new DatagramSocket();
        String sendStr = "Hello! I'm Client";
        byte[] sendBuf;
        sendBuf = sendStr.getBytes();
        InetAddress addr = InetAddress.getByName("10.0.10.220");
        int port = 8080;
        DatagramPacket sendPacket = new DatagramPacket(sendBuf, sendBuf.length,    addr, port);
        client.send(sendPacket);
        byte[] recvBuf = new byte[100];
        DatagramPacket recvPacket = new DatagramPacket(recvBuf, recvBuf.length);
        client.receive(recvPacket);
        String recvStr = new String(recvPacket.getData(), 0,
                recvPacket.getLength());
        System.out.println("服务端传来消息:" + recvStr);
        client.close();
    }

}

使用netty之后的:

 public void initServer() {
        try {
            Bootstrap b = new Bootstrap();
            EventLoopGroup group = new NioEventLoopGroup();
            b.group(group)
                    .channel(NioDatagramChannel.class)
                    .option(ChannelOption.SO_BROADCAST, true)
                    .handler(new UdpServerHandler());
            b.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(65535));
            b.bind(searchPort);
        } catch (Exception e) {
            log.warn("sever 启动失败", e);
        }
    }

    private static class UdpServerHandler extends SimpleChannelInboundHandler<DatagramPacket> {
        @Override
        protected void channelRead0(ChannelHandlerContext channelHandlerContext, DatagramPacket datagramPacket) throws Exception {
            ByteBuf content = datagramPacket.copy().content();
            //... ...
            //处理数据
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值