21天养成好习惯

21天养成好习惯,第1张

21天养成好习惯 21天养成好习惯_第十四天 今天主要完成了Java的TDP网络编程的实例

参考视频: 狂神说Java的网络实战视频

1. 先实现TalkSend.java
package Net_Study.ChatRoom;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;

public class TalkSend implements Runnable{
    DatagramSocket socket = null;
    BufferedReader reader = null;

    private int fromPort;
    private String toIp;
    private int ToPort;

    public TalkSend(int fromPort, String toIp,int ToPort){
        this.fromPort = fromPort;
        this.toIp = toIp;
        this.ToPort = ToPort;
        try{
            socket = new DatagramSocket(fromPort); //创建一个端口socket
            //创建一个DatagramSocket实例,并将该对象绑定到本机默认IP地址、指定端口。
            reader = new BufferedReader(new InputStreamReader(System.in));
            //从键盘输入
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    @Override
    public void run() {

        while (true){
            try {
                String data = reader.readLine();
                byte[] datas = data.getBytes(StandardCharsets.UTF_8);
                DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress(this.toIp,this.ToPort));
                //发送方的IP和port
                socket.send(packet);
                if(data.equals("bye")){
                    break;
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        socket.close();
    }
}

使用BufferedReader的原因

缓冲区读取内容,避免中文乱码
一:使用StringBuffer类不停的连接readLine()从每次读取的一行内容, 直至读取的为null为止。然后进行输出。
二:使用StringBuffer类不停的连接read()方法读取到的每一个数字转化后的字符。然后进行输出。
参考资料:BufferedReader

2.实现TalkReceive.java
package Net_Study.ChatRoom;

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

public class TalkReceive implements Runnable{

    DatagramSocket socket = null;
    private int port;
    private String msgFrom;

    public TalkReceive(int port, String msgFrom) {
        this.port = port;
        this.msgFrom = msgFrom;
        try{
            socket = new DatagramSocket(port); //创建一个port用于接收信息
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {

        while (true){
            try{
                //准备接收包裹
                byte[] container = new byte[1024];
                DatagramPacket packet = new DatagramPacket(container,0,container.length);
                socket.receive(packet); //阻塞式接收包裹 写入到packet中

                //断开连接bye
                byte[] data = packet.getData();
//                System.out.println("packet.length"+packet.getLength());
                String receiveData = new String(data,0,packet.getLength());

                System.out.println(msgFrom + ": " + receiveData);

                if (receiveData.equals("bye")){
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        socket.close();
    }
}

实现学生类和老师类

Teacher.java

package Net_Study.ChatRoom;

public class Student {
    public static void main(String[] args) {
        //开启两个线程
        new Thread(new TalkSend(7777,"localhost",9999)).start();
        //发送: 从 7777 -> 9999
        new Thread(new TalkReceive(8888,"老师")).start();
        //接收: 接收所有传入8888端口的消息
    }
}

老师

package Net_Study.ChatRoom;

public class Teacher {
    public static void main(String[] args) {
        new Thread(new TalkSend(5555,"localhost",8888)).start();
        //发送 5555 -> 8888
        new Thread(new TalkReceive(9999,"学生")).start();
        //接收9999 端口的所有 数据
    }
}

运行结果:


其他知识

学习了离散数学 , C++的一些知识

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5077497.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-16
下一篇 2022-11-16

发表评论

登录后才能评论

评论列表(0条)

保存