package com.han.network.udp;
import jdk.jfr.Description;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;
import java.util.Vector;
public class UDPUtils implements Runnable {
private int waitPort;
private int receivePort;
private String ipAddress;
private boolean status = true;
private String name;
private ByteArrayOutputStream bos;
private DatagramPacket dp;
private DatagramSocket ds;
public UDPUtils(int waitPort, int receivePort, String ipAddress, String name) {
this.waitPort = waitPort;
this.receivePort = receivePort;
this.ipAddress = ipAddress;
this.name = name;
}
public boolean chatStart(Runnable t) {
System.out.println("-------开启聊天-------");
Thread receive = new Thread(t, "receive");
receive.start();
Thread send = new Thread(t, "send");
send.start();
return true;
}
@Override
public void run() {
while (status) {
if (Thread.currentThread().getName().equals("receive"))
try {
System.out.println(receiveMassage());
} catch (IOException e) {
e.printStackTrace();
}
else {
Scanner scanner = new Scanner(System.in);
String next = scanner.nextLine();
System.out.println("输入的内容:" + next);
try {
sendMassage(next);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public boolean sendMassage(String massage) throws IOException {
if (massage.getBytes().length > 64 * 1024) {
full(massage);
} else {
String mass = null;
DatagramSocket ds = new DatagramSocket();
if (name != null) {
mass = "[" + name + "][" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")) + "]: " + "「" + massage + "」";
} else {
mass = "[" + waitPort + "][" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")) + "]: " + "「" + massage + "」";
}
byte[] bytes = mass.getBytes();
dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(ipAddress), receivePort);
ds.send(dp);
ds.close();
}
return true;
}
public String receiveMassage() throws IOException {
ds = new DatagramSocket(waitPort);
byte[] bytes = new byte[64 * 1024];
DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
ds.receive(dp);
int length = dp.getLength();
ds.close();
return new String(bytes, 0, length);
}
public void full(String massage) throws IOException {
bos = new ByteArrayOutputStream();
byte[] bytes = massage.getBytes();
bos.write(bytes, 0, bytes.length);
bos.flush();
byte[] temp1 = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(temp1);
byte[] temp2 = new byte[60 * 1024];
int len = 0;
while ((len = bis.read(temp2)) != -1) {
sendMassage(new String(temp2, 0, len));
}
bos.close();
}
public boolean sendMassage(String massage, String ip, int sendPort) throws IOException {
String mass;
DatagramSocket ds1 = new DatagramSocket();
if (name != null) {
mass = "[" + name + "][" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")) + "]: " + "「" + massage + "」";
} else {
mass = "[" + waitPort + "][" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")) + "]: " + "「" + massage + "」";
}
byte[] bytes = mass.getBytes();
dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(ip), sendPort);
ds1.send(dp);
ds1.close();
return true;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public UDPUtils(int port) throws SocketException {
this.waitPort = port;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
public int getReceivePort() {
return receivePort;
}
public void setReceivePort(int receivePort) {
this.receivePort = receivePort;
}
public UDPUtils(int waitPort, int receivePort, String name, DatagramPacket dp, DatagramSocket ds) throws SocketException {
this.waitPort = waitPort;
this.receivePort = receivePort;
this.name = name;
this.dp = dp;
this.ds = ds;
}
public UDPUtils(int waitPort, String name) throws SocketException {
this.waitPort = waitPort;
this.name = name;
}
public int getWaitPort() {
return waitPort;
}
public void setWaitPort(int waitPort) {
this.waitPort = waitPort;
}
public DatagramPacket getDp() {
return dp;
}
public void setDp(DatagramPacket dp) {
this.dp = dp;
}
public DatagramSocket getDs() {
return ds;
}
public void setDs(DatagramSocket ds) {
this.ds = ds;
}
public UDPUtils() throws SocketException {
}
public UDPUtils(int port, DatagramPacket dp, DatagramSocket ds) throws SocketException {
this.waitPort = port;
this.dp = dp;
this.ds = ds;
}
public String getIpAddress() {
return ipAddress;
}
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
}
|