码迷,mamicode.com
首页 > 其他好文 > 详细

UDP案例_在线咨询

时间:2020-12-31 12:48:49      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:reader   method   一个   gen   over   his   stream   div   cat   

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的按钮 多建一个  一共是两个   运行老师这个程序  运行学生这个程序 ,旁边还有一个选择是哪个程序 就可以看到了

 

结果如图所示

技术图片

 

UDP案例_在线咨询

标签:reader   method   一个   gen   over   his   stream   div   cat   

原文地址:https://www.cnblogs.com/Galesaur-wcy/p/14196715.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!