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();
} } } }
<>代码运行截图(用两个终端开启)

技术
©2019-2020 Toolsou All rights reserved,
uni-app中使用 async + await 实现异步请求同步化Dart中的Isolate十分钟掌握Pytorch搭建神经网络的流程利用python对monkey日志完成自动化分析二进制模2除法(CRC循环冗余检验)vue实现pc端的自适应,rem适配希尔排序Unity面试经验(两天面六家,四个offer,济南)C++实现《走迷宫》小游戏VR、AR和MR这些技术的区别