I'm having trouble running these two threads in Java. I have two methods in the client class and in each method they both have a socket of different ports but when I run the client, i see the error for a split second of one of the threads but the other one that sends the file over works.
Any help?
ClientApp.java
public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException {
Thread getFileThread = new Thread() {
public void run() {
Client client = new Client();
try {
client.getTheFile("girlwithmask.jpg");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
Thread getListOfFilesThread = new Thread() {
public void run() {
Client client = new Client();
ArrayList<String> listOfFiles = null;
try {
listOfFiles = client.getFileList();
System.out.println(listOfFiles.get(1));
notify();
} catch (ClassNotFoundException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
getListOfFilesThread.start();
getFileThread.start();
}
Client.java
public class Client {
private static final int PORT = 2665;
private static String HOST = "localhost";
Client() {
}
public void getTheFile(String filename) throws UnknownHostException, IOException {
filename = "girlwithmask.jpg"; ///this is temporary
int filesize = 5000000; //buffer size 5mb
int bytesRead;
int currentTotalNumberOfBytes = 0;
//connect to port on server - server waits for this after running socket.accept() in the Server class
Socket socket = new Socket(HOST, PORT);
byte[] byteArray = new byte[filesize]; //create a byte array of 5mb
InputStream inputStream = socket.getInputStream(); //channel to to server
FileOutputStream fileOutStream = new FileOutputStream("myClientFiles/" + filename);
BufferedOutputStream bufferOutStream = new BufferedOutputStream(fileOutStream);
bytesRead = inputStream.read(byteArray, 0, byteArray.length);
currentTotalNumberOfBytes = bytesRead;
do { //read till the end and store total in bytesRead and add it to currentTotalNumberOfBytes
bytesRead = inputStream.read(byteArray, currentTotalNumberOfBytes, (byteArray.length - currentTotalNumberOfBytes));
if (bytesRead >= 0) {
currentTotalNumberOfBytes += bytesRead;
}
} while (bytesRead > -1); // when bytesRead == -1, there's no more data left and we exit the loop
bufferOutStream.write(byteArray, 0, currentTotalNumberOfBytes); //write the bytes to the file
bufferOutStream.flush();
bufferOutStream.close();
socket.close();
}
public ArrayList<String> getFileList() throws UnknownHostException, IOException, ClassNotFoundException {
Socket socket = new Socket("localhost", 9999);
ArrayList<String> titleList = new ArrayList<String>();
ObjectInputStream objectInput = new ObjectInputStream(socket.getInputStream());
Object object = objectInput.readObject();
titleList = (ArrayList<String>) object;
// System.out.println(titleList.get(2));
return titleList;
}
}
I'm not sure what is going on here. Been working with this for a couple of hours.
In the absence of an actual error, or indeed question, all we can do is critique your code:
byte[] byteArray = new byte[filesize]; //create a byte array of 5mb
You don't know what filesize is. You've hardcoded a guess of 5000000. This will not work for any case where the filesize is bigger than 5000000 which might be very often. There is an approach where you don't need to know the filesize: nor do you need a buffer the size of the whole file in the first place. You're assuming the file fits into memory and that the file length fits into an int. Both assumptions may be wrong. Use a smaller buffer size of 8192 or some such reasonable number which is usually a multiple of 1024 to get a good memory alignment. Hard-coding a big size of 5000000 has the drawbacks mentioned.
InputStream inputStream = socket.getInputStream(); //channel to to server
FileOutputStream fileOutStream = new FileOutputStream("myClientFiles/" + filename);
BufferedOutputStream bufferOutStream = new BufferedOutputStream(fileOutStream);
You don't really need the BufferedOutputStream with this code, or at least with this code as it's going to be, but let it pass for now.
bytesRead = inputStream.read(byteArray, 0, byteArray.length);
currentTotalNumberOfBytes = bytesRead;
do { //read till the end and store total in bytesRead and add it to currentTotalNumberOfBytes
bytesRead = inputStream.read(byteArray, currentTotalNumberOfBytes, (byteArray.length - currentTotalNumberOfBytes));
if (bytesRead >= 0) {
currentTotalNumberOfBytes += bytesRead;
}
} while (bytesRead > -1); // when bytesRead == -1, there's no more data left and we exit the loop
bufferOutStream.write(byteArray, 0, currentTotalNumberOfBytes); //write the bytes to the file
To make this code shorter you might want to change it to the canonical form:
int count;
byte[] buffer = new byte[8192];
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
substituting variable names as appropriate. You will:
Save memory
Reduce latency
Have clear well-tested code that's been working for 18 years.
bufferOutStream.flush();
flush() before close() is redundant.
bufferOutStream.close();
socket.close();
Closing the socket after closing its output stream (or input stream) is redundant. Just close the output stream. In a finally block.
Related
I'm trying to build a file server and file client program in Java using sockets, and I've been running into some issues when trying to send files from the server to the client. Below is the code that I use to send and receive files respectively:
private void sendFile(String filePath) {
try (BufferedInputStream fileInputStream = new BufferedInputStream(new FileInputStream(filePath))) {
BufferedOutputStream outputStream = new BufferedOutputStream(socket.getOutputStream());
byte[] buffer = new byte[4096];
while (fileInputStream.read(buffer) != -1) {
outputStream.write(buffer);
outputStream.flush();
}
}
catch (IOException e) {e.printStackTrace();}
}
private void downloadFile(String fileName, long fileSize) {
try (BufferedOutputStream fileOutputStream = new BufferedOutputStream(new FileOutputStream(downloadDir + "/" + fileName));
BufferedInputStream inputStream = new BufferedInputStream(socket.getInputStream());
OutputStreamWriter writer = new OutputStreamWriter(new BufferedOutputStream(socket.getOutputStream()), "UTF-8");) {
writer.write("GET" + fileName + System.lineSeparator());
writer.flush();
long totalReceived = 0;
int bufferSize = 4096;
byte[] buffer = new byte[bufferSize];
while (totalReceived < fileSize) {
inputStream.read(buffer);
int numOfBytesToWrite = fileSize - totalReceived > bufferSize ? buffer.length : (int)(fileSize % bufferSize);
fileOutputStream.write(buffer, 0, numOfBytesToWrite);
fileOutputStream.flush();
totalReceived += numOfBytesToWrite;
}
}
catch (IOException e) {}
}
The downloaded file does get created and seems to be of the right size, but always gets corrupted and cannot be opened by any program. However, this issue does not show itself when I run the client on the same machine and connect it to "localhost" or "127.0.0.1", then there are no problems and downloaded files are not corrupted. See any issues with my code?
In your sendFile() you need to consider the return value from the read() which may be less than 4096... This value should then be used in the write call to only write out the portion of the array that has been populated...
int bytesRead = 0;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
outputStream.flush();
}
A similar problem occurs in downloadFile(), return from read() is the actual number of bytes read, some value less than or equal to 4096...
long totalReceived = 0;
int bufferSize = 4096;
byte[] buffer = new byte[bufferSize];
while (totalReceived < fileSize) {
int bytesRead = inputStream.read(buffer);
fileOutputStream.write(buffer, 0, bytesRead);
fileOutputStream.flush();
totalReceived += bytesRead;
}
Why does your code work on localhost, but not over a network?
Atypical physical layer of a network is Ethernet, this will have a MTU of 1500 bytes. So you'll probably be seeing successive read() calls only filling 1500, or fewer bytes of your buffer...
However, localhost is optimized in the stack to bypass the physical layer which will not have this limitation. It is likely in this case successive calls will fill the full 4096 buffer, apart from the last, unless your file size is exact multiple of 4096.
I want to receive text files through a socket connection in java, I set up the server end but before I continue with the client I would like to know if the code I made works, except I have no idea how to test this.
Any help would be much appreciated..
EDIT: I know the port is open and listening for requests, what i want is to test what happens if it receives anything, will it create a file from the input and can I test this by simulation(sending a file or bytes i dont know)?
public class Server {
private static int port = 8080;
private static int maxConnections = 100000;
// Listen for incoming connections and handle them
public static void startServer() {
int i = 0;
try {
ServerSocket listener = new ServerSocket(port);
Socket server;
System.out.println("Started server on port:" + port);
while ((i++ < maxConnections) || (maxConnections == 0)) {
RunServer connection;
server = listener.accept();
RunServer conn_c = new RunServer(server);
Thread t = new Thread(conn_c);
t.start();
System.out.println("Created new thread");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class RunServer implements Runnable {
private Socket server;
RunServer(Socket server) {
this.server = server;
}
public void run() {
int bytesRead;
int current = 0;
BufferedOutputStream bufferedOutputStream = null;
FileOutputStream fileOutputStream = null;
DataInputStream clientData = null;
File file = null;
try {
// creating connection.
System.out.println("connected.");
// receive file
byte[] byteArray = new byte[6022386];
System.out.println("Please wait downloading file");
// reading file from socket
InputStream inputStream = server.getInputStream();
file = new File("toread.txt");
clientData = new DataInputStream(inputStream);
fileOutputStream = new FileOutputStream(file);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
bytesRead = inputStream.read(byteArray, 0, byteArray.length);
current = bytesRead;
do {
bytesRead = inputStream.read(byteArray, current, (byteArray.length - current));
if (bytesRead >= 0)
current += bytesRead;
} while (bytesRead > -1);
bufferedOutputStream.write(byteArray, 0, current);
bufferedOutputStream.flush();
ReaderHelper.readTextFile(file);
} catch (IOException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (fileOutputStream != null)
fileOutputStream.close();
if (bufferedOutputStream != null)
bufferedOutputStream.close();
if (clientData != null)
clientData.close();
if (server != null)
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Well, your socket will be up on port 8080 right?
You can open your browser and type: http://localhost:8080. The browser will create a connection and the line
server = listener.accept();
will "unlock". If you just wanna test if the socket is listenning it will do. Reading the stream you will see the first message of the HTTP protocol.
At first you have a possible error in a reading loop
byte[] byteArray = new byte[6022386];
//some code
do {
bytesRead = inputStream.read(byteArray, current, (byteArray.length - current));
if (bytesRead >= 0)
current += bytesRead;
} while (bytesRead > -1);
If file length is more than byteArray.length which is possible, then (byteArray.length - current) would be negative.
I suggest you to use smaller array, e.g. byte[] byteArray = new byte[8192]; and read file like this
while ((bytesRead = in.read(byteArray)) > 0 ) {
current += bytesRead;
bufferedOutputStream.write(byteArray, 0, bytesRead);
}
storing it into disk chunk by chunk. And after exiting while loop current will hold total number of read bytes.
Send file from another thread, which will connect to server
Socket clientSocket = new Socket("localhost", Server.port);
BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
DataOutputStream output = new DataOutputStream(clientSocket.getOutputStream());
FileInputStream fileInput = new FileInputStream("fileName");
byte[] buffer = new byte[8192];
while(fileInput.read(buffer) != -1){
output.write(buffer, 0, length);
}
//close resources
This question already has answers here:
Java multiple file transfer over socket
(3 answers)
Closed 7 years ago.
I'm making a communicator with ability to send files.
So far I managed to make text sending working using additional thread (listener).
I'm trying to make the same thing with files, but I don't know, how can I make a file listener - a thread, that detects incoming file, downloads it and listens for another file. Also, I don't know if I'm making my file sender properly. Could you help?
Current sender code:
try {
InputStream in = new FileInputStream(fileToSend);
OutputStream out = fileConn.getOutputStream();
Controller.copyData(in, out);
out.close();
in.close();
} catch (IOException e) {
System.out.println("Problem!");
}
And receiver code:
while (true)
{
try {
InputStream in = socket.getInputStream();
OutputStream out = new FileOutputStream("hi.txt"); //temporary
Controller.copyData(in, out);
out.close();
in.close();
} catch (IOException e) {
System.out.println("Problem!");
}
}
EDIT: I forgot to add my copyData. There it is:
public static void copyData(InputStream in, OutputStream out) throws IOException{
byte[] buf = new byte[8192];
int len = 0;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
}
You can achive tha by just adding to your listening thread option to wait for diffrent messages/options and react accordingly. For example:
private class WaitingThread extends Thread {
volatile boolean awaitsServer = false;
DataInputStream dataInput = new DataInputStream(inputStream);
public void run() {
while (connected) {
int message = 0;
if (awaitsServer == true) {
if (dIn.available() ==0) {
view.setLog("waiting");
} else {
message = dIn.readInt();
switch (tempMessage) {
// TO DO ALL KIND OF COMMUNICATION
case 1:
int filesize = dataInput.readInt();
int bytesRead;
int currentTot = 0;
byte[] bytearray = new byte[filesize];
int len = dataInput.readInt();
FileOutputStream fos = new FileOutputStream(currentlySelectedFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = dataInput.read(bytearray, 0, bytearray.length);
currentTot = bytesRead;
do {
bytesRead = dataInput.read(bytearray, currentTot,
(len - currentTot));
if (bytesRead >= 0)
currentTot += bytesRead;
} while (currentTot < len);
bos.write(bytearray, 0, currentTot);
bos.close();
}
case 2: //GET TEXT
case 3: //DO SOMETHING ELSE
}}}
Btw you have example how to send files.
Faced with the following problem:
I need to determine bandwidth for a given ip and depending on it my task will be done in different ways.
I've written a simple implementation
Client:
public void send(Socket socket, File file) throws IOException {
FileInputStream inputStream = null;
DataOutputStream outputStream = null;
try {
inputStream = new FileInputStream(file);
int fileSize = (int) file.length();
byte[] buffer = new byte[fileSize];
outputStream = new DataOutputStream(socket.getOutputStream());
outputStream.writeUTF(file.getName());
int recievedBytesCount = -1;
while ((recievedBytesCount = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, recievedBytesCount);
}
} catch (IOException e) {
System.out.println(e);
} finally {
inputStream.close();
outputStream.close();
socket.close();
}
Server:
public void recieve() throws IOException {
ServerSocket server = new ServerSocket (port);
Socket client = server.accept();
DataInputStream dataInputStream = new DataInputStream(client.getInputStream());
DataInputStream inputStream = new DataInputStream(client.getInputStream());
String fileName = dataInputStream.readUTF();
FileOutputStream fout = new FileOutputStream("D:/temp/" + fileName);
byte[] buffer = new byte[65535];
int totalLength = 0;
int currentLength = -1;
while((currentLength = inputStream.read(buffer)) != -1){
totalLength += currentLength;
fout.write(buffer, 0, currentLength);
}
}
Test class:
public static void main(String[] args) {
File file = new File("D:\\temp2\\absf.txt");
Socket socket = null;
try {
socket = new Socket("127.0.0.1", 8080);
} catch (IOException e) {
e.printStackTrace();
}
ClientForTransfer cl = new ClientForTransfer();
long lBegin = 0;
long lEnd = 0;
try {
lBegin = System.nanoTime();
cl.send(socket, file);
lEnd = System.nanoTime();
} catch (IOException e) {
e.printStackTrace();
}
long lDelta = lEnd - lBegin;
Double result = ( file.length() / 1024.0 / 1024.0 * 8.0 / lDelta * 1e-9 ); //Mbit/s
System.out.println(result);
}
The problem is that using different sizes of the input files I get different speeds.
Tell me, please, how to solve this problem.
The problem is the TCP slow-start. http://en.wikipedia.org/wiki/Slow-start
Try to first transfer something like 10KBs, followed by the real measurement transfer. Make sure to use the same connection for both transfers.
This is not an easily "solved" problem. It is completely normal to get different speeds for different size files, as the "bandwidth" depends on many, many factors, including raw connection speed, quality (dropped packets) and latency, and can vary even from moment to moment.
You need to try several different file sizes, starting with small files and moving to larger files until the transfer takes 10-20 seconds in order to judge average bandwidth.
I am trying to send a file from client to server. Below is the code i have tried. But at times, there is a packet loss during the transfer. I am not sure where i am wrong.
SERVER SIDE CODE:
public static void ReadAndWrite(byte[] aByte, Socket clientSocket,
InputStream inputStream, String fileOutput)
throws FileNotFoundException, IOException {
int bytesRead;
FileOutputStream fileOutputStream = null;
BufferedOutputStream bufferedOutputStream = null;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try
{
fileOutputStream = new FileOutputStream( fileOutput );
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
bytesRead = inputStream.read(aByte, 0, aByte.length);
System.out.println("The length is "+bytesRead);
int count = 0;
do {
count++;
byteArrayOutputStream.write(aByte);
bytesRead = inputStream.read(aByte);
} while (bytesRead != -1);
System.out.println("The count is "+count);
System.out.println("The length is "+byteArrayOutputStream.size());
bufferedOutputStream.write(byteArrayOutputStream.toByteArray());
bufferedOutputStream.flush();
bufferedOutputStream.close();
clientSocket.close();
}
catch(Exception ex)
{
Logger.writeLog(ex,Listen.class.getName(), LogType.EXCEPTION);
throw ex;
}
CLIENT SIDE CODE:
public void readByteArrayAndWriteToClientSocket(
Socket connectionSocket, BufferedOutputStream outToClient, String fileToSend ) throws Exception
{
try{
if (outToClient != null)
{
File myFile = new File(fileToSend);
System.out.println(myFile.length());
byte[] byteArray = new byte[(int) myFile.length()];
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(myFile);
} catch (IOException ex) {
Logger.writeLog(ex, FileUtility.class.getName(), LogType.EXCEPTION);
throw ex;
}
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
try {
bufferedInputStream.read(byteArray, 0, byteArray.length);
outToClient.write(byteArray, 0, byteArray.length);
outToClient.flush();
outToClient.close();
connectionSocket.close();
return;
} catch (IOException ex) {
Logger.writeLog(ex, FileUtility.class.getName(), LogType.EXCEPTION);
throw ex;
}
}
}catch (Exception e) {
Logger.writeLog(e, getClass().getName(), LogType.EXCEPTION);
throw e;
}
}
There is no 'packet loss', just bugs in your code.
The canonical way to copy a stream in Java is as follows:
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
If you know the number of bytes in advance and the sender must keep the connection open after the transfer, it becomes:
while (total < expected && (count = in.read(buffer, 0, expected-total > buffer.length ? buffer.length : (int)(expected-total))) > 0)
{
out.write(buffer, 0, count);
total += count;
}
Forget all the ByteArrayInput/OutputStreams and the extra copies. Just read from the file and send to the socket, or read from the socket and write to the file.
The sockets read method will return when its has obtained all the bytes you asked for, OR, when it stops receiving data from the network.
As transmission is often interrupted in any real network you need to keep issuing read calls until you have the number of bytes you want.
You need code something like this:
char [] buffer = new char[1024];
int expect = 1000;
int sofar = 0;
int chars_read;
try
{
while((chars_read = from_server.read(buffer[sofar])) != -1)
{
sofar = sofar + chars_read;
if (sofar >= expected) break;
}
}
catch(IOException e)
{
to_user.println(e);
}