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();            
            }
    
        }
    
    }
  • 相关阅读:
    2019互联网安全城市巡回赛·西安站圆满收官
    跨域漏洞丨JSONP和CORS跨域资源共享
    浅谈URL跳转与Webview安全
    事务嵌套的问题
    小代码编写神器:LINQPad 使用入门
    重构指导之一
    视频的文件格式、压缩格式、码率、分辨率
    Asp.Net中自以为是的Encode
    Solution Explorer 和 Source Control Explorer 的 View History 异同
    借助 Resharper 和 StyleCop 让代码更整洁
  • 原文地址:https://www.cnblogs.com/xiaotaoxu/p/5536591.html
Copyright © 2011-2022 走看看