Reputation: 191
I'm trying to build a client/server application that sends files but I'm having an issue with larger files. I'm using a BufferedInputStream to read information from a file and OutputStream to write to the socket. I have a loop that reads 1 KB from the file and then sends it, which works fine for the first 25 loops then crashes with a socket write error. Any ideas? Here's the code.
Client
import java.io.*;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
public class TCPClient
{
public static void main(String[] args)
{
/*Variables*/
int serverPort = 8899;
String ip = "localhost";
File myFile = new File("GeneratedFile.txt"); //fileToBeSent.txt
System.out.println(myFile.length());
try
{
/*Connect to Server*/
Socket sock = new Socket(ip, serverPort);
System.out.println("Connection Made");
/*Create Streams*/
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
OutputStream clientOutput = sock.getOutputStream();
/*This is the old code for transfer*/
/*Create Byte Array
byte[] myByteArray = new byte[(int) myFile.length()]; //was 1024
/*Send File
bis.read(myByteArray, 0, 1024);
clientOutput.write(myByteArray, 0, 1024);
clientOutput.flush();
*/
for(long i = 0; i <= myFile.length(); i += 1024)
{
byte[] myByteArray = new byte[1024];
bis.read(myByteArray, 0, 1024);
clientOutput.write(myByteArray, 0, 1024);
clientOutput.flush();
System.out.println("i is: " + i);
}
System.out.println("File Written");
sock.close();
} catch (IOException ex)
{
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("You can't do that!");
}
System.out.println("Finished");
}
}
Server
import java.io.*;
import java.net.*;
public class RequestHandler
{
public void handleRequest()
{
try
{
ServerSocket welcomeSocket = new ServerSocket(8899);
while(true)
{
Socket socket = welcomeSocket.accept();
System.out.println("Socket Open");
/* Create byte array */
byte[] mybytearray = new byte[1024 * 512];
/* Create streams */
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("newFile.txt",true);
BufferedOutputStream bos = new BufferedOutputStream(fos);
/*Write to file*/
int bytesRead = is.read(mybytearray, 0, mybytearray.length);
bos.write(mybytearray, 0, bytesRead);
/*Close Stream and Socket*/
bos.close();
socket.close();
}
} catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String args[])
{
RequestHandler rq = new RequestHandler();
rq.handleRequest();
System.out.println("Here");
}
}
Upvotes: 0
Views: 180
Reputation: 311054
Your copying technique is incorrect. This is how to copy streams in Java:
byte[] buffer = new byte[8192]; // or whatever you like, but declare it outside the loop
int count;
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
out.flush();
// then in a finally block ...
out.close();
in.close();
You need this at both ends. You cannot assume that any given read will fill the buffer, so you must loop until EOS. Note that you don't flush inside the loop.
Upvotes: 1