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

客户端:

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/archive/2012/12/17/2822298.html,如需转载请自行联系原作者

上一篇:PL/SQL Developer 9.x 注册码


下一篇:.NET 云原生架构师训练营(模块二 基础巩固 配置)--学习笔记