一、UDP实现数据传输
UDP是相当佛系的存在,对于数据发送者来说,不论接受者在与不在,我都将发送,对于数据接受者来说,不论数据好与坏,能收的,我都将接受。状态是浮云,毕竟是无状态协议,只要机子好,传输就是棒。
1.1 数据发送者
public class Server {
static class UDPServer {
DatagramSocket socket;
UDPServer() throws SocketException {
//发送端随机分配端口
this.socket = new DatagramSocket();
}
/**
*
* @param address 主机地址
* @param port 目标端口
* @param content 传输内容
* @throws IOException
*/
public void send(String address, int port, Object... content) throws IOException {
//DatagramPacket对象装载发送内容:由于UDP每次传输存在大小限制,需要采用分段传输
DatagramPacket packet;
for (Object o : content) {
byte[] bytes = o.toString().getBytes();
packet = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(address), port);
this.socket.send(packet);
}
}
}
1.2 数据接收者
public class Client {
static class UDPClient {
DatagramSocket socket;
UDPClient(int port) throws SocketException {
this.socket = new DatagramSocket(port);
}
public void receive() throws IOException {
while (true) {
//定义信息存储结构(最大为64kb)
DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);
//开始接受输入流
this.socket.receive(packet);
//获取信息
byte[] data = packet.getData();
System.out.println(new String(data));
}
}
}
}
1.3 测试
public static void main(String[] args) throws IOException {
UDPClient client = new UDPClient(8081);
client.receive();
}
public static void main(String[] args) throws IOException {
UDPServer server = new UDPServer();
String[] str = {"你好", "很高兴遇到你", "我的名字是linux"};
server.send("localhost", 8081, str);
}
二、TCP实现简易聊天
如果UDP是个佛系青年,那么TCP就是严谨的资深投资人,三次握手,四次挥手,必须得确认了再确认才给双方通信的机会,虽然慢了些,不过胜在安全可靠啊。
2.1 聊天发起者
public class Server {
static class TCPServer {
//指定发送端的端口
Socket socket;
TCPServer(String address, int port) throws IOException {
this.socket = new Socket(address, port);
}
/**
* 建立通信
*
* @param username 用户名
* @throws IOException
*/
public void connect(String username) throws IOException {
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();
//接收控制台输入
BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
//获取通道流内容
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
//向通道写入内容
PrintWriter writer = new PrintWriter(new OutputStreamWriter(out));
String line;
while (!(line = sin.readLine()).equalsIgnoreCase("再见")) {
writer.println(username + ": " + line);
//立即写入
writer.flush();
System.out.println(reader.readLine());
System.out.println(username + ": " + line);
}
writer.close();
reader.close();
sin.close();
this.socket.close();
}
}
}
2.2 聊天对象
public class Client {
static class TCPClient {
//指定发送端的端口
ServerSocket socket;
TCPClient(int port) throws IOException {
this.socket = new ServerSocket(port);
}
/**
* 建立通信
* @param username 用户名
* @throws IOException
*/
public void connect(String username) throws IOException {
Socket accept = socket.accept();
OutputStream out = accept.getOutputStream();
InputStream in = accept.getInputStream();
BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
PrintWriter writer = new PrintWriter(new OutputStreamWriter(out));
String line;
while (!(line = sin.readLine()).equalsIgnoreCase("再见")) {
writer.println(username + ": " + line);
writer.flush();
System.out.println(username + ": " + line);
System.out.println(reader.readLine());
}
writer.close();
reader.close();
sin.close();
this.socket.close();
}
}
}
2.3 聊天测试
public static void main(String[] args) throws IOException {
TCPClient client1 = new TCPClient(8082);
client1.connect("菜鸡");
}
public static void main(String[] args) throws IOException {
TCPServer server1 = new TCPServer("127.0.0.1", 8082);
server1.connect("大佬");
}