1 Replies - 708 Views - Last Post: 09 May 2015 - 01:58 AM Rate Topic: -----

#1 MUProgrammer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-May 15

Second packet read from input stream has incomplete data,

Posted 08 May 2015 - 07:46 PM

I a simple server that receives bytes using TCP and then saves them to a file stream. Through many tests I have seen that the first packet received is always just the filename with no other data. The second packet received only has one byte and it is the first letter of the input text file. After this all packets are sent correctly, but I can't seem to figure out what is messing up the second packet. It also appears that the last packet is written twice. Can anyone see what I am doing wrong? Here is an example Input/Output: https://www.diffchecker.com/srclclrx

InputStream in = clntSock.getInputStream(); //server's input stream - gets data from the client
OutputStream out = clntSock.getOutputStream(); //server's output stream - server sends data to the client

byte[] byteBuffer = new byte[BUFSIZE];
int count = in.read(byteBuffer, 0, BUFSIZE);
String firstRead = new String(byteBuffer, 0, count);
int fileNameEnd = firstRead.indexOf("\r\n");
String fileName = firstRead.substring(0, fileNameEnd);

FileOutputStream fout = new FileOutputStream(fileName); //unzipped file output stream

int contentBegin = fileNameEnd+2;
byte[] oldBuffer = Arrays.copyOfRange(byteBuffer, contentBegin, count);
int oldCount = count-contentBegin;
String oldString = new String(byteBuffer, contentBegin, count-contentBegin, "US-ASCII");

while((count = in.read(byteBuffer, 0, BUFSIZE)) != -1) { // read from origin's buffer into byteBuffer until origin is out of data
String newString = new String(byteBuffer, 0, count, "US-ASCII");
String combinedString = oldString + newString;
int index = combinedString.indexOf("--------MagicStringCSE283Miami");
if(index != -1){
System.out.println("Final Print");

byte[] combinedBuffer = concat(oldBuffer, byteBuffer);

for(int i=0; i<index; i++){
System.out.print((char)combinedBuffer[i]);
}
System.out.println("");

fout.write(combinedBuffer, 0, index);
fout.flush();
fout.close();
break;
}
System.out.println(+ oldCount);
fout.write(oldBuffer, 0, oldCount); //write the byteBuffer's data to the client via the zip output stream
fout.flush(); //push all data out of the zipOutputStream before continuing
if(count == 1){
for(int i=0; i<count; i++){
System.out.println((char)byteBuffer[i]);
}
}

oldBuffer = byteBuffer;
oldCount = count;
oldString = newString;
}

Edit: Another peculiarity to me is that the second to last packet is always just "-" and then the last packet has the remainder of the magic string which terminates the file output stream.

Edit2: I am aware that it is not safe to make the assumption that the file name will be sent in one go before the loop, but I wil fix that later. I am not concerned about it at all now. THe problem si that the loop should account for any amount read in, but for some reason the first packet always contains one letter, and the second packet picks up around 16000 bytes later for some reason. Why would this be?

I can't edit my above post so here is the code with code tags.

		        InputStream in = clntSock.getInputStream(); //server's input stream - gets data from the client
			OutputStream out = clntSock.getOutputStream(); //server's output stream - server sends data to the client
			
			byte[] byteBuffer = new byte[BUFSIZE];
			int count = in.read(byteBuffer, 0, BUFSIZE);
			String firstRead = new String(byteBuffer, 0, count);
			int fileNameEnd = firstRead.indexOf("\r\n");
			String fileName = firstRead.substring(0, fileNameEnd);
			
			FileOutputStream fout = new FileOutputStream(fileName); //unzipped file output stream
			
			int contentBegin = fileNameEnd+2;
			byte[] oldBuffer = Arrays.copyOfRange(byteBuffer, contentBegin, count);
			int oldCount = count-contentBegin;
			String oldString = new String(byteBuffer, contentBegin, count-contentBegin, "US-ASCII");

			while((count = in.read(byteBuffer, 0, BUFSIZE)) != -1) { // read from origin's buffer into byteBuffer until origin is out of data
				String newString = new String(byteBuffer, 0, count, "US-ASCII");
				String combinedString = oldString + newString;
				int index = combinedString.indexOf("--------MagicStringCSE283Miami");
				if(index != -1){
					System.out.println("Final Print");
					
					byte[] combinedBuffer = concat(oldBuffer, byteBuffer);
					
					for(int i=0; i<index; i++){
						System.out.print((char)combinedBuffer[i]);
					}
					System.out.println("");
					
					fout.write(combinedBuffer, 0, index);
					fout.flush();
					fout.close();
					break;
				}
				System.out.println(+ oldCount);
				fout.write(oldBuffer, 0, oldCount); //write the byteBuffer's data to the client via the zip output stream
				fout.flush(); //push all data out of the zipOutputStream before continuing
				if(count == 1){
					for(int i=0; i<count; i++){
						System.out.println((char)byteBuffer[i]);
					}
				}
				
				oldBuffer = byteBuffer;
				oldCount = count;
				oldString = newString;
			}




Is This A Good Question/Topic? 0
  • +

Replies To: Second packet read from input stream has incomplete data,

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Second packet read from input stream has incomplete data,

Posted 09 May 2015 - 01:58 AM

Please Posted Image as you were asked. andrewsw's screencast will show you how to use code-tags (if it's still there). It isn't difficult.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1