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:
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?
Re: Transfer file with Socket class
What is the code on the other side of the connection doing?
Did it close the connection?
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:
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)
{}
}
}
}
}
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)
{}
}
}
}
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.
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.
Re: Transfer file with Socket class
Never mind. I created my own method my way and it works flawless.
Re: Transfer file with Socket class