0

I'm trying to make a video file transfer but am having problems getting the server to start sending bytes.

The first step is for the client to connect, the socket gets accepted. Then the client sends the video file name but the server never reads this.

This is the code for the server up until it blocks:

 try(ServerSocket serverSocket = new ServerSocket(4005))
    {
        Socket socket = serverSocket.accept();
        System.out.println("accepted");
        OutputStream os = socket.getOutputStream();

        BufferedReader receiveReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        System.out.println("This gets printed");
        String request = receiveReader.readLine();//never passes this line
        System.out.println("This doesn't get printed");

and this is the client up until it blocks waiting for the server to send the video bytes:

 try(Socket socket = new Socket(IPAddress, 4005))
            {
                byte[] messageBytes = new byte[10000];

                DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
                outputStream.writeBytes("REQUEST;"+videoPath);//This is the line that should send the bytes for the server to read, so it won't block.

                String home = System.getProperty("user.home");
                String path = home+"\\Downloads" + videoName; 
                path = path.trim();
                FileOutputStream fos = new FileOutputStream(path);
                BufferedOutputStream bos = new BufferedOutputStream(fos);
                InputStream is = socket.getInputStream();

                int bytesRead = 0;
                System.out.println("Downloading file...");
                while((bytesRead = is.read(messageBytes))!=-1)//This blocks here

Why on earth isn't the server reading the "Request" + videoPath bytes that the server is sending? I tried outputStream.flush() as well, no luck.

Aequitas
  • 2,205
  • 1
  • 25
  • 51

1 Answers1

2

Usual problem. You're reading a line but you aren't writing a line. Add a line terminator to the sent message.

When you fix this you will then discover that you can't mix buffered streams and readers on the same socket. I suggest you do all the I/O via the DataInput/OutputStream classes, using read/writeUTF() for the name.

If you're sending multiple files see my answer there.

Community
  • 1
  • 1
user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thanks for your answer, do you mean `"REQUEST;"+videoPath` should be `"REQUEST;"+videoPath + "\n"` I guess that makes sense. I don't quite understand what you mean in your 2nd paragraph either. Are you saying I can't use FileOutputStream after using DataOutputStream? – Aequitas May 11 '16 at 00:15
  • I said *buffered.* You're using a `BufferedReader`. You can't use a buffered reader and then use the underlying stream directly. – user207421 May 11 '16 at 00:20
  • i'm sorry but I don't understand how to do it then. The new line worked as you suggested, but now when I send the video the file won't play on the receiving computer side. I imagine this may be due to the problem you alluded to? That example you linked to uses data input streams from buffered streams – Aequitas May 12 '16 at 01:52
  • I got it to work thanks, I changed the os/is to be like your example. – Aequitas May 12 '16 at 03:10