当我运行客户端时,它返回(一个错误): Sockets 的 java.io.BufferedOutputStream.write(Unknown Source) 的 java.lang.System.arraycopy(Native Method) 的线程“main”java.lang.ArrayIndexOutOfBoundsException 中的异常.FileSocketClient.main(FileSocketClient.java:14)
我知道它发生在哪里[bos.write(mybytearray, 0, bytesRead);],我只是不明白为什么
服务器
import java.io.*;
import java.net.*;
public class FileSocketServer {
public static void main(String args[]) throws IOException {
ServerSocket serverSocket = new ServerSocket(1235);
File myFile = new File("test.txt");
while(true) {
Socket socket = serverSocket.accept(); //Understand
byte[] mybytearray = new byte[(int)myFile.length()]; //Don't understand
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile)); //Don't understand
bis.read(mybytearray, 0, mybytearray.length); //Don't understand
OutputStream os = socket.getOutputStream(); //Don't understand
os.write(mybytearray, 0, mybytearray.length); //Don't understand
os.flush(); //Don't understand
socket.close(); //Don't understand
}
}
}
客户
package Sockets;
import java.io.*;
import java.net.*;
public class FileSocketClient {
public static void main(String args[]) throws IOException{
Socket socket = new Socket("GANNON-PC", 1235); //Understand
byte[] mybytearray = new byte[1024]; //Don't understand
InputStream is = socket.getInputStream(); //Don't understand
FileOutputStream fos = new FileOutputStream("mods//test.txt"); //Don't understand
BufferedOutputStream bos = new BufferedOutputStream(fos); //Don't understand
int bytesRead = is.read(mybytearray, 0, mybytearray.length); //Don't understand
bos.write(mybytearray, 0, bytesRead); //Don't understand
bos.close(); //
socket.close();
}
}