• 【Java学习笔记】UDP客户端/服务器端


    作者:gnuhpc
    出处:http://www.cnblogs.com/gnuhpc/

    客户端:

    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    import java.net.UnknownHostException;
    import java.io.IOException;
    public class UdpClient {
        public static void main(String arg[]) {
            String outstr;
            if(arg.length >= 1)
                outstr = arg[0];
            else
                outstr = "count";
            try {
                DatagramSocket socket = new DatagramSocket();
                byte outblock[] = outstr.getBytes();
                InetAddress address = InetAddress.getLocalHost();
                DatagramPacket outpacket =
                        new DatagramPacket(outblock,outblock.length,address,8765);
                socket.send(outpacket);
                System.out.println("Client sent: " + outstr);
                byte inblock[] = new byte[256];
                DatagramPacket inpacket =
                        new DatagramPacket(inblock,inblock.length);
                socket.receive(inpacket);
                String instr = new String(inpacket.getData(),0,inpacket.getLength());
                System.out.println("Client got: " + instr);
                socket.close();
            } catch(SocketException e) {
                System.out.println(e);
            } catch(UnknownHostException e) {
                System.out.println(e);
            } catch(IOException e) {
                System.out.println(e);
            }
        }
    }

    服务器端:

    import java.net.DatagramSocket;
    import java.net.DatagramPacket;
    import java.net.InetAddress;
    import java.io.IOException;
    import java.util.Date;
    public class UdpServer {
        public static void main(String arg[]) {
            DatagramSocket socket = null;
            int count = 0;
            try {
                socket = new DatagramSocket(8765);
            } catch(IOException e) {
                System.out.println(e);
            }
            while(true) {
                try {
                    byte block[] = new byte[256];
                    DatagramPacket inpacket
                            = new DatagramPacket(block,block.length);
    socket.receive(inpacket);
                    int length = inpacket.getLength();
                    System.out.println("Length of the data received: " + length);
       byte inblock[] = inpacket.getData();
                    String inmsg = new String(inblock,0,length);
                    System.out.println("Server got: " + inmsg);
                    count++;
                    String outmsg;
                    if(inmsg.equals("date")) {
                        Date date = new Date();
                        outmsg = date.toString();
                    } else if(inmsg.equals("halt")) {
                        socket.close();
                        return;
                    } else if(inmsg.equals("count")) {
                        outmsg = "Number of messages: " + count;
                    } else {
                        outmsg = "What is " + inmsg + "?";
                    }
                    byte outblock[] = outmsg.getBytes();
                    InetAddress returnaddress = inpacket.getAddress();
                    int returnport = inpacket.getPort();
                    DatagramPacket outpacket = new DatagramPacket(
                            outblock,outblock.length,returnaddress,returnport);
                    socket.send(outpacket);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    作者:gnuhpc
    出处:http://www.cnblogs.com/gnuhpc/

  • 相关阅读:
    Node 之http模块
    Node 之 模块加载原理与加载方式
    Node 之NPM介绍
    Node.js的特点
    ECMAScript 6 简介
    Node 之URL模块
    用户模块 之 根据条件查询用户
    用户模块 之 完成用户列表的分页显示
    用户模块 之 完成查询所有帖子、完成查询所有回复以及点赞
    用户模块 之 完成查询所有用户
  • 原文地址:https://www.cnblogs.com/gnuhpc/p/2822298.html
Copyright © 2020-2023  润新知