0

I'm making a communicator with ability to send files. So far I managed to make text sending working using additional thread (listener).

I'm trying to make the same thing with files, but I don't know, how can I make a file listener - a thread, that detects incoming file, downloads it and listens for another file. Also, I don't know if I'm making my file sender properly. Could you help?

Current sender code:

try {
    InputStream in = new FileInputStream(fileToSend);
    OutputStream out = fileConn.getOutputStream();
    Controller.copyData(in, out);
    out.close();
    in.close();
} catch (IOException e) {
    System.out.println("Problem!");
}

And receiver code:

while (true)
{
    try {
        InputStream in = socket.getInputStream();
        OutputStream out = new FileOutputStream("hi.txt"); //temporary
        Controller.copyData(in, out);
        out.close();
        in.close();
    } catch (IOException e) {
        System.out.println("Problem!");
    }
}

EDIT: I forgot to add my copyData. There it is:

public static void copyData(InputStream in, OutputStream out) throws IOException{
    byte[] buf = new byte[8192];
    int len = 0;
    while ((len = in.read(buf)) != -1) {
        out.write(buf, 0, len);
    }
}
Chmielok
  • 150
  • 1
  • 7

1 Answers1

-1

You can achive tha by just adding to your listening thread option to wait for diffrent messages/options and react accordingly. For example:

private class WaitingThread extends Thread {
    volatile boolean awaitsServer = false;
    DataInputStream dataInput = new DataInputStream(inputStream);
    public void run() {
        while (connected) {
            int message = 0;
                if (awaitsServer == true) {
                    if (dIn.available() ==0) {
                        view.setLog("waiting");
                    } else {
                        message = dIn.readInt();
                        switch (tempMessage) {
                        // TO DO ALL KIND OF COMMUNICATION 

                        case 1: 
                            int filesize = dataInput.readInt();
                            int bytesRead;
                            int currentTot = 0;
                            byte[] bytearray = new byte[filesize];
                            int len = dataInput.readInt();
                            FileOutputStream fos = new FileOutputStream(currentlySelectedFile);
                    BufferedOutputStream bos = new BufferedOutputStream(fos);
                    bytesRead = dataInput.read(bytearray, 0, bytearray.length);

                    currentTot = bytesRead;
                    do {
                        bytesRead = dataInput.read(bytearray, currentTot,
                                (len - currentTot));
                        if (bytesRead >= 0)
                            currentTot += bytesRead;

                    } while (currentTot < len);
                    bos.write(bytearray, 0, currentTot);
                    bos.close();

                    }
                                case 2: //GET TEXT
                                case 3: //DO SOMETHING ELSE
}}}

Btw you have example how to send files.

k0staa
  • 319
  • 2
  • 5
  • Classic pointless, CPU-wasting misuse of `available()` ; classic misuse of a too-large buffer that may not fit into memory, or whose size may not fit into an `int`K and which needlessly adds latency. See my answer in the duplicated question for a correct solution. – user207421 May 17 '15 at 22:20