• UDPsocket编程


    socket编程UDP模式,

    package com.wtd.socket.udp;
    
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetSocketAddress;
    import java.net.SocketException;
    
    public class UDPServer {
        public static void main(String[] args) {
            try {
                DatagramSocket sever= new DatagramSocket(9000);
                byte[] data= new byte[100];
                DatagramPacket dPacket= new DatagramPacket(data, 0, data.length);
                sever.receive(dPacket);
                String string= new String(data, 0, data.length);
                byte[] data2= "hello client".getBytes();
                System.out.println(dPacket.getData());
                DatagramPacket dPacket2= new DatagramPacket(data2, 0, data2.length,dPacket.getSocketAddress());
                sever.close();
            } catch (SocketException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
            }
            
        }
    }
    package com.wtd.socket.udp;
    
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetSocketAddress;
    import java.net.SocketException;
    
    public class UDPClint {
        public static void main(String[] args) {
            try {
                DatagramSocket clint= new DatagramSocket();
                String str= "hello world";
                byte[] data= str.getBytes();
                DatagramPacket daPacket= new DatagramPacket(data, 0, data.length, new InetSocketAddress("127.0.0.1", 9000));
                clint.send(daPacket);
                clint.close();
            } catch (SocketException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
    }
  • 相关阅读:
    UVA 10817 Headmaster's Headache(状压DP)
    UVA 11795 Mega Man's Mission(状态压缩DP)
    ZOJ3777 Problem Arrangement(状态压缩DP)
    NYOJ832 合并游戏(简单状压DP)
    UVA 1252 Twenty Questions(状压DP)
    UVA 10911 Forming Quiz Teams(状压DP)
    HDU 2196 Computer(经典树形DP)
    内连接、左外连接、右外连接、交叉连接区别 羽毛
    javaweb学习总结<转> 羽毛
    HIbernate基础 羽毛
  • 原文地址:https://www.cnblogs.com/westward/p/5490914.html
Copyright © 2020-2023  润新知