gpt4 book ai didi

java - 服务器/客户端 java.net.SocketException

转载 作者:行者123 更新时间:2023-12-01 15:01:08 25 4
gpt4 key购买 nike

我正在尝试构建一个发送文件的客户端/服务器应用程序,但我遇到了较大文件的问题。我使用 BufferedInputStream 从文件中读取信息,并使用 OutputStream 写入套接字。我有一个循环从文件中读取 1 KB,然后发送它,该循环在前 25 个循环中运行良好,然后因套接字写入错误而崩溃。有任何想法吗?这是代码。

客户端

import java.io.*;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TCPClient
{
public static void main(String[] args)
{
/*Variables*/
int serverPort = 8899;
String ip = "localhost";
File myFile = new File("GeneratedFile.txt"); //fileToBeSent.txt

System.out.println(myFile.length());

try
{
/*Connect to Server*/
Socket sock = new Socket(ip, serverPort);
System.out.println("Connection Made");

/*Create Streams*/
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
OutputStream clientOutput = sock.getOutputStream();

/*This is the old code for transfer*/
/*Create Byte Array
byte[] myByteArray = new byte[(int) myFile.length()]; //was 1024

/*Send File
bis.read(myByteArray, 0, 1024);
clientOutput.write(myByteArray, 0, 1024);
clientOutput.flush();
*/

for(long i = 0; i <= myFile.length(); i += 1024)
{
byte[] myByteArray = new byte[1024];
bis.read(myByteArray, 0, 1024);
clientOutput.write(myByteArray, 0, 1024);
clientOutput.flush();
System.out.println("i is: " + i);
}
System.out.println("File Written");
sock.close();
} catch (IOException ex)
{
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("You can't do that!");
}
System.out.println("Finished");
}
}

服务器

import java.io.*;
import java.net.*;

public class RequestHandler
{
public void handleRequest()
{
try
{
ServerSocket welcomeSocket = new ServerSocket(8899);

while(true)
{
Socket socket = welcomeSocket.accept();
System.out.println("Socket Open");

/* Create byte array */
byte[] mybytearray = new byte[1024 * 512];

/* Create streams */
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("newFile.txt",true);
BufferedOutputStream bos = new BufferedOutputStream(fos);

/*Write to file*/
int bytesRead = is.read(mybytearray, 0, mybytearray.length);
bos.write(mybytearray, 0, bytesRead);

/*Close Stream and Socket*/
bos.close();
socket.close();
}
} catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String args[])
{
RequestHandler rq = new RequestHandler();
rq.handleRequest();
System.out.println("Here");
}
}

最佳答案

您的复制技术不正确。这是在 Java 中复制流的方法:

byte[] buffer = new byte[8192]; // or whatever you like, but declare it outside the loop
int count;
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
out.flush();
// then in a finally block ...
out.close();
in.close();

两端都需要这个。您不能假设任何给定的读取都会填充缓冲区,因此您必须循环直到 EOS。请注意,您不会在循环内进行刷新。

关于java - 服务器/客户端 java.net.SocketException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13633175/

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