Results 1 to 7 of 7
Thread: Transfer file with Socket class
- 02-01-2012, 11:49 PM #1
Transfer file with Socket class
I am trying to transfer a file from one PC to an other PC with java.net.Socket class. I get exception after exactly 1024 bytes have been transferred.
I did use the code from this tutorial:
Transfer a file via Socket : Socket*«*Network Protocol*«*Java
And the exception:
Note that it is the server class that causes the exception. Line 27 look like this:Java Code:java.net.SocketException: Connection reset by peer: socket write error at java.net.SocketOutputStream.socketWrite0(Native Method) at java.net.SocketOutputStream.socketWrite(Unknown Source) at java.net.SocketOutputStream.write(Unknown Source) at lab.testing.Server.main(Server.java:27)
os.write(mybytearray, 0, mybytearray.length); (see the rest of the code on the page I linked above).
Why is this happening?
- 02-02-2012, 12:04 AM #2
Re: Transfer file with Socket class
What is the code on the other side of the connection doing?
Did it close the connection?
- 02-02-2012, 12:22 AM #3
Re: Transfer file with Socket class
yeah, I have modified it a bit so it always close the conncetion. however I do not think that code is working, but I found this:
Transfer a file via Socket - Real's Java How-to
I got some problems in this code. This time however, I do not get exception. I just get nothing. It does not transfer any byte at all.
After some minor modifications:
Java Code:import java.io.BufferedInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) throws IOException { ServerSocket servsock = new ServerSocket(1202); File myFile = new File("C:\\test.obj"); while (true) { Socket sock = null; try { sock = servsock.accept(); //Send the file size int fileSize = (int) myFile.length (); DataOutputStream out = new DataOutputStream (sock.getOutputStream()); out.writeInt (fileSize); //Send the file byte[] mybytearray = new byte[fileSize]; BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile)); bis.read(mybytearray, 0, mybytearray.length); OutputStream os = sock.getOutputStream(); os.write(mybytearray, 0, mybytearray.length); os.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { sock.close(); break; } catch (Exception e) {} } } } }Java Code:import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.Socket; public class Client { public static void main(String[] argv) throws Exception { Socket sock = null; BufferedOutputStream out = null; try { sock = new Socket("127.0.0.1",1202); InputStream is = sock.getInputStream(); File file = new File ("testNew.obj"); FileOutputStream fos = new FileOutputStream(file); out = new BufferedOutputStream(fos); //Get the file size DataInputStream in = new DataInputStream (sock.getInputStream()); int fileSize = in.readInt (); //Get the file int bytesRead; int current = 0; byte [] mybytearray = new byte [fileSize]; bytesRead = is.read(mybytearray,0,mybytearray.length); current = bytesRead; do { bytesRead = is.read(mybytearray, current, (mybytearray.length-current)); if(bytesRead >= 0) current += bytesRead; } while(bytesRead > -1); out.write(mybytearray, 0 , current); out.flush(); } catch (IOException e) {} finally { try { if (out != null) out.close(); } catch (Exception e) {} try { if (sock != null) sock.close(); } catch (Exception e) {} } } }
- 02-02-2012, 12:25 AM #4
Re: Transfer file with Socket class
Your empty catch blocks are a worry. Put a call to printStackTrace in them.
Add some println statements to show what is happening. Print out variable values as the code executes.
- 02-02-2012, 01:00 AM #5
Re: Transfer file with Socket class
hmm. I changed the file object in the Client to "E:\\testNew.obj". Now i get heap space error at line 33 on the client file. wtf?
oh and the test file was a byte in size.
- 02-02-2012, 01:31 AM #6
Re: Transfer file with Socket class
Never mind. I created my own method my way and it works flawless.
- 02-02-2012, 01:38 AM #7
Similar Threads
-
pls..transfer data in socket
By langkhach_codon in forum NetworkingReplies: 7Last Post: 03-28-2010, 05:21 AM -
file transfer
By prashant in forum NetworkingReplies: 3Last Post: 12-10-2009, 06:46 AM -
socket communication taking 4 minutes to transfer 1mbytes to client
By santhosh_el in forum NetworkingReplies: 5Last Post: 11-01-2009, 05:16 PM -
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


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks