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,
Solve in servlet The Chinese output in is a question mark C String function and character function in language MySQL management 35 A small coup optimization Java performance —— Concise article Seven sorting algorithms (java code ) use Ansible Batch deployment SSH Password free login to remote host according to excel generate create Build table SQL sentence Spring Source code series ( sixteen )Spring merge BeanDefinition Principle of Virtual machine installation Linux course What are the common exception classes ?