3

It has been days I am struggling with this problem. I want to create a local android server to let other devices download a file in LAN. So far i have created a socket server that writes a pdf file along with header on output stream, but it is not working. When url is hit on web browser almost 95% of the data is downloaded without any problem after that download fails, it shows network problem(In Google chrome).

Following is the code to create server:

class VideoStreamServer {

    public void startServer() {
        outFilePath = getActivity().getExternalFilesDir("/") + "/pdf.pdf";
        outFile = new File(outFilePath);
        Runnable videoStreamTask = new Runnable() {
            @Override
            public void run() {
                try {
                    ServerSocket socket = new ServerSocket(port);


                    System.out.println("Waiting for client to connect.");
                    while (true) {
                        Socket client = socket.accept();

                        BufferedOutputStream os = new BufferedOutputStream(client.getOutputStream());
                        FileInputStream in = new FileInputStream(outFile);
                        BufferedInputStream inFromClient = new BufferedInputStream(client.getInputStream());

                        StringBuilder sb = new StringBuilder();
                        sb.append("HTTP/1.1 200 OK\r\n");
                        sb.append("Accept-Ranges: bytes\r\n");
                        sb.append("Connection: close\r\n");
                        sb.append("Content-Length: " + in.available() + "\r\n");
                        sb.append("Content-Disposition: attachment; filename=file.pdf\r\n");
                        sb.append("Content-Type: application/pdf \r\n");
                        sb.append("\r\n");


                        byte[] data = new byte[1024];
                        int length;
                        //inFromClient.read(data);
                        //System.out.println("request from client"+getStreamData(inFromClient));
                        System.out.println("Thread Started");
                        //System.setProperty("http.keepAlive", "false");
                        os.write(sb.toString().getBytes());
                        while ((length = in.read(data)) != -1) {
                            os.write(data, 0, length);
                        }
                        os.close();
                        client.close();
                        socket.close();
                        in.close();
                        break;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        };

        Thread streamServer = new Thread(videoStreamTask);
        streamServer.start();
    }

}

Any help would be appreciated.

EDIT1

 public String getStreamData(InputStream in) {
    StringBuffer buffer = new StringBuffer();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));

    String line;
    try {
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return buffer.toString();
}
Sachin Chandil
  • 17,133
  • 8
  • 47
  • 65
  • You already have a thread on the same problem.http://stackoverflow.com/questions/34728935/android-local-video-server?noredirect. You should have reported about that inputstream there. Again you do not specify errors. – greenapps Jan 14 '16 at 19:06
  • In that thread i was trying to stream video and i m struggling with that problem from days. then i thought i should start from a simple thing pdf(file) downloading. Still i am unable to make it work. i do not understand the input string you are taking about. I tried reading input stream also it did not work. I am just trying things around to solve my problem. If you know the solution please share it to me. it will be really grateful. i need help buddy. – Sachin Chandil Jan 14 '16 at 20:23
  • You did not report on 'inFromClient' then. Why not? Please report now and show full code. – greenapps Jan 14 '16 at 20:33
  • Well why are you still not posting the code for `getStreamDatta()` ? And a report about what happens? I gave you the hint that you first should read from the inputstream. Now do and solve your problem! – greenapps Jan 15 '16 at 09:39
  • Actually i am in office and don't have code here. i will post when i m at home. thanks for your attention. i will do that around 9:00 PM IST. Sorry for inconvenience. – Sachin Chandil Jan 15 '16 at 10:06
  • @greenapps i have updated my que. – Sachin Chandil Jan 16 '16 at 08:05
  • And again you just dump code here. You are not telling what it does. You are not telling the results. You are not even calling that function. – greenapps Jan 16 '16 at 12:31
  • i just wrote that function to test that fortunately it works. You are just looking for what i have done wrong. If i knew where to call this function i would have solved my issue. – Sachin Chandil Jan 16 '16 at 12:42
  • It looks as if you are calling it on the right place. So uncomment that line. Remove the two //. Then try again and tell the results. – greenapps Jan 16 '16 at 12:45
  • I just solved that problem. There was a problem that while reading input stream readLine() output should be checked if it is empty as well along with null. Thanks for your help. I have reached one milestone. Now if i m trying to stream video still same problem. I search for that then i came across that i need to set up resume support (`range` property). for that i need to read input stream to get `Range` property value but input stream is empty each time. – Sachin Chandil Jan 16 '16 at 13:16

0 Answers0