• 【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/

  • 相关阅读:
    luogu P2827 蚯蚓
    CHOI1001/1002 火车进出栈问题
    hdoj4699 Editor
    反弹shell监控
    AppScan 9.0.3.6 crack
    Spectre & Meltdown Checker – CPU芯片漏洞检查脚本Linux版
    Microsoft IIS WebDav 'ScStoragePathFromUrl' Remote Buffer Overflow (CVE-2017-7269)
    Shodan新手使用指南
    The Art of Subdomain Enumeration (转)
    DDOS攻击方式总结 (转)
  • 原文地址:https://www.cnblogs.com/gnuhpc/p/2822298.html
Copyright © 2020-2023  润新知