1-1 hypothesis Tom and Jerry utilize Java UDP Chat , Please program them . The details are as follows :
(1),Tom and Jerry Both sides of the chat should have sender and receiver ;
(2), utilize DatagramSocket And DatagramPacket;
(3), realization java.lang.Runnable class , rewrite run() method .

<>Jerry code :
public class UDPSocket_Jerry { public class UDPSocket_Jerry { public static
void main(String[] args) { // Start sending message thread and receiving message thread respectively 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 code :
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();
} } } }
<> Screenshot of code running ( Turn on with two terminals )

Technology
©2019-2020 Toolsou All rights reserved,
switch Statement programming : Give a percentage score , Output grade required A,B,C,D,E.Jetson Nano read GPS data (TTL turn USB)yolov3 Detailed explanation - model structure Android Mobile application foundation learning —— Chapter 1 basic introduction Four years of programming , Or is it inferior Database design AWS Elastic Load Balancing (Amazon ELB) Study notes el-select Parameter problem From outsourcing testing to Alibaba , A girl of three books C++STL Container summary