zoukankan      html  css  js  c++  java
  • 【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/

  • 相关阅读:
    北京 到 娄底 灌湄
    AVR--IO设置编程
    AVR--I/O端口寄存器
    AVR--IO结构分析
    虚拟机VM下 UBUNTU 下安装Mantis
    Windows环境下Mantis搭建概述
    SW4STM32 : Error message from debugger back end: Error erasing flash with vFlashErase packet Error erasing flash with vFlashErase packet
    三极管驱动继电器详解
    STM32 Bootloader 跳转到App
    (转载) STM32IAP升级---IAP升级功能编写初期的一些困惑与疑问---完成功能后的总结
  • 原文地址:https://www.cnblogs.com/gnuhpc/p/2822298.html
Copyright © 2011-2022 走看看