ud类型聊天


    public static void main(String[] args)throws Exception
    {
        System.out.println("发送数据、、、");
        DatagramSocket ds=new DatagramSocket();
        String str="我来了udp";    
        BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
        String line=null;
        while((line=bufr.readLine())!=null)
        {
            byte[] buf=line.getBytes();
            DatagramPacket dp=
                    new DatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.1"),1000);
                ds.send(dp);
            if("over".equals(line))
                break;
        }
    
    
        ds.close();
    }

 


import
java.net.*; class charservedDemo { public static void main(String[] ars)throws Exception { System.out.println("接收数据、、、"); DatagramSocket ds=new DatagramSocket(1000); while(true) { byte[] buf=new byte[1024]; DatagramPacket dp=new DatagramPacket(buf,buf.length); ds.receive(dp); String str=new String(dp.getData(),0,dp.getLength()); System.out.println(dp.getAddress()+""+str); } // ds.close(); } }

posted on 2012-11-20 15:04  蝌蚪的精神  阅读(207)  评论(0编辑  收藏  举报

导航