【发布时间】:2020-06-23 06:00:09
【问题描述】:
如何在Java中使用“TCP紧急数据”实现传输控制。
我实现了一个客户端-服务器应用程序,用于使用 TCP 协议传输文件。服务器是并行的。还需要使用紧急数据来实现传输控制。我在网上没有找到关于Java的解决方案。
服务器类:
import javafx.beans.binding.Bindings;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private static final String FILE_PATH_SERVER = "C:\\Users\\anduser\\IdeaProjects\\Shafarenko_LR1\\src\\main\\resources\\fileServer.txt";
public static final File FILE_SERVER = new File(FILE_PATH_SERVER);
private ServerSocket serverSocket;
public void start(int port) throws IOException {
serverSocket = new ServerSocket(port);
while (true)
new ClientHandler(serverSocket.accept()).start();
}
public void stop() throws IOException {
serverSocket.close();
}
private static class ClientHandler extends Thread {
private Socket clientSocket;
private DataOutputStream out;
private FileInputStream in;
public ClientHandler(Socket socket) {
this.clientSocket = socket;
}
public void run() {
try {
out = new DataOutputStream(clientSocket.getOutputStream());
out.writeInt((int) FILE_PATH_SERVER.length());
} catch (IOException e) {
e.printStackTrace();
}
try {
in = new FileInputStream(FILE_PATH_SERVER);
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
byte buf[] = new byte[8];
int len = 0;
try {
len = in.read(buf);
} catch (IOException e) {
e.printStackTrace();
}
if (len == -1) {
break;
}
try {
out.write(buf, 0, len);
} catch (IOException e) {
e.printStackTrace();
}
try {
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户端类:
import org.apache.commons.lang3.RandomStringUtils;
import java.io.*;
import java.net.Socket;
public class Client {
private String generatedFileClient = RandomStringUtils.randomAlphanumeric(10) + ".txt";
private String FILE_PATH_CLIENT = "C:\\Users\\anduser\\IdeaProjects\\Shafarenko_LR1\\src\\test\\resources\\" + generatedFileClient;
private Socket clientSocket;
private FileOutputStream out;
private DataInputStream in;
private File fileCilent;
public File getFileClient() {
return new File(FILE_PATH_CLIENT);
}
public void getFile() throws IOException {
int i = 0;
int len;
byte buf[] = new byte[8];
int fileSize;
fileSize = in.readInt();
while (i < fileSize) {
len = in.read(buf);
if (len == -1) {
break;
}
i += len;
out.write(buf, 0, len);
out.flush();
}
out.close();
}
public void startConnection(String ip, int port) throws IOException {
clientSocket = new Socket(ip, port);
out = new FileOutputStream(FILE_PATH_CLIENT);
in = new DataInputStream(clientSocket.getInputStream());
}
public void stopConnection() throws IOException {
in.close();
out.close();
clientSocket.close();
}
}
测试:
public class TestClient {
@Test(threadPoolSize = 10, invocationCount = 1000, timeOut = 0)
public void givenClient() throws IOException, InterruptedException {
SoftAssert softAssert = new SoftAssert();
Client client = new Client();
client.startConnection("127.0.0.1", 555);
client.getFile();
softAssert.assertTrue(FileUtils.contentEquals(Server.FILE_SERVER, client.getFileClient()), "The files differ!");
client.stopConnection();
softAssert.assertAll();
}
}
【问题讨论】:
-
也许您正在寻找错误的术语。您如何定义“紧急数据”以及您预计会发生什么?
-
能否请您通过服务器和客户端的控制台输出并进行测试?还有原始和生成的文件内容。
-
我的回答对您有帮助还是有歧义?
标签: java sockets networking tcp network-programming