Results 1 to 7 of 7
Thread: File Transfer with Sockets
- 12-09-2011, 08:40 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 16
- Rep Power
- 0
File Transfer with Sockets
Hi anybody like to tell me what I'm doing wrong here:
I'm trying to transfer a file with sockets and set the socket with the relevant IP and Port number, but while the client seems to send all the data , the server receives all but the very last buffer (approximately). The file is not corrupted, and the client code executes fine without exception.
Client side relevant code, which executes fine and exits the while loop:
FileInputStream fileInputStream = new FileInputStream(file);
byte[] buffer = new byte[socket.getSendBufferSize()];
int read = 0;
int readtotal = 0;
while ((read = fileInputStream.read(buffer)) != -1) {
readtotal = read + readtotal;
System.out.println("Writing :" + read + ", Total written:" + readtotal);
socket.getOutputStream().write(buffer, 0, read);
}
Server code is using ServerSocket to spawn a new thread, then
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
FileOutputStream fileOutputStream = new FileOutputStream(file);
byte[] buffer = new byte[socket.getReceiveBufferSize()];
int read = 0;
int readtotal = 0;
while ((read = inputStream.read(buffer))!= -1) {
readtotal = readtotal + read;
System.out.println("Writing :" + read + ", Total written:" + readtotal);
fileOutputStream.write(buffer, 0, read);
}
//Then I do some processing on the file here and close the i/o streams and socket but the program never exits the while loop, it reads up until about the last buffer size from the inputStream (according to the readTotal output) then nothing
Any help appreciated....
- 12-09-2011, 08:42 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Re: File Transfer with Sockets
Try having the client send the file size first and use that as the exit condition of the while loop on the server.
- 12-09-2011, 08:56 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 16
- Rep Power
- 0
Re: File Transfer with Sockets
Yeah good idea, but i'm still dropping data some where as I've checked the file size on client versus file size on server...
it should work, so that's what's puzzling me.. not clearing it up would leave a massive potential glitch..
But thanks for your advice...I'll test that now
- 12-09-2011, 09:20 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 16
- Rep Power
- 0
Re: File Transfer with Sockets
Tested... and it's still stalling on the "read = inputStream.read(buffer))!= -1" part
The readtotal variable never gets to the original file size because it needs to be "read" first and then added to, and this will always happen after the inputStream.read() method.......
So I think the -1 part is getting lost ...Any other suggestions ?
Interestingly if I break off the client and try again (a few times), though i get an error.. I can break the while loop with
if (readtotal == filesize) break;, .. Though its totally impractical to have to keep re uploading the file
below is the output from the client and server.
Client :
Writing :49152, Total written:49152
Writing :49152, Total written:98304
Writing :49152, Total written:147456
Writing :49152, Total written:196608
Writing :49152, Total written:245760
Writing :49152, Total written:294912
Writing :49152, Total written:344064
Writing :49152, Total written:393216
Writing :49152, Total written:442368
Writing :49152, Total written:491520
Writing :49152, Total written:540672
Writing :49152, Total written:589824
Writing :49152, Total written:638976
Writing :49152, Total written:688128
Writing :49152, Total written:737280
Writing :49152, Total written:786432
Writing :49152, Total written:835584
Writing :49152, Total written:884736
Writing :49152, Total written:933888
Writing :16328, Total written:950216
Finished :950216
Server:
Writing :65536, Total written:65536
Writing :65536, Total written:131072
Writing :65536, Total written:196608
Writing :50612, Total written:247220
Writing :47692, Total written:294912
Writing :49152, Total written:344064
Writing :49152, Total written:393216
Writing :49152, Total written:442368
Writing :49152, Total written:491520
Writing :49152, Total written:540672
Writing :49152, Total written:589824
Writing :49152, Total written:638976
Writing :49152, Total written:688128
Writing :49152, Total written:737280
Writing :65536, Total written:802816
Writing :65536, Total written:868352
Writing :65536, Total written:933888
Writing :16328, Total written:950216
Note the file tally at the end is identical.......... baffled?
- 12-09-2011, 11:02 PM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Re: File Transfer with Sockets
It looks like that client never sends the -1. Try removing the check and just read the buffer.Java Code:read = inputStream.read(buffer))!= -1
- 12-09-2011, 11:26 PM #6
Member
- Join Date
- Oct 2011
- Posts
- 16
- Rep Power
- 0
Re: File Transfer with Sockets
I don't think you can do that as how else are you to know the end of the file, as (readTotal == filesize) is based on inputStream.read(buffer) being used to add read to the readtotal.. Any way I've tried what you suggested a few ways and there's no change.
I've also changed port, changed IP, shutdown any servers running, turned off firewall, and tried to read the whole file into a byte[filesize] and send the whole thing in one go. I also tried with a simple txt file.. all these made no difference...
Is this windows thing , or a netbeans glitch?
Again thanks for the suggestions.. this is really bugging me now, as it's pretty standard java stuff
- 12-10-2011, 12:23 AM #7
Member
- Join Date
- Oct 2011
- Posts
- 16
- Rep Power
- 0
Re: File Transfer with Sockets
OK....
Anybody fancy running this code and figuring out why the server threads while loop never exits???
I slimmed it down to just the bare bones.....
3 classes and create a .txt file and name it "UPLOAD_ME.txt" or just download the attached src.zip (3 java source files and "UPLOAD_ME.txt")
Java Code://////////////////////////////////////////////////////////////UPLOADER (CLIENT) CLASS - BOCKYMURPHY package uploadtest; import java.io.FileInputStream; import java.net.Socket; public class Uploader { Socket socket; public static void main(String[] args) { try { Socket socket = new Socket("127.0.0.1", 9000); FileInputStream fileInputStream = new FileInputStream("src/uploadTest/UPLOAD_ME.txt"); byte[] buffer = new byte[socket.getSendBufferSize()]; int read = 0; int readtotal = 0; while ((read = fileInputStream.read(buffer)) != -1) { readtotal = read + readtotal; System.out.println("Writing :" + read + ", Total written:" + readtotal); socket.getOutputStream().write(buffer, 0, read); } socket.getOutputStream().flush(); System.out.println("Client Finished :"+ read + ", Total written:" + readtotal); } catch (Exception e) { e.printStackTrace(); } } } //////////////////////////////////////////////////////// END OF UPLOADER (CLIENT) CLASS - BOCKYMURPHY /////////////////////////////////////////////////////UPLOAD CLASS (SERVER) - BOCKYMURPHY package uploadtest; import java.net.ServerSocket; public class UploadTest implements Runnable { private ServerSocket serverSocket = null; private boolean RUN_SERVER = true; public UploadTest() { System.out.println("Server: initialized:"); } @Override public void run() { System.out.println("Server: running"); try { serverSocket = new ServerSocket(9000); while (RUN_SERVER) { new UploadTestThread(serverSocket.accept()).start(); } serverSocket.close(); } catch (Exception ex) { } } public static void main(String[] args) { new UploadTest().run(); } } ///////////////////////////////////////////////END OF UPLOAD CLASS (SERVER) - BOCKYMURPHY //////////////////////////////////////////////////////UPLOAD THREAD CLASS (SERVER) - BOCKYMURPHY package uploadtest; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.Socket; public class UploadTestThread extends Thread{ public Socket socket = null; public UploadTestThread(Socket accept) { this.socket = accept; } @Override public void run () { try { InputStream inputStream = socket.getInputStream(); FileOutputStream fileOutputStream = new FileOutputStream(new File("src/uploadTest/I_VE_BEEN_UPLOADED.txt")); byte[] buffer = new byte[socket.getReceiveBufferSize()]; int read = 0; int readtotal = 0; while ((read = inputStream.read(buffer)) != -1) { readtotal = readtotal + read; System.out.println("Writing :" + read + ", Total written:" + readtotal); fileOutputStream.write(buffer, 0, read); } System.out.println("Server Finished :"+ read + ", Total written:" + readtotal); } catch (IOException ex) { } } } //////////////////////////////////////////////////////UPLOAD THREAD CLASS (SERVER) - BOCKYMURPHY
Similar Threads
-
how to transfer file from to another by using FTP in java
By m_chenna in forum Java SoftwareReplies: 6Last Post: 02-08-2011, 07:08 AM -
file transfer
By prashant in forum NetworkingReplies: 3Last Post: 12-10-2009, 06:46 AM -
Regarding File Transfer in TCP/IP
By manabendra in forum NetworkingReplies: 2Last Post: 08-07-2009, 07:51 AM -
tcp file transfer
By ddj in forum NetworkingReplies: 2Last Post: 03-20-2009, 02:10 PM -
File and Message transfer over sockets!
By rameshraj in forum NetworkingReplies: 3Last Post: 05-14-2008, 08:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks