Can Send ASCII Files Correctly But NOT Binary FIles (TCP)
In this code right here we are sending in a file and outputting it to the same computer -- we are just trying to test to see if it works. We are breaking the file up into 128 bit packets into byte arrays which we convert to strings to append ACKs, etc.
When we send and ASCII file it is no problem, and it come out perfectly correct.
When we send a BINARY file and try view it in wordpad, everything looks the same except sometimes a block (original file) will be a ? (copied file). Yes, it will literally show up as a question mark.
Our code is below -- any help would be AMAZING.
// *********************************************************
// PROGRAMMING ASSIGNMENT #1 (TCPCLIENT.JAVA)
//
// Names: Brendan G. Lim, Andrew Tilt, Naoki Kyobashi
// Class: COMP 4320 - Introduction to Computer Networks
//**********************************************************
import java.io.*;
import java.net.*;
public class TCPClientString {
public static int fileSize = 0;
public static int arrayLength = 0;
public static byte[] leftOver;
public static int lengthLeft = 0;
public static void main(String[] args) throws Exception {
String fileName = "test.jpg";
String fileOut = "copy.jpg";
byte[][] splitArray;
byte[] sendPacket;
int ackNum = 1;
int fileSizeOut = 0;
int windowStart = 0;
int windowEnd = 0;
int totalAcks = 0;
//String serverAddress = args[0];
//final int BUFFER_SIZE = 128;
//byte[] buffer = new byte[BUFFER_SIZE];
//Socket socket = new Socket (serverAddress, 6190);
BufferedInputStream in = new BufferedInputStream(new FileInputStream(fileName));
// //BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(fileOut));
int len = 0;
splitArray = createFileArray(fileName);
boolean[] acksRecieved = new boolean[arrayLength];
for(int i=0; i < arrayLength; i++)
{
acksRecieved = false;
}
if (arrayLength >= 5)
windowEnd = 4;
else
windowEnd = arrayLength - 1;
System.out.print(arrayLength);
if (arrayLength != 0)
{
while(totalAcks != arrayLength)
{
for(int l=windowStart; l <= windowEnd && l < arrayLength; l++)
{
if (acksRecieved[l] != true)
{
sendPacket = getPacket(splitArray, l, l, 128);
// System.out.println(windowStart);
// System.out.println(windowEnd);
out.write(sendPacket);
fileSizeOut += 128;
acksRecieved[l] = true;
totalAcks++;
}
}
while (acksRecieved[windowStart] == true && windowStart < arrayLength -1)
{
windowStart++;
if (windowEnd < arrayLength-1)
windowEnd++;
}
}
}
if (lengthLeft != 0)
{
// System.out.println("did this");
sendPacket = getPacket(leftOver, ackNum, lengthLeft);
out.write(sendPacket);
fileSizeOut += lengthLeft;
}
// System.out.println("\n\nSending File...");
// while((len = in.read(buffer)) > 0 )
// {
// out.write(buffer, 0, len);
//
// }
in.close();
out.flush();
out.close();
//socket.close();
System.out.println("\n \n" +fileSize + " " +fileSizeOut);
System.out.println("\n****File Transfered****\n\n");
}
public static byte[][] createFileArray(String fileName)throws Exception
{
int arraySize = 128;
FileInputStream fr = new FileInputStream(fileName);
fileSize = fr.available();
System.out.println(fileSize);
arrayLength = fileSize/128;
int currentPos = 0;
int bytesRead = 0;
byte[] msg = new byte[fileSize];
int counter = 0;
fr.read(msg);
byte[][] brokenUp = new byte[arrayLength][arraySize];
int msgPos = 0;
if(fileSize >= 128)
{
for (int j = 0; j < arrayLength; j++)
{
for (int i = 0; i < 128 && msgPos < fileSize; i++)
{
brokenUp[j] = msg[msgPos];
msgPos++;
}
}
}
if (fileSize%128 != 0)
{
lengthLeft = fileSize%128;
leftOver = new byte [lengthLeft];
for (int j = 0; j < lengthLeft; j++)
{
for (int i = 0; i < 128 && msgPos < fileSize; i++)
{
leftOver = msg[msgPos];
msgPos++;
}
}
}
return brokenUp;
}
public static String getPacket(byte[][] brokenMatrix, int position, int ack, int length)
{
byte[] packet = new byte[128];
String pack;
String finalPack;
for (int i = 0; i<128; i++)
{
packet = brokenMatrix[position];
}
pack = new String(packet, 0, 128);
// finalPack = ack+"**"+"Address1"+"**"+"Address2"+"**";//+pack;
// System.out.print(pack);
//packet = pack.getBytes();
return pack;
}
public static String getPacket(byte[] brokenMatrix, int ack, int length)
{
byte[] packet = new byte[128];
String pack;
String finalPack;
for (int i = 0; i<length; i++)
{
packet = brokenMatrix;
}
pack = new String(packet, 0, length);
// //finalPack = ack+"**"+"Address1"+"**"+"Address2"+"**"+pack;
//
//
//packet = pack.getBytes();
return packet;
}
//
}>

