gpt4 book ai didi

Java套接字文件传输线程失败,但在调试时可以工作

转载 作者:行者123 更新时间:2023-12-02 07:18:50 25 4
gpt4 key购买 nike

我不知道为什么我的程序在调试时可以工作,但在正常执行时却失败。

我已经设置了一个简单的服务器来通过套接字提供文件。当客户端连接时,启动一个线程来执行此任务,但它失败了。我收到“套接字已关闭”错误。

这是作业:在我弄清楚为什么会发生这种情况之前,我无法前进,并且只是想朝正确的方向插入。

为了让它在调试中工作,我在服务器类中设置了断点,它接受客户端连接。

提前致谢。

public class File_Server {

private static ServerSocket servsock;

public static void main(String[] args) throws IOException
{
// create socket
try {
servsock = new ServerSocket(4444);
} catch (Exception e) {
System.out.println("Port already in use.");
System.exit(1);
}

while (true) {
System.out.println("Waiting...");
try (Socket sock = servsock.accept()) {
System.out.println("Accepted connection : " + sock);

Thread t = new Thread(new CLIENTConnection(sock));

t.start();

} catch (Exception e) {
System.out.println("Cannot accept connection.");
}
}
}
}



public class CLIENTConnection implements Runnable {

private Socket client;

CLIENTConnection(Socket client) {
this.client = client;
}

@Override
public void run()
{
try {
FileInputStream fis = null;
// sendfile
File myFile = new File("studentData.txt");
byte[] mybytearray = new byte[(int) myFile.length()];
fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray, 0, mybytearray.length);
OutputStream os = client.getOutputStream();
System.out.println("Sending...");
os.write(mybytearray, 0, mybytearray.length);
os.flush();
fis.close();
} catch (IOException ex) {
Logger.getLogger(CLIENTConnection.class.getName()).log(Level.SEVERE, null, ex);
}
}
}



public class File_Client {

private static long start = System.currentTimeMillis();
private static int bytesRead;
private static int current = 0;

public static void main(String[] args) throws IOException
{
int filesize = 60223861; // filesize temporary hardcoded

try (Socket sock = new Socket("127.0.0.1", 4444)) {
System.out.println("Connecting...");

// receive file
byte[] mybytearray = new byte[filesize];
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream("studentData-received.txt");
try (BufferedOutputStream bos = new BufferedOutputStream(fos)) {
bytesRead = is.read();//.read(mybytearray, 0, mybytearray.length);
current = bytesRead;

do {
bytesRead =
is.read(mybytearray, current, (mybytearray.length - current));
if (bytesRead >= 0) {
current += bytesRead;
}
} while (bytesRead > -1);

bos.write(mybytearray, 0, current);
bos.flush();
long end = System.currentTimeMillis();
System.out.println("Time taken " + (end - start) + " milliseconds");
}
} catch (Exception e) {
System.out.println("Cannot connect to server.");
}
}
}

最佳答案

您犯了忽略 read() 结果的常见错误。它返回实际读取的字节数,或者在 EOS 处为 -1。 Java中复制流的方法如下:

while ((count = in.read(buffer)) >= 0)
{
out.write(buffer, 0, count);
}

其中“count”是一个整数。

关于Java套接字文件传输线程失败,但在调试时可以工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14532952/

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