gpt4 book ai didi

java - 套接字客户端-服务器在发送到服务器端后卡住

转载 作者:行者123 更新时间:2023-12-01 21:55:34 24 4
gpt4 key购买 nike

我需要为我的项目制作一个带有套接字的文件传输应用程序。到目前为止,我已经编写了一个简单的客户端-服务器通信,但是每当我尝试从客户端输出接收字符数据到服务器输入时,我的代码就会锁定。代码如下:

服务器

public class ClientServer extends Thread {

Socket connection;
File file;

public ClientServer (Socket connection){
this.connection = connection;
this.start();
}

public void run(){
try {
System.out.println("Starting Client Thread...");
BufferedReader input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
DataOutputStream output = new DataOutputStream(connection.getOutputStream());
System.out.println("IO created...");
System.out.println("input = " + input.readLine());

String s = input.readLine();

System.out.println(s);
file = new File(s);

if(file.isFile()){
System.out.println("File " + s + " exists");

FileInputStream fileOutput = new FileInputStream(file);
byte[] buffer = new byte[1024*1024];
int length = 0;
while((length = fileOutput.read(buffer)) != -1){
output.write(buffer, 0, buffer.length);
}
System.out.println("File " + s + " sent.");
fileOutput.close();
output.close();
input.close();
} else {
System.out.println(s + " is not a file");
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

finally{
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}

}
}


public static void main(String[] args) throws IOException {

int serverPort = 9000;

ServerSocket server = new ServerSocket(serverPort);

Socket connectionSocket = server.accept();

ClientServer con = new ClientServer(connectionSocket);
}
}

客户端

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub

String ip = "localhost";
int port = 9000;
Socket clientSocket = new Socket(ip, port);


BufferedWriter out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));

DataInputStream in = new DataInputStream(clientSocket.getInputStream());

BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter valid file name and press 'ENTER': ");

String message = userInput.readLine();
System.out.println("Sending message...");
out.write(message);
System.out.println("File name sent: " + message);

int length = in.readInt();
byte[] buffer = new byte[length];
for(int i=0; i< length; i++){

buffer[i] = in.readByte();
System.out.println("Reading byte... " + buffer[i]);
}
File file = new File("C:/Users/Dominik/Desktop/lol/new.php");
FileOutputStream fos = new FileOutputStream(file);
fos.write(buffer);
fos.close();

clientSocket.close();
System.out.println("Closing");
}
}

编辑

这是我的输出,因为没有一个答案可以解决我的问题(可能是我做错了什么):

客户端

输入有效的文件名并按“ENTER”:
C:\\Users\\Dominik\\Desktop\\Login.php//我的输入
正在发送消息...
发送的文件名:C:\\Users\\Dominik\\Desktop\\Login.php

服务器

正在启动客户端线程...
IO 创建...
//服务器应该sysout发送的消息,但它没有这样做,它只是卡在这里

最佳答案

您尝试在客户端中readInt,但在服务器端您仅发送文件内容。我看起来您在发送文件之前忘记发送文件大小。

关于java - 套接字客户端-服务器在发送到服务器端后卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34431366/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com