UDP案例_在线咨询

TalkReceiver 
package 网络编程;

import java.io.BufferedReader;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class TalkReceiver implements Runnable {
    private DatagramSocket server;
    private String from;
    public TalkReceiver(int port,String from)
    {
        this.from = from;
        try {
        server = new DatagramSocket(port);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
    
    @Override
    public void run() {
        // TODO Auto-generated method stub
        while(true)
        {
            byte[]container = new byte[1024*60];
            DatagramPacket packet = new DatagramPacket (container,0,container.length);
            try
            {
                server.receive(packet);
                byte[]datas = packet.getData();
                int len = packet.getLength();
                String str = new String(datas,0,len);
                System.out.println(from + ":" + str);
                if(str.equals("bye")) {
                    break;
                }    
            }catch(IOException e)
            {
                e.printStackTrace();
            }
        }
        server.close();
    }
}

 

TalkSend 
package 网络编程;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;

public class TalkSend implements Runnable {
    private DatagramSocket client;
    private BufferedReader reader;
    private String toIP; 
    private int toPort; 
    public TalkSend(int port,String toIP, int toPort) {
        this.toIP = toIP;
        this.toPort = toPort;
        try{
            client = new DatagramSocket(port);
            reader = new BufferedReader(new InputStreamReader(System.in));
        }catch(SocketException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        while(true)
        {
            String data;
            try
            {
                data = reader.readLine();
                byte []datas = data.getBytes();
                DatagramPacket packet = new DatagramPacket (datas,0,datas.length,new InetSocketAddress(this.toIP,this.toPort));
                client.send(packet);
                if(data.equals("bye"))
                    break;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        client.close();
    }
}
TalkStudent 
package 网络编程;
//加入多线程  实现双向交流  模拟在线咨询
public class TalkStudent {

    public static void main(String[] args) {
        
        new Thread(new TalkSend(7777,"localhost",9999)).start();//发送
        new Thread(new TalkReceiver(8888,"学生说")).start();//接收
    }
}

 

TalkTeacher

package 网络编程;

public class TalkTeacher {
    public static void main(String[] args) {
        new Thread(new TalkReceiver(9999,"老师说")).start();//接收
        new Thread(new TalkSend(5555,"localhost",8888)).start();//发送
    }
}

这边还有一个就是   在控制台那边有个新建console的按钮 多建一个  一共是两个   运行老师这个程序  运行学生这个程序 ,旁边还有一个选择是哪个程序 就可以看到了

 

结果如图所示

 

posted @ 2020-12-27 11:19  冰糖葫芦很乖  阅读(141)  评论(0编辑  收藏  举报