基于UDP的编程与在线客服聊天案例

发送方: DatagramSocket  数据包:DatagramPacket
发送方: DatagramSocket  数据包:DatagramPacket

//发送端

import java.io.IOException;
import java.net.*; 
public class TestSend {//发送方 
    public static void main(String[] args) throws IOException {
        System.out.println("Send端上线并开始发送你好");
        //1.创建套接字;//指定端口是发送方的端口
        DatagramSocket ds=new DatagramSocket(8888);
        //2.创建数据包
        //发送:"你好"
        String str="你好";
        byte[] b=str.getBytes();
        DatagramPacket dp=new DatagramPacket(b,b.length, InetAddress.getByName("Localhost"),9999); 
        //3.发送数据包
        ds.send(dp); 
        //4.关闭资源
        ds.close(); 
    }
}
//接收方

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket; 
public class TestReceive {//接收方 
    public static void main(String[] args) throws IOException {
        System.out.println("Receive端上线");
        //1.创建套接字,并且指定接收方的端口号
        DatagramSocket ds =new DatagramSocket(9999);
        //2.接受数据包
        byte[] b=new byte[1024];
        DatagramPacket dp=new DatagramPacket(b,b.length);//数据包是空的
        //3.接收数据:将数据放入数据包
        ds.receive(dp);//这个数据包还是空的么?不是
        String str=new String(dp.getData());//将getData得到的字节数组转换为String类型 
        System.out.println("Send类给我发出:"+str); 
        //关闭资源
        ds.close(); 
    }
}
//发送方
import java.io.IOException;
import java.net.*;
import java.util.Scanner; 
public class TestSend {//发送方
    public static void main(String[] args) throws IOException {
        System.out.println("Send端上线并开始发送你好");
        //1.创建套接字;//指定端口是发送方的端口
        DatagramSocket ds=new DatagramSocket(8888);
        //2.创建数据包
        //发送:
        Scanner sc =new Scanner(System.in);
        System.out.print("Send端说");
        String str=sc.next();
        byte[] b=str.getBytes();
        DatagramPacket dp=new DatagramPacket(b,b.length, InetAddress.getByName("Localhost"),9999);
        //3.发送数据包
        ds.send(dp); 
        //2.接受数据包
        byte[] by=new byte[120];
        DatagramPacket dp2=new DatagramPacket(by,by.length);//数据包是空的
        //3.接收数据:将数据放入数据包
        ds.receive(dp2);//这个数据包还是空的么?不是
        String s=new String(dp2.getData());//将getData得到的字节数组转换为String类型
        System.out.println("TestReceive类给我发出:"+s); 
        //4.关闭资源
        ds.close();
    }
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner; 
public class TestReceive {//接收方
    public static void main(String[] args) throws IOException {
        System.out.println("Receive端上线");
        //1.创建套接字,并且指定接收方的端口号
        DatagramSocket ds =new DatagramSocket(9999);
        //2.接受数据包
        byte[] b=new byte[120];
        DatagramPacket dp=new DatagramPacket(b,b.length);//数据包是空的
        //3.接收数据:将数据放入数据包
        ds.receive(dp);//这个数据包还是空的么?不是
        String str=new String(dp.getData());//将getData得到的字节数组转换为String类型
        System.out.println("TestSend类给我发出:"+str);
        //接收端回复
        Scanner sc=new Scanner(System.in);
        System.out.print("Receive说:");
        String s=sc.next();
        byte[] by=s.getBytes();
        DatagramPacket dp2=new DatagramPacket(by,by.length, InetAddress.getByName("Localhost"),8888);
        //发送数据包
        ds.send(dp2);
        //关闭资源
        ds.close();
    }
}
//先启动的接收方
import java.io.IOException;
import java.net.*;
import java.util.Scanner;
public class TestReceive {//接收方
    public static void main(String[] args){
        System.out.println("Receive端上线");
        //1.创建套接字,并且指定接收方的端口号
        DatagramSocket ds = null;
        try {
            ds = new DatagramSocket(9999);
            while(true){
                //2.接受数据包
                byte[] b=new byte[120];
                DatagramPacket dp=new DatagramPacket(b,b.length);//数据包是空的
                //3.接收数据:将数据放入数据包
                ds.receive(dp);//这个数据包还是空的么?不是
                String str=new String(dp.getData(),0,dp.getLength());//将getData得到的字节数组转换为String类型
                System.out.println("TestSend类给我发出:"+str);
                if("byebye".equals(str)){
                    System.out.println("老师也下线");
                    break;
                }
                //接收端回复
                Scanner sc=new Scanner(System.in);
                System.out.print("Receive说:");
                String s=sc.next();
                byte[] by=s.getBytes();
                DatagramPacket dp2=new DatagramPacket(by,by.length, InetAddress.getByName("Localhost"),8888);
                //发送数据包
                ds.send(dp2);
            }
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            ds.close();
        }
    }
}
//后启动先结束的发送方
import java.io.IOException;
import java.net.*;
import java.util.Scanner;
public class TestSend {//发送方
    public static void main(String[] args) {
        System.out.println("Send端上线并开始发送你好");
        //1.创建套接字;//指定端口是发送方的端口
        DatagramSocket ds= null;
        try {
            ds = new DatagramSocket(8888);
            while(true){
                //2.创建数据包
                //发送:
                Scanner sc =new Scanner(System.in);
                System.out.print("Send端说");
                String str=sc.next();
                byte[] b=str.getBytes();
                DatagramPacket dp=new DatagramPacket(b,b.length, InetAddress.getByName("Localhost"),9999);
                //3.发送数据包
                ds.send(dp);
                if("byebye".equals(str)){
                    System.out.println("学生下线");
                    break;
                }
                //接受数据包
                byte[] by=new byte[120];
                DatagramPacket dp2=new DatagramPacket(by,by.length);//数据包是空的
                //3.接收数据:将数据放入数据包
                ds.receive(dp2);//这个数据包还是空的么?不是
                String s=new String(dp2.getData(),0,dp2.getLength());//将getData得到的字节数组转换为String类型
                System.out.println("TestReceive类给我发出:"+s);
            }
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭资源
            ds.close();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值