2

I am using linux and I have the server that receives an Input Stream from a client and returns only the file the client asks for from the browser.

It compiles perfectly but it returns nothing, i list all the steps i follow at the end to run the server and get the file.

The Server

import java.io.*;
import java.net.*;

public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = null;

        try {
            serverSocket = new ServerSocket(9999);
        } catch (IOException ex) {
            System.out.println("Can't setup server on this port number. ");
        }

        Socket socket = null;
        InputStream in = null;
        OutputStream out = null;

        try {
            socket = serverSocket.accept();
        } catch (IOException ex) {
            System.out.println("Can't accept client connection. ");
        }

        try {
            in = socket.getInputStream();
        } catch (IOException ex) {
            System.out.println("Can't get socket input stream. ");
        }

        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String line = br.readLine();
            System.out.println("File name = " + line);

            //Deixem nomes el nom del File o fitxer
            String fileName = line.replace("GET /", "");
            fileName = fileName.replace(" HTTP/1.1", "");
            System.out.println("File name is" + fileName);          

            out = new FileOutputStream(fileName);
        } catch (FileNotFoundException ex) {
            System.out.println("File not found. ");
        }

        byte[] bytes = new byte[16*1024];

        int count;
        while ((count = in.read(bytes)) > 0) {
            out.write(bytes, 0, count);
            System.out.println("Bytees "+count);
        }

        out.close();
        in.close();
        socket.close();
        serverSocket.close();
    }
}

As you can see I am using a method replace() to leave only the fileName. The client will be the browser who will ask for a file for example:

  • Client wants a file hello.txt localhost:9999/hello.txt
  • The server is in the folder java/bin with name Server.class
  • The file hello.txt is in the java/files

This how I execute my server from console:

  • cd java/files
  • java -cp ../bin Server [-p 9999]

But i dont receive Any file why? Am i doing something wrong??

  • 3
    Well you haven't written any HTTP headers in the response, for a start. I would strongly recommend against trying to write your own HTTP server, when there are so many that already exist... if you want to write code to respond however you want, put that within the context of an existing HTTP server, e.g. as a servlet in Tomcat. (I'd also note that the code you've written is going to be a big security issue... you really don't want a client to be able to ask for absolutely *any* file on the server). – Jon Skeet Mar 06 '16 at 12:13

1 Answers1

0

Firstly, Follow advice by Jon Skeet

Secondly, if you are learning then follow this
writing and reading using socket

Problem is that you try to write not to socket out stream but to file and of course if you want to do http server. you can check this simple http server question

other problems: you have to read your file for streaming it, it would be better to write responce headers. your server will handle only one request which is not right.You should ignore path and allow only specific formats to be downloaded. your try catch blocks misused.

Community
  • 1
  • 1
qwr
  • 3,660
  • 17
  • 29
  • down voter can you inform what you dislike in the answer? – qwr Mar 06 '16 at 15:31
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/11526425) – spongebob Mar 06 '16 at 17:24
  • @FrancescoMenzani If you have read all you can see that I mentioned the main problem. stating "Promblem is.." And adding links were better choice instead of rewriting of all his code that have many other errs not related even to question. But I can agree it would be better to write it as comment – qwr Mar 06 '16 at 20:05