0

I created a client-server application for exchanging strings and files. I need to send the same file to all clients, but only the first client receives that file. Funny thing I noted is that when first client receives file, and I open and close that file on file system, then the second client receives that file.

Server side, sending file:

private static void sendFile(String path, OutputStream os) throws IOException {

    File file = new File(path);
    byte[] toSent  = new byte [(int)file.length()];

    FileInputStream fis = new FileInputStream(file);
    BufferedInputStream bis = new BufferedInputStream(fis);
    bis.read(toSent, 0, toSent.length);

    os.write(toSent);
    os.flush();

    bis.close();

}

Client side, receiving file:

private static void receiveFile(String path, InputStream is){
    int bytesRead = 0;
    byte [] toRecieve  = new byte [2048];

    try(BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path))) {

        bytesRead = is.read(toRecieve, 0, toRecieve.length);

        bos.write(toRecieve, 0, bytesRead);

        bos.flush();

    } catch (IOException e) {
        e.printStackTrace();
    } 
}

Definition of OutputStream object in ServerThread class(extends Thread):

Socket socket;
BufferedReader in;
PrintWriter out;
OutputStream os;
InputStream is;

public ServerPricaloThread(Socket socket) {
    super();
    this.socket = socket;

    try {
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
        os = socket.getOutputStream();
        is = socket.getInputStream();
    } catch (IOException e) {
        e.printStackTrace();
    }

    this.start();
}

Server class:

public class Server {

private static final int TCP_PORT = 9000;

public static void main(String[] args) {

    ServerSocket ss = null;
    Socket socket = null;

    try {

        File file = new File("server/online.txt"); 
        file.delete();

        ss = new ServerSocket(TCP_PORT);
        while(true) {
            socket = ss.accept();
            new ServerThread(socket);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}
szeljic
  • 183
  • 8
  • Could you add the definition of the `OutputStream os` variable seen in the Server side code as well? – Some guy Jul 21 '15 at 15:33
  • I added part of ServerThread class where I defined OutputStream os – szeljic Jul 21 '15 at 15:38
  • `is.read(toRecieve, 0, toRecieve.length)` there is not guarantee that `toRecieve.length` bytes will be read. – Titus Jul 21 '15 at 15:39
  • Size of file is less than 1kB – szeljic Jul 21 '15 at 15:41
  • What I am interested to know is where you are defining the client list, that is, where you are specifying to the server to send the files to the respective clients. – Some guy Jul 21 '15 at 16:00
  • Every client gets his own thread on server, and from that tread client need to receives file – szeljic Jul 21 '15 at 16:08
  • Just working on a hunch here, could you add a SysOut statement in your `while(true)` loop in the server side code, and see if the client's context is immediately removed from the main thread and that it proceeds to serve the next as and when a client connects? – Some guy Jul 21 '15 at 16:33
  • I can achieve communication between clients via the server, that is not a problem. It could be related to this: http://stackoverflow.com/questions/5137212/does-javas-tcp-socket-class-block-when-sending-data?lq=1 Maybe I should use NIO for transfer files... – szeljic Jul 21 '15 at 16:40

0 Answers0