When I run the client, it returns(an error): Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException at java.lang.System.arraycopy(Native Method) at java.io.BufferedOutputStream.write(Unknown Source) at Sockets.FileSocketClient.main(FileSocketClient.java:14)
I understand where it is occurring[bos.write(mybytearray, 0, bytesRead);], I just don't understand WHY
Server
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
}
}
}
Client
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();
}
}