This question already has answers here:
Java multiple file transfer over socket
(3 answers)
Closed 5 years ago.
I am writing a program that has a client and server where the client will send a img file to the server. The code below is for the server where it will get stuck on that while loop from obIn.read blocking on its last run, so it never is able to return -1 and break the loop. It does break the loop for my Client. So I tried to flush it after the loop on the Client but it doesn't seem to do any good. I don't want to close obOut because that will in return close the socket which I want to keep open. The server end is where it receives the bites from the obIn(inputstream that is an instance variable) and writes it to a file that I have created.
//server receives file
File file = new File("image/image.png");
BufferedOutputStream fileOut = new BufferedOutputStream(new FileOutputStream(file));
byte[] bytes = new byte[1000];
int c = 0;
while((c = obIn.read(bytes)) != -1) {
fileOut.write(bytes, 0, c);
fileOut.flush();
System.out.println(c);
}
System.out.println(c);
fileOut.close();
System.out.println("File Created");
//Client
String imgPath = in.nextLine();
File file = new File(imgPath);
BufferedInputStream fileIn = new BufferedInputStream(new FileInputStream(file));
byte[] bytes = new byte[1000];
int c = 0;
while((c = fileIn.read(bytes)) != -1) {
obOut.write(bytes, 0, c);
obOut.flush();
System.out.println(c);
}
obOut.write(bytes, 0, 1000);
obOut.flush();
System.out.println(c);
fileIn.close();
System.out.println("File Sent");
This image is the output where the server is on top and the client is on bottom. That is where I found that the Server is stuck blocking.
Here is where I found this method and tried making it work for my setup. This is my first time working with streams.
In your client class try replacing write and flush function out side of while loop with obOut.close();
Related
This question already has answers here:
Java multiple file transfer over socket
(3 answers)
Closed 7 years ago.
I have a file sender and receiver. From the 10 or so posts I've found on the internet, this is the correct way to send files through sockets in Java. Take a look at the code snippets below.
Sender:
OutputStream bos = clientSocket.getOutputStream();
FileInputStream fis = new FileInputStream(sendingFile);
BufferedInputStream bis = new BufferedInputStream(fis);
int n;
int total = 0;
byte[] buffer = new byte[8192];
while ((n = bis.read(buffer)) > 0) {
bos.write(buffer, 0, n);
System.out.println("Sending File... ");
total += n;
System.out.println(total);
}
Receiver:
InputStream bis = clientSocket.getInputStream();
FileOutputStream fos = new FileOutputStream(fileOut);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int n;
int total = 0;
byte[] buffer = new byte[8192];
while ((n = bis.read(buffer)) > -1) {
bos.write(buffer, 0, buffer.length);
System.out.println("Writing File... ");
total += n;
System.out.println(total);
if (total == fileSize) {
break;
}
}
System.out.println("File Received");
Here's the output from the Sender
Sending File: D:\Users\Administrator\Documents\AAAAAAAAAAAAAAAAAAAAAAAAAAA.avi
File Size: 54236160
Sending File...
8192
Sending File...
16384
....................some time later
Sending File...
54231040
Sending File...
54236160
File Sent
The last number, 54236160, is the exact size of the file being sent. Now here is the last line on the receiver side.
Writing file...
54234710
So for this particular file, it always stops at this size and so because the entire file isn't sent, the receiver never stops waiting for data. I dont understand how the sender is sending the correct amount of data but the receiver doesn't get it all. I never see "File Received" on the receiver side and the amount of data that the receiver reads is never equal to the amount being sent.
This issue occurs for any file I sent and if I send really small files, such as those that are in bytes not kb, I dont see any "Writing File..." on the receiver side at all, almost like it just ignores the stream.
Yet every post on the internet says this is the proper way to send files. Oh and if I close the sending socket and/or stream, which I dont want to do because there are other things the client and server need to do, it still doesnt fix the problem anyway.
The other thing I've noticed is that while the sender always seems to indicate its writing the full amount to the buffer (always multiples of 8192 for "total"), the receiver doesnt.
Below is the code that gets the filesize and filename. Perhaps this is where the error lies? I dont see how since its all done before we start sending and receiving the file.
System.out.println("Receiving File");
try {
clientScanner.nextLine();
String fileName = clientScanner.nextLine();
int fileSize = clientScanner.nextInt();
System.out.println("File: " + fileName + " Size: " + fileSize);
File fileOut = new File("C:\\Users\\owner\\AppData\\Local\\temp\\" + fileName);
InputStream bis = clientSocket.getInputStream();
FileOutputStream fos = new FileOutputStream(fileOut);
BufferedOutputStream bos = new BufferedOutputStream(fos);
You shouldn't try to read more data than expected. Your loop should be:
int n;
int bytesLeft = fileSize;
byte[] buffer = new byte[8192];
while (bytesLeft > 0) {
int n = bis.read(buffer, 0, Math.min(buffer.length, bytesLeft));
if (n < 0) {
throw new EOFException("Expected " + bytesLeft + " more bytes to read");
}
bos.write(buffer, 0, n);
System.out.println("Writing File... ");
bytesLeft -= n;
System.out.println(total " bytes left to read");
}
(You can adjust the output to show how many bytes you've read instead of how many you've got left to read if you want, but I think this approach is simpler.)
If that still doesn't get you all the data, you should flush the output stream in the writing code - I wouldn't expect you'd have to, but if you're not closing the socket, I guess it could be buffering it forever.
If the loop terminates but the file appears to be incomplete, make sure you're closing bos after all of this.
If none of that helps, then it's possible that something else has read the start of your data before you get into this loop. You should compare the data in the output file with the original file. Look at the first (say) 16 bytes in the output file, and check that they're at the start of the input file. If they're not, then that suggests that the problem is with what you're doing with the connection before the code you've shown us. (For example, the code used to send and receive the expected file size.)
Now that we can see how you're sending / receiving the file size, that's definitely the problem. The scanner is reading some of the file data. To fix this, just use DataOutputStream on the sending side and DataInputStream on the receiving side:
DataOutputStream output = new DataOutputStream(clientSocket.getOutputStream());
output.writeUTF(fileName);
output.writeInt(fileSize);
// Write the data as before, to output
On the receiving side:
DataInputStream input = new DataOutputStream(clientSocket.getInputStream());
String fileName = input.readUTF(name);
int fileSize = input.readInt();
// Read the data before, as above, from input
I am trying to communicate between two machines using socket programming.
What I basically need is both machines should be able to send and receive files. The code I am pasting below is not showing any error but the server side program seems to be running indefinitely, i.e., it is not terminating. It got stuck on the line marked with comment stuck here.
In this code, initially, server is sending the file named "file.txt" and client is receiving it and saving the file with name "copy.txt". Later client is sending a file named "file2.txt" and server is receiving and saving it with name "copy2.txt".
Can someone please tell me the error and suggest some improvements?
//server side code
import java.net.*;
import java.io.*;
public class server
{
public static void main (String [] args ) throws IOException
{
//sending file started
ServerSocket serverSocket = new ServerSocket(16167);
Socket socket = serverSocket.accept();
System.out.println("Accepted connection : " + socket);
File transferFile = new File ("/Users/abhishek/desktop/file.txt");
byte [] bytearray = new byte [(int)transferFile.length()];
FileInputStream fin = new FileInputStream(transferFile);
BufferedInputStream bin = new BufferedInputStream(fin);
bin.read(bytearray,0,bytearray.length);
OutputStream os = socket.getOutputStream();
System.out.println("Sending Files...");
os.write(bytearray,0,bytearray.length);
os.flush();
System.out.println("File transfer complete");
//socket.close();
//sending comleted
//receiving file started
int filesize=1022386;
int bytesRead=0;
int currentTot = 0;
byte [] bytearray1 = new byte [filesize];
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("/Users/abhishek/desktop/copy2.txt");
//fos.flush();
BufferedOutputStream bos = new BufferedOutputStream(fos);
//bos.flush();
System.out.println("not moving ahead!!!");//program stucked here
bytesRead = is.read(bytearray1,0,bytearray1.length);
currentTot = bytesRead;
System.out.println("current"+currentTot);
do
{
bytesRead = is.read(bytearray1, currentTot, (bytearray1.length-currentTot));
if(bytesRead >= 0)
currentTot += bytesRead;
System.out.println("current"+currentTot);
} while(bytesRead > -1);
System.out.println("outside current"+currentTot);
bos.write(bytearray1, 0 , currentTot);
bos.flush();
//receiving complete
System.out.println("Receving file completed");
socket.close();
}
}
//client side code
import java.net.*;
import java.io.*;
public class client
{
public static void main (String [] args ) throws IOException
{
int filesize=1022386;
int bytesRead=0;
int currentTot = 0;
Socket socket = new Socket("localhost",16167);
byte [] bytearray = new byte [filesize];
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("/Users/abhishek/desktop/copy.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(bytearray,0,bytearray.length);
currentTot = bytesRead;
do
{
bytesRead = is.read(bytearray, currentTot, (bytearray.length-currentTot));
if(bytesRead >= 0)
currentTot += bytesRead;
} while(bytesRead > -1);
System.out.println("current"+currentTot);
bos.write(bytearray, 0 , currentTot);
bos.flush();
bos.close();
System.out.println("receiving first file completed!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
//sending file
System.out.println("sending second file started!");
File transferFile = new File ("/Users/abhishek/desktop/file2.txt");
byte [] bytearray2 = new byte [(int)transferFile.length()];
FileInputStream fin = new FileInputStream(transferFile);
BufferedInputStream bin = new BufferedInputStream(fin);
bin.read(bytearray2,0,bytearray2.length);
OutputStream os = socket.getOutputStream();
os.flush();
os.write(bytearray2,0,bytearray2.length);
os.flush();
System.out.println("sending second file completed!");
//sending complete
socket.close();
}
}
Does the System.out.println("not moving ahead!!!");//program stucked here line actually execute? if so, then the problem is that the InputStream.read() functions are blobking functions; they will stop execution of the program ("block") until they are able to complete.
From the JavaDoc for InputStream:
Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read is returned as an integer.
This method blocks until input data is available, end of file is detected, or an exception is thrown.
Since you aren't getting an exception, this means that when you call .read(), there is no data available to be read, and you program sits around waiting for data to read (that never arrives). You should check that your client program is actually sending the data in the first place.
I'd bet
bytesRead = is.read(bytearray1,0,bytearray1.length);
is where you're really getting stuck. The problem normally if you are stuck here is that the other side of the communication has not sent any data, there's nothing to read, and your thread is stuck waiting for it to send.
on your client side, you call
bos.close();
after sending the first message. This is going to cause the socket to close as well, which will throw an IOException on your server end, and because you are not catching the IOException, your server program will just exit.
How much socket experience do you have? If you are just beginning with sockets, you might want to check out the extensions I wrote around this, starting with ServerSocketEx and DataFetcher.
I run the server and client on separate cmd windows. The server prints out "Upload successful" and indeed the file gets uploaded to the server but the client freezes and i have to quit the client process using Ctrl+C. It seems to me that the client cannot break out of the while loop. Please tell me where i am going wrong?
(The code snippets are parts of much larger code body that i am writing to implement file read, write, read/write-lock, delete file etc from the server. Once a request to upload the file from the client is handled by the server I want the process to return to the command line menu i have incorporated with the client program[not shown below]. So it is important that the client returns to that user interface. Any help is greatly appreciated. Thanks in advance! )
Client side:
// initialized client and server connection made...
BufferedReader filePath = new BufferedReader(new InputStreamReader(System.in));
String pathname = filePath.readLine();
try{
File file_to_upload = new File(pathname);
byte[] bytearray = new byte[(int) file_to_upload.length()];
OutputStream os = client1.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file_to_upload));
do {
bis.read(bytearray, 0, bytearray.length);
os.write(bytearray, 0, bytearray.length);
os.flush();
bis.close();
} while (bis.read(bytearray, 0, bytearray.length)!= -1);
}
catch(FileNotFoundException e){
System.out.println(e);
}
catch(IOException e){
System.out.println(e);
}
System.out.println("\n" + inFromServer.readLine() + "\n");
client1.close();
break;
server side:
byte[] mybytearray = new byte[1];
InputStream is = server1accept.getInputStream();
FileOutputStream fos = new FileOutputStream("234rews");
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead = is.read(mybytearray, 0, mybytearray.length);
bos.write(mybytearray, 0, bytesRead);
bos.close();
outToClient.writeBytes("Uploaded");
System.out.println("Upload Successful");
Both your copy loops are incorrect. The canonical way to copy between streams in Java is:
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
At present you are doing extra reads and ignoring read() results and all kinds of other strange things. Use this code at both ends, with any buffer size greater than zero.
I was trying to write simple "FTP" program, but then suddenly an error occured. So this is a network with client and server and a server storages files uploaded from client, there is also a possibility to download files from server. But when I upload file it is saved in Server directory as an empty file, will someone help me find an error in code?
Here is Client
String nameOfFileToUp = fileFromFileChooser.getName();
System.out.println("fileChooserfile name= " + fileFromFileChooser.getName());
System.out.println("File path= " + fileFromFileChooser.getPath());
pw.println(nameOfFileToUp);
File sendFile = new File(fileFromFileChooser.getPath());
FileInputStream fis = new FileInputStream(sendFile);
int size =(int) fileFromFileChooser.length();
byte[] buffer = new byte[size+1];
int bytes = 0;
while((bytes = fis.read(buffer)) != -1)
{
out.write(buffer,0,bytes);
}
fis.close();
Where pw is PrintWriter,
And Server
FileOutputStream fos = new FileOutputStream(f);
DataOutputStream dops = new DataOutputStream(fos);
while(done)
{
fc = in.readLine();
if(fc == null)
{
done = false;
}
else
{
dops.writeChars(fc);
}
}
fos.close();
Can anyone help? Please
You need to flush/close the output stream.
Also, your server should not be reading by "line", it should be reading bytes (just like your client code).
Hii i am uploading a file to server using socket and i need the percent of file loaded?how can i do that?i have the maximun value i.e the file length ,how can i get how much file has been uploaded?
FileInputStream fis = new FileInputStream(fil);
BufferedInputStream in = new BufferedInputStream(fis);
BufferedOutputStream out = new BufferedOutputStream(skt.getOutputStream());
//Write the file to the server socket
int i;
while ((i = in.read()) != -1) {
publishProgress(???);
out.write(i);
System.out.println(i);
}
I need to pass the length of file uploded in the publishProgress method.
using buffered copying
FileInputStream fis = new FileInputStream(fil);
BufferedInputStream in = new BufferedInputStream(fis);
BufferedOutputStream out = new BufferedOutputStream(skt.getOutputStream());
//Write the file to the server socket
int i;
int written = 0;
byte[] buf = new byte[512];
while ((i = in.read(buff)) != -1) {
out.write(buff,0,i);
written += i;
publishProgress((double)written/length);
//passing a double value from 0-1 to say how much is transmitted (length is length of file)
System.out.println(buff+", "+i);
}
To do this you need to do one of a couple of things:
Use a Flash uploader such as swfupload (see http://demo.swfupload.org/Documentation/) as these typically provide access to upload progress of this sort.
Provide a back channel of your own: perform the form submit with Ajax and then while the form submit occurs you run a javascript timer that hits a URL on the server with a key of some kind that corresponds to the upload. The URL on the server looks up how much of the file has been uploaded and returns that number and you pass that through to your uploadProgress method.
Below is your modified code. written holds the number of ints written to the socket.
FileInputStream fis = new FileInputStream(fil);
BufferedInputStream in = new BufferedInputStream(fis);
BufferedOutputStream out = new BufferedOutputStream(skt.getOutputStream());
//Write the file to the server socket
int i;
int written = 0;
while ((i = in.read()) != -1) {
out.write(i);
publishProgress(++written);
System.out.println(i);
}
javax.swing.ProgressMonitorInputStream