Home>

Server

Public Static Void Activ (Serversocket Serversocket) Throws IoException {
    Socket Socket= Serversocket.accept ();
    Inputstream sin= socket.getInputStream ();
    OutputStream Sout= socket.getoutputstream ();
    FileInputStream Fin= New FileInputStream (New File ("Log.txt"));
    Inputstream sfin= new bufferedInputstream (FIN);
    int ris= outils.copy (SFIN, SOUT);
    FIN.Close ();
    sfin.close ();
    Sout.flush ();
    Fin= New FileInputStream (New File ("Client.png"));
    sfin= new bufferedinputstream (FIN);
    res= outils.copy (SFIN, SOUT);
    Sout.flush ();
    Sout.Close ();
    socket.close ();

client

Public Class Main {
Public Static Void Main (String [] Args) Throws IoException {
    Socket Socket= New Socket ("127.0.0.1", 15123);
    Inputstream in= socket.getInputStream ();
    FileOutputStream Fout= New FileOutputStream (New File ("Log.txt"));
    OutputStream Out= New BufferedOutputStream (Fout);
    int ris= outils.copy (in, out);
    Out.Close ();
    Out.Flush ();
    Fout= New FileOutputStream (New File ("Client.png"));
    Out= New BufferedOutputStream (Fout);
    res= outils.copy (in, out);
    Out.Flush ();
    Out.Close ();
    in.close ();
    //bos.flush ();
    //bos.close ();
    socket.close ();
}

}

Trying to transfer files using ioutils.copy (), but faced with such a problem that the program all files folds into one file. I will give an example to be a little clearer: I'm trying to pass 2 files "log.txt" and "client.png". Upon completion of the program, these 2 files turn into one, i.e. Mixed. Who knows how to solve this problem? The file "log.txt" is as follows:

Update /Images Client.png Small
Update /Images Close.png Small

But at the output, he gives me the following:

Update /Images Client.png Small
Update /images close.png Small <
CLIENT.PNG > contents;
  • Answer # 1

    Before passing the file itself, transfer it to the length (and the file information is the name, the date of creation, etc.). The receiver reads this meta information and creates the correct size files with the correct attributes.

    Is it possible to specify something in the server part that the transmission of this file is over? Because I used to simply transmitted files without specifying their dimension and everything worked, but it was half a year ago and I have already forgot about it.

    Dmitry Lin2021-05-04 00:26:16

    You can close the channel. And for the next file to create it again.

    Mikhail Vaysman2021-05-04 00:26:16

    those. For each file separately create a channel? And if I have a list of files?

    Dmitry Lin2021-05-04 00:26:16

    @Dmitrylin just use my idea.

    Mikhail Vaysman2021-05-04 00:26:16