gpt4 book ai didi

java - 如何修复线程 "main"java.io.EOFException 中的异常? (java套接字)

转载 作者:行者123 更新时间:2023-12-02 03:18:56 26 4
gpt4 key购买 nike

我想使用java套接字创建一个简单的多文件传输程序。

服务器输出:

I am server
The server is listening...
Client are connected
Files Selected : 2
README.txt
vcruntime140.dll
Server Closed!

客户端输出:

i am client
Server are connected
filecount : 2
filenames : README.txt

Exception in thread "main" java.io.EOFException at java.io.DataInputStream.readFully(Unknown Source) at java.io.DataInputStream.readLong(Unknown Source) at socket.client.main(client.java:32)

这是我的服务器代码!服务器是发送者。

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

System.out.println("i am server");
System.out.println("");
server = new ServerSocket(12345);
System.out.println("Server is listening...");
client = server.accept();
System.out.println("Client are connected");
dos = new DataOutputStream(client.getOutputStream());


dir = "C:\\Users\\Nitesh Rathi\\Downloads\\vcruntime140";
files = new File(dir).listFiles();
System.out.println("Files Selected : " + files.length);

dos.writeInt(files.length);

byte[] b = new byte[4096];

for (File file : files)
{
long length = file.length();
dos.writeLong(length);

String filename = file.getName();
dos.writeUTF(filename);

System.out.println(file.getName());
fis = new FileInputStream(file);

while (fis.read(b) != -1)
{
fis.read(b, 0, b.length);
dos.write(b, 0, b.length);
}
}

System.out.println("");
fis.close();
client.close();
server.close();
System.out.println("Server Closed!");
}

这是我的客户端代码!客户端是接收者。

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

System.out.println("i am client");
System.out.println("");
soc = new Socket("localhost", 12345);
System.out.println("Server are connected");
dis = new DataInputStream(soc.getInputStream());

int filecount = dis.readInt();
File[] files = new File[filecount];

System.out.println("filecount : " + filecount);
byte[] b = new byte[1024];

for (int i=0;i<filecount;i++)
{
long filelength = dis.readLong();
String filename = dis.readUTF();
System.out.println("filenames : "+filename);

files[i] = new File(dirPath + "/" + filename);
fos = new FileOutputStream(files[i]);

for(int j = 0; j < filelength; j++)
{
fos.write(b);
dis.read(b);
}

}

System.out.println("data received!");

fos.flush();
fos.close();
soc.close();

System.out.println("client closed!");
}

我期望客户端的输出:文件数量:2文件名:README.txt vcruntime140.dll

最佳答案

在服务器端,您在从输入文件流读取时,每次循环迭代都会调用 fis.read() 两次。每次迭代只需调用它一次,并且需要将 fis.read() 的返回值传递给 dos.write() 以便它知道正确的数字要写入的字节数。

在客户端,读取文件流时,您在调用 dis.read() 填充 之前调用 fos.write() b 与数据。并且您循环了太多次,因为您没有使用每次迭代实际读取的字节数来更新您的 j 循环计数器。

尝试更多类似这样的事情:

服务器:

public static void main(String[] args) throws Exception
{
System.out.println("i am server");
System.out.println("");

ServerSocket server = new ServerSocket(12345);
System.out.println("Server is listening...");

Socket client = server.accept();
System.out.println("Client is connected");

DataOutputStream dos = new DataOutputStream(client.getOutputStream());

String dir = "C:\\Users\\Nitesh Rathi\\Downloads\\vcruntime140";
Files[] files = new File(dir).listFiles();

System.out.println("Files Selected : " + files.length);
dos.writeInt(files.length);

byte[] b = new byte[4096];

for (File file : files)
{
long filelength = file.length();
dos.writeLong(filelength);

String filename = file.getName();
dos.writeUTF(filename);
System.out.println(filename);

FileInputStream fis = new FileInputStream(file);
DataInputStream dis = DataInputStream(fis);

int loops = (int) (filelength / (long) b.length);
int remainder = (int) (filelength % (long) b.length);

for (int j = 0; j < loops; j++)
{
dis.readFully(b);
dos.write(b);
}

if (remainder > 0)
{
dis.readFully(b, 0, remainder);
dos.write(b, 0, remainder);
}
}

System.out.println("");

dos.close();
server.close();

System.out.println("Server Closed!");
}

客户:

public static void main(String[] args) throws Exception
{
System.out.println("i am client");
System.out.println("");

Socket soc = new Socket("localhost", 12345);
System.out.println("Server is connected");

DataInputStream dis = new DataInputStream(soc.getInputStream());

int filecount = dis.readInt();
File[] files = new File[filecount];
System.out.println("filecount : " + filecount);

byte[] b = new byte[1024];

for (int i = 0; i < filecount; i++)
{
long filelength = dis.readLong();
String filename = dis.readUTF();
System.out.println("filename : " + filename);

files[i] = new File(dirPath + "/" + filename);

FileOutputStream fos = new FileOutputStream(files[i]);

int loops = (int) (filelength / (long) b.length);
int remainder = (int) (filelength % (long) b.length);

for (int j = 0; j < loops; j++)
{
dis.readFully(b);
fos.write(b);
}

if (remainder > 0)
{
dis.readFully(b, 0, remainder);
fos.write(b, 0, remainder);
}
}

System.out.println("data received!");

dis.close();

System.out.println("client closed!");
}

关于java - 如何修复线程 "main"java.io.EOFException 中的异常? (java套接字),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56941050/

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