I am trying to get a program working where a client sends the same file to the server 100 times and then checks to make sure all 100 files are the same as the original. Right now I cant get it to work because the server reads some arbitrary amount of bytes from the client and it causes some files to have too many bytes in them and I'm not sure how to proceed. Right now it will send all 100 files properly but some files just have too much stuff in them.
Client code
String sentence;
InetAddress host = InetAddress.getLocalHost();
Socket clientSocket = new Socket(host, 6789);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
long startTime, endTime, tGap, totalTime;
byte[] sendData;
int filesSent = 0;
File fs = new File("Test1.txt");
int fileLength = (int) fs.length();
totalTime = 0;
while (filesSent < 100) {
FileInputStream fin = new FileInputStream(fs);
int numBytes = 0;
System.out.println("Sending file to server...");
sendData = new byte[1024];
if (fileLength < 1024) {
numBytes = fin.read(sendData, 0, fileLength);
} else {
numBytes = fin.read(sendData, 0, 1024);
}
startTime = System.currentTimeMillis();
int count = 0;
while (numBytes != -1) {
count += numBytes;
System.out.println(numBytes);
sentence = new String(sendData);
outToServer.writeBytes(sentence);
numBytes = fin.read(sendData, 0, 1024);
}
System.out.println(count);
endTime = System.currentTimeMillis();
tGap = endTime - startTime;
totalTime = totalTime + tGap;
System.out.println("Finished run " + filesSent + " with a time of " + tGap);
filesSent++;
fin.close();
outToServer.flush();
}
clientSocket.close();
System.out.println("I am done to send " + filesSent + " times file, and the average time is: " + (double) totalTime / filesSent);
}
Server Code
String clientSentence;
int filesRecieved = 0;
ServerSocket welcomeSocket = new ServerSocket(6789);
int numError = 0;
int numBytes;
char check = 'a';
System.out.println("I am starting now....");
long startTime, endTime, tGap, totalTime;
totalTime = 0;
Socket connectionSocket;
connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
while(filesRecieved < 100) {
FileOutputStream outPut = new FileOutputStream("receivedFile" + filesRecieved + ".txt");
char[] receiveData = new char[1024];
numBytes = inFromClient.read(receiveData, 0, 1024);
while(numBytes != 1024){
numBytes += inFromClient.read(receiveData,0,1024-numBytes);
}
System.out.println("OG Bytes:" + numBytes);
startTime = System.currentTimeMillis();
int count = 0;
while (numBytes != -1 && count < 105942) {
try {
count += numBytes;
clientSentence = new String(receiveData, 0, numBytes);
outPut.write(clientSentence.getBytes(), 0, numBytes);
System.out.println(numBytes);
numBytes = inFromClient.read(receiveData, 0, 1024);
System.out.println(count);
}catch(Exception e){
break;
}
}
outPut.close();
endTime = System.currentTimeMillis();
tGap = endTime - startTime;
totalTime = totalTime + tGap;
System.out.println("I have received" + "receivedFile" + filesRecieved + ".txt using time = " + tGap);
filesRecieved++;
}
connectionSocket.close();
System.out.println("I am done now..." + "and the average time used to receive each copy is: " + totalTime / filesRecieved);
welcomeSocket.close();