0

I am trying to make a server which receives some big files sent by the client and create the same file on an another location (basically download that file and move it in another place). The problem is that the servers freeze when trying to send big files. Server.java:

    package testing_server;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;

public class Server {
    public static void main(String[] args) throws IOException, InterruptedException
    {
    ServerSocket server = new ServerSocket(2000);

    Client client2 =  new Client();
    client2.main("");   
    
    while (true)
    {   
        
        Socket client = server.accept();
        DataInputStream in = new DataInputStream(client.getInputStream());
        
        int a;
        byte[] buffer = new byte[1025];

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        while ((a= in.read(buffer))!=-1 )
            {
            bos.write(buffer,0,a);
            bos.flush();
            System.err.println(a);
            }
        

        
    }

}
}

And this is Client.java

package testing_server;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Iterator;

public class Client {
public static void main(String args) throws UnknownHostException, IOException, InterruptedException
{
    Socket server = new Socket("ip",2000);
    DataOutputStream out =  new DataOutputStream(server.getOutputStream());

    final FileChannel channel = new FileInputStream("test.jpg").getChannel();
    MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());

    byte[] ar = new byte[1025];
    //this is only for testing 
    int many = buffer.capacity()/1025;
    
    int i  =0;
    while (i<=many)
    {
        buffer.get(ar);
        out.write(ar,0,1025);
        out.flush();
        
    
        i++;    
    }

    
    // when finished
    channel.close();
    out.close();
    server.close();
}
}

How can I send this big file without freeze. And can I send it in only one piece so I won't lose bytes? And if not how can i somehow manage to not lose bytes while sending chunks? I mention that the file is 10MB so it is not a very large file. I want to manage this without compression.

DanCodes
  • 51
  • 1
  • 5
  • 1
    I suggest you read the hundreds of others questions and answers related to your problem. If you're losing bytes, your code is faulty. If your server freezes, your code is faulty. Servers are usually multi-threaded, yours is single-threaded so multiple clients can't connect at the same time. – Kayaman Sep 02 '20 at 12:11
  • @Kayaman as i said,this is only for testing. I have my server with multi-threading and i just test this feature so I can add it later. There are not even 10 questions about this problem, and I really do not understand those questions so that's why I am asking. – DanCodes Sep 02 '20 at 17:26
  • There are [way over 10](https://google.com/search?q=site%3Astackoverflow.com+java+send+files+socket) questions about this problem. There's explanations, even complete code. – Kayaman Sep 03 '20 at 06:28

0 Answers0