I'm trying to send large files using socket programming in java for a p2p file sharing application. This code is sending 200-300 mb files without any problems, but for large files around 1 gb it is giving the error:-
java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at java.io.BufferedOutputStream.write(BufferedOutputStream.java:105)
at FileSender.main(FileSender.java:28)
I'm already sending file in small chunks as suggested in many answers I got. What should I do to send 1 gb files. I'm programming on Windows.
Here are my codes:
Server
public class FileSender {
public static void main(String...s)
{
BufferedOutputStream bos;
String file="D:\\filename.mp4";
try {
ServerSocket sock=new ServerSocket(12345);
while(true)
{
System.out.println("waiting");
Socket soc=sock.accept();
bos= new BufferedOutputStream(soc.getOutputStream());
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
int n=-1;
byte[] buffer = new byte[8192];
while((n = bis.read(buffer))>-1)
{
bos.write(buffer,0,n);
System.out.println("bytes="+n);
bos.flush();
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Reciever
public class FileReciev {
public static void main(String...s)
{
try
{
Socket sock=new Socket("127.0.0.1",12345);
File file=new File("D:\\newfilename.mp4");
BufferedInputStream bis=new BufferedInputStream(sock.getInputStream());
FileOutputStream fos = new FileOutputStream(file);
int n;
byte[] buffer = new byte[8192];
System.out.println("Connected");
while ((n = bis.read(buffer)) > -1) {
System.out.println("bytes="+n);
fos.write(buffer, 0, n);
if(n<(8192)){
fos.close();
bis.close();
break;
}
fos.flush();
}
System.out.println("recieved");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
if(n<(8192)){
fos.close();
bis.close();
What this means is if you ever get less than the number of bytes that you expected, you'll shut down your socket. There's no reason to do this.
Related
So I have this thing that I'm making and the problem is that I think that some bytes are lost while the file is compressed and send to server as well as saved locally. I was able to send data while not using ByteArrayOutputStream but I have to use it and can't just use socket's OutputStream directly as I need each sent file to have somwhat unique ID. Also sizes of out.zip and out2.zip are different by around 10kB (out2.zip is smaller)
here is Log dump: https://pastebin.com/UyTqnRYc
Function that sends data in chunks
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
ZipParameters zipParameters = buildZipParameters(compressionMethod);
ObjectOutputStream objOS = new ObjectOutputStream(outputStream);
File f = new File("out2.zip");
FileOutputStream fos = new FileOutputStream(f);
for (File directory:pathsToDirectories) {
String relativeRootDirectory = directory.getName();
List<File> filesAndDirectories = Arrays.stream(Objects.requireNonNull(directory.listFiles())).toList();
for (File somefile:filesAndDirectories) {
if(somefile.isDirectory()){
addDirectoryToZip(zos,somefile,zipParameters,relativeRootDirectory,rootDirectoryName);
}
else
{
if (zipParameters.getCompressionMethod() == CompressionMethod.STORE) {
zipParameters.setEntrySize(somefile.length());
}
byte[] buff = new byte[4096];
zipParameters.setFileNameInZip(rootDirectoryName+ "/"+somefile.getName()); //YIKES
zos.putNextEntry(zipParameters);
try(InputStream inputStream = new FileInputStream(somefile)) {
int readLen;
while (((readLen = inputStream.read(buff)) != -1)) {
zos.write(buff);
byte[] b = baos.toByteArray();
System.out.println("written :"+baos.toByteArray().length);
objOS.writeObject(new TransportDataModel((byte) 1,baos.toByteArray()));
fos.write(baos.toByteArray());
baos.reset();
baos.flush();
}
}
zos.closeEntry();
}
}
}
objOS.writeObject(new TransportDataModel((byte) 1,baos.toByteArray()));
zos.flush();
System.out.println("REST OF IT :"+ Arrays.toString(baos.toByteArray()));
System.out.println(baos.toByteArray().length);
objOS.writeObject(new TransportDataModel((byte) 1,baos.toByteArray()));
fos.write(baos.toByteArray());
baos.reset();
System.out.println("REST OF IT :"+ Arrays.toString(baos.toByteArray()));
fos.flush();
fos.close();
objOS.writeObject(new TransportDataModel((byte) -1,new byte[0]));
zos.close();
}
Record used as a packet
import java.io.Serializable;
public record TransportDataModel(byte id, byte[] data) implements Serializable {
}
Server code:
public Server(int port)
{
// starts server and waits for a connection
try
{
server = new ServerSocket(port);
System.out.println("Server started");
System.out.println("Waiting for a client ...");
socket = server.accept();
System.out.println("Client accepted");
in = new ObjectInputStream(socket.getInputStream());
File f = new File("out.zip");
FileOutputStream outputStream = new FileOutputStream(f);
TransportDataModel transportDataModel;
while (true) {
transportDataModel= (TransportDataModel) in.readObject();
if (transportDataModel.id() == -1) {
break;
}
if(transportDataModel.data().length != 0)
{
//System.out.println(transportDataModel.id());
outputStream.write(transportDataModel.data());
}
}
outputStream.flush();
outputStream.close();
System.out.println("Closing connection");
// close connection
socket.close();
in.close();
ZipFile zipFile = new ZipFile("out.zip");
zipFile.extractAll("/home/xenu/IdeaProjects/BBT-BasicBackupTool");
}
catch(IOException i)
{
i.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
I have no problems sending large text documents, or other files over sockets with my java program, but when I try to send a .jpg file or other image I get java.net.SocketException: Connection reset.
I have not run into any trouble when sending a 229 KB text document over the socket, but when I try to send the 89 KB image, I get the error. I use a while loop to read and write the file.
This is the part of the server class in question (it is named EasyDataSend):
public EasyDataSend() throws IOException
{
port = 8080;
server = new ServerSocket(port);
socket = server.accept();
dataOutputStream = new DataOutputStream(socket.getOutputStream());
}
public void sendFile(String path) throws IOException
{
File file = new File(path);
InputStream fileInputStream = new FileInputStream(file);
OutputStream fileOutputStream = socket.getOutputStream();
byte[] bytes = new byte[16 * 1024];
int count;
while ((count = fileInputStream.read(bytes)) > 0)
{
fileOutputStream.write(bytes, 0, count);
}
fileOutputStream.close();
fileInputStream.close();
socket.close();
server.close();
}
And this is the part of the client class (name EasyDataReceive):
public EasyDataReceive() throws UnknownHostException, IOException
{
ip = "127.0.0.1";
port = 8080;
socket = new Socket(ip,port);
}
public void receiveFile(String path) throws IOException, SocketException
{
File file = new File(path);
InputStream fileInputStream = socket.getInputStream();
OutputStream fileOutputStream = new FileOutputStream(file);
byte[] bytes = new byte[16*1024];
int count;
while ((count = fileInputStream.read(bytes)) > 0)
{
fileOutputStream.write(bytes, 0, count);
}
fileOutputStream.close();
fileInputStream.close();
socket.close();
}
This is the error I get:
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at EasyDataReceive.receiveFile(EasyDataReceive.java:111)
at TesterClient.main(TesterClient.java:23)
Also, the line 111 is really just the start of the while loop in the client class. I did not want to paste the whole class into the post. The line 23 is just part of the testing class where I construct the EasyDataReceive object.
Your socket should be inside the methods: receiveFile() and sendFile().
For example,
public static void sendFile(String path) throws IOException {
try {
socket = new Socket("127.0.0.1", 8080);
System.out.println("Connected");
File file = new File(path);
InputStream fileInputStream = new FileInputStream(file);
OutputStream fileOutputStream = socket.getOutputStream();
byte[] bytes = new byte[16 * 1024];
int count;
while ((count = fileInputStream.read(bytes)) > 0) {
fileOutputStream.write(bytes, 0, count);
}
fileOutputStream.close();
fileInputStream.close();
} catch (UnknownHostException u) {
System.out.println(u);
} catch (IOException i) {
System.out.println(i);
} finally {
try {
socket.close();
} catch (IOException i) {
System.out.println(i);
}
}
}
Or you should pass the socket client to your method, i.e.
private void receiveFile (Socket client) {
...
}
I am working on transfer file from server to client example.When i am trying to transfer large file (in example i have 500mb video file) then it shows error Android:java.lang.OutOfMemoryError Failed to allocate a 360318346 byte allocation with 5802592 free bytes and 87MB until OOM. Please help me how to solve this error.
Server side some peace of code
public class FileTxThread extends Thread {
Socket socket;
FileTxThread(Socket socket){
this.socket= socket;
}
#Override
public void run() {
File file = new File(Environment.getExternalStorageDirectory(),"RAHASYA.mp4");
byte[] bytes = new byte[(int) file.length()];
BufferedInputStream bis;
try {
bis = new BufferedInputStream(new FileInputStream(file));
bis.read(bytes, 0, bytes.length);
OutputStream os = socket.getOutputStream();
os.write(bytes, 0, bytes.length);
os.flush();
// socket.close();
final String sentMsg = "File sent to: " + socket.getInetAddress();
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(MainActivity.this,sentMsg,Toast.LENGTH_LONG).show();
}});
Client side some peace of code
private class ClientRxThread extends Thread {
String dstAddress;
int dstPort;
ClientRxThread(String address, int port) {
dstAddress = address;
dstPort = port;
}
#Override
public void run() {
Socket socket = null;
try {
socket = new Socket(dstAddress, dstPort);
out.println("Sockt "+socket.getInetAddress());
File file = new File(Environment.getExternalStorageDirectory(),"RAHASYA.mp4");
byte[] bytes = new byte[1024];
InputStream is = socket.getInputStream();
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
while (true) {
int bytesRead = is.read(bytes);
if (bytesRead < 0) break;
bos.write(bytes, 0, bytesRead);
// Now it loops around to read some more.
}
bos.close();
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(MainActivity.this,"Finished",Toast.LENGTH_LONG).show();
}});
your byte array on server side is too large , change it smaller and try ?
for reference:
File file = new File(Environment.getExternalStorageDirectory(),"RAHASYA.mp4");
byte[] bytes = new byte[2048];
BufferedInputStream bis;
try {
bis = new BufferedInputStream(new FileInputStream(file));
OutputStream os = socket.getOutputStream();
int read = bis.read(bytes, 0, bytes.length);
while (read!=-1){
os.write(bytes, 0, read);
os.flush();
read = bis.read(bytes, 0, bytes.length);
}
//...and so on
Your problem is the line
byte[] bytes = new byte[(int) file.length()];
You are creating an array of the size of the whole file. You need to read a bit at a time.
I have created a program to send file from client to server but when i am running that file in localhost it works good and i can send the file but when i am sending that file from another PC to my PC it shows "java.net.ConnectException: Connection refused".
Port number at both side same and firewall is also off. Even port is listening when client request to connect server socket is also accepting the socket.
Thank you in advance
Code of my program is :
public class ProgramSocket {
public ProgramSocket(ServerSocket servsock) throws IOException {
System.out.print("server started");
while (true) {
final Socket sock;
try {
sock = servsock.accept();
} catch (SocketException e) {
break;
}
new Thread(new Runnable() {
#Override
public void run() {
try {
connectToNewClient(sock);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
System.out.print("server stopped");
}
void connectToNewClient(Socket sock) throws IOException,
InterruptedException {
FileOutputStream fos = new FileOutputStream("C:/abc.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead = is.read(mybytearray, 0, mybytearray.length);
bos.write(mybytearray, 0, bytesRead);
bos.close();
sock.close();
}
}
Client side :
public String uploadFile(String filePath) throws Exception {
Socket sock = new Socket(ip, 24999);
// select file
File myFile = new File(filePath);
// send the program file
byte[] mybytearray = new byte[(int) myFile.length()];
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(myFile));
OutputStream os = sock.getOutputStream();
bis.read(mybytearray, 0, mybytearray.length);
os = sock.getOutputStream();
os.write(mybytearray, 0, mybytearray.length);
os.flush();
bis.close();
sock.close();
return message;
}
omitted some code for brevity.
hi i am trying to create a program in java for sending files from multiple client to single server. is this is possible?
i tried this could for multiple client and single server and also it is from server to client sending program.
Client Program
public class Client {
public Client(String ip,String path,int i) throws IOException {
int filesize=2022386;
int bytesRead;
int currentTot = 0;
File file=new File(path);
File dir=new File("c:/PAMR/Dest"+i+"/");
String name=file.getName();
if(!dir.exists())
{
dir.mkdir();
}
Socket socket = new Socket(ip,9100);
byte [] bytearray = new byte [filesize];
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream(dir+"/"+name);
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);
bos.write(bytearray, 0 , currentTot);
bos.flush();
bos.close();
socket.close();
}
}
for Server
public class Source {
public Source(String path) throws IOException {
ServerSocket serverSocket = new ServerSocket(9100);
System.out.println("Server is in Listening Mode");
Socket socket = serverSocket.accept();
File transferFile = new File (path);
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();
os.write(bytearray,0,bytearray.length);
bin.close();
os.close();
os.flush();
socket.close();
serverSocket.close();
}
}
it is a part of code for my project i am doing.
Any body pls help.
You want something like this:
boolean working = true;
ServerSocket ss = new ServerSocket(9100);
while(working) {
Socket s = ss.accept();
MyThread myThread = new MyThread(s);
myTread.start();
}
where MyThread is a class of yours that extends the Thread class, accepts the connection to the client that just connected and then accepts any file or data sent to it.