zoukankan      html  css  js  c++  java
  • UDP 编程 客服咨询回复

    package 网络编程_客户咨询;
    
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    import java.net.UnknownHostException;
    import java.util.Scanner;
    
    public class sender {
    
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            Scanner input = new Scanner(System.in);
    
            do{
            InetAddress ia = null;
            ia = InetAddress.getByName("localhost");
            System.out.println("input a message:");
    
            String mes = input.next();
            DatagramPacket dp = new DatagramPacket(mes.getBytes(),mes.length(),ia,8806);
            DatagramSocket ds = new DatagramSocket();
            ds.send(dp);
            System.out.println("ok!sending..");
        //-------------接受------
            byte[] buf= new byte[1024];
            DatagramPacket dp1 = new DatagramPacket(buf, 1024);
            DatagramSocket ds1 = new DatagramSocket(8806);
            ds1.receive(dp1);
            String mee = new String(dp1.getData(),0,dp1.getLength());
            System.out.println(mee);
            System.out.println("receive session over!");
            ds.close();
            }while(true);
        }
    
    }
    
    package 网络编程_客户咨询;
    
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    import java.util.Scanner;
    
    public class receiver {
    
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            Scanner input = new Scanner(System.in);
            do{
            System.out.println("Session begin:");
            byte[] buf= new byte[1024];
            DatagramPacket dp = new DatagramPacket(buf, 1024);
            DatagramSocket ds = new DatagramSocket(8806);
            ds.receive(dp);
            String mee = new String(dp.getData(),0,dp.getLength());
            System.out.println(mee);
            System.out.println("receive session over!");
    //-------------------------发送回复---------------------
            System.out.println("please reply:");
            String reply = input.nextLine();
            InetAddress ia = null;
            ia = InetAddress.getByName("localhost");
            DatagramPacket dpr = new DatagramPacket(reply.getBytes(),reply.length(),ia,8806);
            ds.send(dpr);
            ds.close();
            }while(true);
    
        }
    
    }
    
  • 相关阅读:
    pageControl点击白点时会变
    valueForKeyPath获取对象数组的属性
    探讨:通过循环数组或者集合,插入数据库中没有的数据
    Mybatis 中 Oracle 的拼接模糊查询
    Spring boot 中 Mybatis Plus 在 Oracle 新增数据时,主键自增问题
    在IntelliJ IDEA中,SpringBoot项目通过devtools实现热部署
    在 Mybatis 中遇到的那些坑
    华硕飞行堡垒耳机插进去之后再拔出来,电脑就没有声音了
    nyoj 776 删除元素
    nyoj 14 会场安排问题(贪心专题)java
  • 原文地址:https://www.cnblogs.com/mrcharles/p/11879959.html
Copyright © 2011-2022 走看看