Good afternoon, there is a server

public class Server_main implements Runnable { private ServerSocket server; private Socket client; /*****************STREAM*****************/ static private BufferedOutputStream output; static private BufferedInputStream input; /***************************************/ public static void main(String[] args) { new Thread (new Server_main()).start(); } @Override public void run() { // TODO Auto-generated method stub byte[] byteArray = new byte[8192]; int in; try { server= new ServerSocket(60000/*Π½ΠΎΠΌΠ΅Ρ€ ΠΏΠΎΡ€Ρ‚Π°*/, 10/*колличСство ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠΉ*/); client= server.accept(); output = new BufferedOutputStream(new FileOutputStream("D:/")); input = new BufferedInputStream(client.getInputStream()); while ((in = input.read(byteArray)) != -1){ output.write(byteArray,0,in); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { input.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { output.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 

which after receiving the file stops working. After that, it must be run again manually. The question is where in the code to register the cycle for its uninterrupted work?

    1 answer 1

    The easiest way is this:

     server = new ServerSocket(60000/*Π½ΠΎΠΌΠ΅Ρ€ ΠΏΠΎΡ€Ρ‚Π°*/, 10/*колличСство ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠΉ*/); while (true) { client= server.accept(); output = new BufferedOutputStream(new FileOutputStream("D:/")); input = new BufferedInputStream(client.getInputStream()); while ((in = input.read(byteArray)) != -1){ output.write(byteArray,0,in); } } 

    However, it is worth noting that this is a very rough approach. It would be much better to handle the connection in a separate thread, something like

     server = new ServerSocket(60000/*Π½ΠΎΠΌΠ΅Ρ€ ΠΏΠΎΡ€Ρ‚Π°*/, 10/*колличСство ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠΉ*/); while (true) { client = server.accept(); new ClientThread(client).start(); } 

    And separately:

     public class ClientThread extends Thread { private Socket socket; public ClientThread(Socket socket) { this.socket = socket; } public void run() { File out = File.createTempFile("mysrv", ".dat", new File("D:/")); output = new BufferedOutputStream(out); input = new BufferedInputStream(socket.getInputStream()); byte[] byteArray = new byte[1024]; int in; while ((in = input.read(byteArray)) != -1){ output.write(byteArray,0,in); } } } 

    Naturally, it is necessary to catch mistakes. And it would be good to limit the number of clients that can be connected at the same time - but the basic idea, I hope, is clear.

    • @ AlekseyShimansky Yes, exactly, thank you - Aleks G
    • Thanks, it worked with the second method, but immediately there appeared another question as the server constantly spins those files that I uploaded to it. - Varg Sieg
    • one
      @VargSieg Well, this is understandable - after all, this is exactly what the code is. There are several ways to work with this. One option is to look at what data is received through a socket. If a specific team, then organize a stop. Or through some other thread to stop the server thread. - Aleks G
    • one
      @VargSieg well, twist it to a certain condition .. For example, if the user sent the exit keyword or whatever you want - Alexei Shimansky
    • @ Alexey Shimansky Thank you) - Varg Sieg