欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

假设Tom和Jerry利用Java UDP进行聊天

程序员文章站 2022-03-09 07:56:00
1-1 假设Tom和Jerry利用Java UDP进行聊天,请为他们编写程序。具体如下:(1)、Tom和Jerry聊天的双方都应该具有发送端和接收端;(2)、利用DatagramSocket与DatagramPacket;(3)、实现 java.lang.Runnable类,重写 run()方法。Jerry代码:public class UDPSocket_Jerry { public class UDPSocket_Jerry { public static void main(S...

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

Jerry代码:

public class UDPSocket_Jerry {
   public class UDPSocket_Jerry {
    public static void main(String[] args) {
        //分别启动发信息线程和收消息线程
        Thread thread1 = new Thread(new UDPSocketClientthread());
        Thread thread2 = new Thread(new UDPSocketServerthread());
        thread1.start();
        thread2.start();
    }
}
public class UDPSocketClientthread implements Runnable {
    DatagramSocket datagramSocket;

    {
        try {
            datagramSocket = new DatagramSocket(8081);
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }

    byte[] ibuf = new byte[1024];
    DatagramPacket datagramPacket = new DatagramPacket(ibuf, ibuf.length);


    @Override
    public void run() {
        while (true) {
            try {
                datagramSocket.receive(datagramPacket);
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("Tom:" + new String(datagramPacket.getData(), 0, datagramPacket.getLength()));
        }
    }
}
import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class UDPSocketServerthread implements Runnable {
    byte[] ibuf = new byte[1024];
    String message;
    Scanner scanner = new Scanner(System.in);
    DatagramPacket datagramPacket;

    {
        try {
            datagramPacket = new DatagramPacket(ibuf, ibuf.length, InetAddress.getByName("127.0.0.1"), 2020);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    DatagramSocket ds;

    {
        try {
            ds = new DatagramSocket(8080, InetAddress.getByName("127.0.0.1"));
        } catch (SocketException | UnknownHostException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while (true) {
            message = scanner.nextLine();
            datagramPacket.setData(message.getBytes());
            try {
                assert ds != null;
                ds.send(datagramPacket);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Tom代码:

public class UDPSocket_Tom {
    public static void main(String[] args) {
        Thread thread1 = new Thread(new UDPSocketClientthread());
        Thread thread2 = new Thread(new UDPSocketServerthread());
        thread1.start();
        thread2.start();
    }
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UDPSocketClientthread implements Runnable{
        DatagramSocket datagramSocket;
    {
        try {
            datagramSocket = new DatagramSocket(2020);
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }
        byte[] ibuf = new byte[1024];
        DatagramPacket datagramPacket = new DatagramPacket(ibuf, ibuf.length);


    @Override
    public void run() {
        while(true){
            try {
                datagramSocket.receive(datagramPacket);
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("Jerry:"+new String(datagramPacket.getData(),0,datagramPacket.getLength()));
        }
    }
}
import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class UDPSocketServerthread implements Runnable{
    byte[] ibuf = new byte[1024];
    String message;
    Scanner scanner = new Scanner(System.in);
    DatagramPacket datagramPacket;

    {
        try {
            datagramPacket = new DatagramPacket(ibuf, ibuf.length, InetAddress.getByName("127.0.0.1"), 8081);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
    DatagramSocket ds;
    {
        try {
            ds = new DatagramSocket(8083, InetAddress.getByName("127.0.0.1"));
        } catch (SocketException | UnknownHostException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while (true) {
            message = scanner.nextLine();
            datagramPacket.setData(message.getBytes());
            try {
                assert ds != null;
                ds.send(datagramPacket);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

代码运行截图(用两个终端开启)

假设Tom和Jerry利用Java UDP进行聊天

本文地址:https://blog.csdn.net/Jackyyl729/article/details/110008831