Results 1 to 7 of 7
  1. #1
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default 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:

    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)
    Note that it is the server class that causes the exception. Line 27 look like this:
    os.write(mybytearray, 0, mybytearray.length); (see the rest of the code on the page I linked above).

    Why is this happening?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,392
    Rep Power
    25

    Default Re: Transfer file with Socket class

    What is the code on the other side of the connection doing?
    Did it close the connection?

  3. #3
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default 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)
    			{}
    		}
    	  }
      }

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,392
    Rep Power
    25

    Default 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.

  5. #5
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default 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.

  6. #6
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Re: Transfer file with Socket class

    Never mind. I created my own method my way and it works flawless.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,392
    Rep Power
    25

    Default Re: Transfer file with Socket class

    Glad you figured it out.

Similar Threads

  1. pls..transfer data in socket
    By langkhach_codon in forum Networking
    Replies: 7
    Last Post: 03-28-2010, 05:21 AM
  2. file transfer
    By prashant in forum Networking
    Replies: 3
    Last Post: 12-10-2009, 06:46 AM
  3. Replies: 5
    Last Post: 11-01-2009, 05:16 PM
  4. Regarding File Transfer in TCP/IP
    By manabendra in forum Networking
    Replies: 2
    Last Post: 08-07-2009, 07:51 AM
  5. tcp file transfer
    By ddj in forum Networking
    Replies: 2
    Last Post: 03-20-2009, 02:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •