UDP编程
UDPServer.java
package socket.udp;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UDPServer {
public static void main(String[] args) {
byte[] bytes = new byte[8192]; // 缓冲区,用来接收信息
DatagramPacket dp = new DatagramPacket(bytes,0,bytes.length);// 创建一个数据包对象
try {
DatagramSocket ds = new DatagramSocket(8888); // 开启一个的端口,用于UDP编程
ds.receive(dp); // 收信息:存入bytes里
} catch (IOException e) {
e.printStackTrace();
}
// 将byte数组转化为String字符串
String message = new String(bytes,0,dp.getLength());
System.out.println("收到的消息:" + message);
}
}
UDPClient.java
package socket.udp;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPClient {
public static void main(String[] args) {
String message = "消息";
byte[] bytes = message.getBytes();
DatagramPacket dp = new DatagramPacket(bytes,0,bytes.length); // 把消息封装为一个数据包发送给服务端
try {
// 数据包,封装了消息,设置接收方的ip+port
dp.setAddress(InetAddress.getLocalHost());
dp.setPort(8888);
DatagramSocket ds = new DatagramSocket();
ds.send(dp);// 由DatagramSocket类对象去发送,socket就相当于两端的运输车
} catch (IOException e) {
e.printStackTrace();
}
}
}
TCP
下面代码是一个基于服务器和客户端的软件自动升级的一个原理,运用了多线程和基于TCP的Socket编程,因为传输文件需要可靠的通信,仅供参考和初学者学习,也是给我(初学者)加深印象的哈哈
Server.java
package FileAutoUpgrade;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
ServerSocket ss = null;
try {
ss = new ServerSocket(8888); //ServerSocket开启一个端口给另一端连接
while (true){
System.out.println("等待客户端连接.....");
Socket socket = ss.accept(); // 阻塞函数,用来等待另一端的连接请求
InputStream is = socket.getInputStream(); // socket获取一个输入流,用来获取客户端发来的当前版本号
DataInputStream dis = new DataInputStream(is); // 数据流包装
String versionNumber = dis.readUTF(); // 获取从客户端发来的版本号,再去特定目录下查找比对最新的版本
if(!"v8.5".equals(versionNumber)){ // 如果是旧的版本发送一个新的版本,如果是最新的就持续监测,不用跳出循环
continue;
}else {
SendFileThread sft = new SendFileThread(socket); // 否则开启一个子线程发送给客户文件
sft.start();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 到特定文件夹下查找最新的版本文件,这里逻辑作为测试,不是实际代码
public static String getNewVersion(File file){
String newVersion = null;
File[] files = file.listFiles();
for (File f:
files) {
newVersion = f.getName();
}
return newVersion;
}
}
class SendFileThread extends Thread{
private Socket socket;
public SendFileThread(Socket socket){
this.socket = socket;
}
@Override
public void run() {
/*
* 字节流,服务器发送最新版本
*/
try {
OutputStream outputStream = socket.getOutputStream(); // 输出流发送最新版本号给客户端
File file = new File("D:\\springboot-study\\Demo01\\src\\FileAutoUpgrade\\version");
String newFileName = Server.getNewVersion(file);
// 先发送最新版本号过给客户端
DataOutputStream dos = new DataOutputStream(outputStream);
dos.writeUTF(newFileName);
FileInputStream fileInputStream = new FileInputStream("D:\\springboot-study\\Demo01\\src\\FileAutoUpgrade\\version\\" + newFileName);
byte[] bytes = new byte[1024];
int a = 0;
while ((a = fileInputStream.read(bytes)) != -1) {
outputStream.write(bytes);
}
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client.java
package FileAutoUpgrade;
import java.io.*;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost",8888); // 和服务器建立连接,成功后发送当前版本给服务器
OutputStream os = socket.getOutputStream();// 发送用输出流
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF("v8.5"); // 客户端启动自动发送版本号给服务端
dos.flush();
// 这里应该要有一个交互的过程,如果不为最新的,问用户是否升级为最新版本,这里测试用
GetFileThread getFileThread = new GetFileThread(socket);
getFileThread.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class GetFileThread extends Thread{
private Socket socket;
public GetFileThread(Socket socket){
this.socket = socket;
}
@Override
public void run() {
InputStream inputStream = null;
try {
inputStream = socket.getInputStream(); // 从socket获得一个输入流,接收服务端传来的新版本文件
// 收新版本文件之前先收到新的版本号
DataInputStream dis = new DataInputStream(inputStream);
String newFileName = dis.readUTF();
System.out.println(newFileName);
FileOutputStream fos = new FileOutputStream("D:\\springboot-study\\Demo01\\src\\FileAutoUpgrade\\localversion\\" + newFileName); // 操作本地的文件
byte[] buff = new byte[1024];
int a = 0;
while ((a = inputStream.read(buff))!=-1){
fos.write(buff);// 把服务端接收来的新版本文件写入本地
}
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}