注意在实现过程中 用的是 内部类 (用来对作为通信的接受端)
整个过程中 只能用 一个 DatagramSocket ds 对象 这样才能对
如果不是同一个 ds 就不能实现交互 所以在 内部类 时 定义一个 私有的ds 作为构造函数的参数
用来在 发送 数据时 还可以同时 接受数据 在同一个 ds 对象 下;
相互之间 发送 特定消息可结束线程
import java.io.IOException;
import java.net.;
import java.util.;
public class ComputerOne {
public static final String IP = "192.168.43.170" ;
public static final int S_PORT = 8888;
public static final int R_PORT = 9999;
public static void main(String[] args) {
new ComputerOne().UdpStart();
}
private void UdpStart() {
DatagramSocket ds = null;
try {
ds = new DatagramSocket(S_PORT);
Scanner sc = new Scanner(System.in);
Receive r = new Receive(ds);
new Thread(r).start();
while(true) {
System.out.println("输入一个发送的语句:");
String msg = sc.next().trim();
byte[] buf = msg.getBytes();
InetSocketAddress ins = new InetSocketAddress(IP , R_PORT);
DatagramPacket dp = new DatagramPacket(buf , 0 ,buf.length , ins);
ds.send(dp);
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(ds != null) {
ds.close();
}
}
}
class Receive implements Runnable {
public static final int R_PORT = 9999;
private DatagramSocket ds = null;
public Receive(DatagramSocket ds) {
this.ds = ds;
}
@Override
public void run() {
try {
while(true) {
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf , 0 , buf.length);
ds.receive(dp);
String str =new String(buf , 0 , buf.length);
System.out.println("ComputerOne----->"+ str);
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(ds != null) {
ds.close();
}
}
}
}
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.util.Scanner;
public class ComputerTwo {
public static final String IP = “192.168.43.170” ;
public static final int S_PORT = 9999;
public static final int R_PORT = 8888;
public static void main(String[] args) {
new ComputerTwo().UdpStart();
}
private void UdpStart() {
DatagramSocket ds = null;
try {
ds = new DatagramSocket(S_PORT);
Scanner sc = new Scanner(System.in);
Receive2 r = new Receive2(ds);
new Thread(r).start();
while(true) {
System.out.println("输入一个发送的语句:");
String msg = sc.next().trim();
byte[] buf = msg.getBytes();
InetSocketAddress ins = new InetSocketAddress(IP , R_PORT);
DatagramPacket dp = new DatagramPacket(buf , 0 ,buf.length , ins);
ds.send(dp);
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(ds != null) {
ds.close();
}
}
}
}
class Receive2 implements Runnable {
public static final int R_PORT = 8888;
private DatagramSocket ds = null;
public Receive2(DatagramSocket ds) {
this.ds = ds;
}
@Override
public void run() {
try {
while(true) {
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf , 0 , buf.length);
ds.receive(dp);
String str =new String(buf , 0 , buf.length);
System.out.println("ComputerOne----->"+ str);
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(ds != null) {
ds.close();
}
}
}
}