zoukankan      html  css  js  c++  java
  • 45.客户咨询问题

    package cn.com.pb.ask;
    import java.io.IOException;
    import java.net.*;
    public class Receive {
        public static void main(String[] args) {
            DatagramPacket dp=null;
            DatagramSocket ds=null;
            DatagramPacket dpto=null;
            try{
                //创建DatagramPacket对象,用来准备接收数据包
                byte[] buf=new byte[1024];        
                dp=new DatagramPacket(buf,buf.length);
                //创建DatagramSocket对象,接收数据
                ds=new DatagramSocket(8800);
                ds.receive(dp);
                //显示接收到的信息
                String mess=new String(dp.getData(),0,dp.getLength());
                System.out.println(dp.getAddress().getHostAddress()+"说:"+mess);
                
                String reply="你好,我在,请咨询!";    
                //显示与本地对话框
                System.out.println("我  说:"+reply);            
                            
                //创建DatagramPacket对象,封装数据
                SocketAddress sa=dp.getSocketAddress();
                dpto=new DatagramPacket(reply.getBytes(),reply.getBytes().length ,sa);
                ds.send(dpto);            
                
            }catch (IOException e) {            
                e.printStackTrace();
            }finally{
                ds.close();
            }
        }
    }
    package cn.com.pb.ask;
    import java.io.IOException;
    import java.net.*;
    public class Send {
    
        public static void main(String[] args) {
            InetAddress ia=null;
            DatagramSocket ds=null;
            
            try{            
                String mess="你好,我想咨询一个问题。";    
                //显示与本地对话框
                System.out.println("我  说:"+mess);
                //获取本地主机地址
                ia=InetAddress.getByName("localhost");
                //创建DatagramPacket对象,封装数据
                DatagramPacket dp=new DatagramPacket(mess.getBytes(),mess.getBytes().length ,ia,8800);
                //创建DatagramSocket对象,向服务器发送数据
                ds=new DatagramSocket();
                ds.send(dp);    
                
                byte[] buf=new byte[1024];        
                DatagramPacket dpre=new DatagramPacket(buf,buf.length);
                //创建DatagramSocket对象,接收数据
                //ds=new DatagramSocket(8800);
                ds.receive(dpre);
                //显示接收到的信息
                String reply=new String(dpre.getData(),0,dpre.getLength());
                System.out.println(dpre.getAddress().getHostAddress()+"说:"+reply);
                
            }catch (UnknownHostException e) {            
                e.printStackTrace();
            } catch (IOException e) {            
                e.printStackTrace();
            }finally{
                //关闭DatagramSocket对象
                ds.close();            
            }
    
        }
    
    }
  • 相关阅读:
    06软件构架实践阅读笔记之六
    05软件构架实践阅读笔记之五
    04软件构架实践阅读笔记之四
    03软件构架实践阅读笔记之三
    02软件构架实践阅读笔记之二
    01软件构架实践阅读笔记之一
    实训第四天
    实训第三天
    实训第二天
    实训第一天
  • 原文地址:https://www.cnblogs.com/xiaotaoxu/p/5536591.html
Copyright © 2011-2022 走看看