计网实验一 假设Tom和Jerry利用Java UDP进行聊天,请为他们编写程序。具体如下:

计网实验一 假设Tom和Jerry利用Java UDP进行聊天,请为他们编写程序。具体如下:,第1张

计网实验一 假设Tom和Jerry利用Java UDP进行聊天,请为他们编写程序。具体如下:

一【实验目的】

  1. 掌握基于多线程的复杂网络程序的开发方法和开发流程;
  2. 按照要求设计实现软件,以培养应用复杂计算系统开发的能力,并在设计和开发过程中体现出创新意识。
    二【实验要求】
    以下每个实验均要求:
  3. “实验源代码”处:粘贴所编写的程序源码,务必添加关键语句的注释;
  4. “实验结果”:截图(包括编写的程序和运行结果)粘贴到“实验结果”下方,截图需看到本人的名字及学号;
  5. 有“讨论”的题目,请务必认真回答;

二【实验内容】
1-1 假设Tom和Jerry利用Java UDP进行聊天,请为他们编写程序。具体如下:
(1)、Tom和Jerry聊天的双方都应该具有发送端和接收端;
(2)、利用DatagramSocket与DatagramPacket;
(3)、实现 java.lang.Runnable类,重写 run()方法。

Sender.java

package net;

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

public class Sender implements Runnable{
    private DatagramSocket sender;
    private BufferedReader reader;
    private DatagramPacket packet;
    private static final int MAX_RECEIVE_BUFFER = 1024;
    private int localPort;//本进程端口
    private String targetIP;//目标主机IP
    private int targetPort;//目标主机端口

    public Sender(int localPort,String targetIP,int targetPort){
        this.localPort = localPort;
        this.targetIP = targetIP;
        this.targetPort = targetPort;
        try {
            sender = new DatagramSocket(localPort);//创建发送端
            reader = new BufferedReader(new InputStreamReader(System.in));//创建文本输入器
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    @Override
    //接收数据包
    public void run() {
        String input;
        try {
            while (true) {
                input = reader.readLine();
                byte[] datas = input.getBytes();
                packet = new DatagramPacket(datas,0,datas.length,
                        new InetSocketAddress(this.targetIP,this.targetPort));
                sender.send(packet);
                if (input.equals("exit")||input.equals("bye")){
                    System.out.println("聊天结束");
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        sender.close();
        try {
            if(reader!=null){
                reader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Receiver.java

package net;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Receiver implements Runnable {
    private DatagramSocket receiver;
    private DatagramPacket packet;
    private String name;
    private int local;//本进程端口
    private static final int MAX_RECEIVE_BUFFER = 1024;
    public Receiver(int local,String targetName){
        this.local = local;
        this.name = targetName;
    }

    @Override
    //接收数据包
    public void run() {
        try {
            receiver = new DatagramSocket(local);//创建接收端
            while (true) {
                byte[] buf = new byte[1024*60];
                DatagramPacket data = new DatagramPacket(buf,0,buf.length);
                receiver.receive(data);//阻塞
                String input = new String(data.getData(),0,data.getLength());
                System.out.println(name+":"+input);
                if (input.equals("exit")||input.equals("bye")){
                    System.out.println("聊天结束");
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        receiver.close();
    }
}

Jerry.java

package net;

public class Jerry {
    public static void main(String[] args) {
        System.out.println("洪致远 201931072217");
        Thread thread1=new Thread(new Sender(4396,"localhost",2880));
        Thread thread2=new Thread(new Receiver(7777,"Tom"));
        thread1.start();
        thread2.start();
    }
}

Tom.java

package net;

public class Tom {
    public static void main(String[] args) {
        System.out.println("洪致远 201931072217");
        Thread thread1=new Thread(new Receiver(2880,"Jerry"));
        Thread thread2=new Thread(new Sender(6666,"localhost",7777));
        thread1.start();
        thread2.start();
    }
}

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存