Xelian
Xelian

Reputation: 17228

Client can't receive info from Server

I am trying to send file info from server to client, but can't receive the info on the client side. I think it's an error with streams but can't find it.

I debug the code and I see that the info is reading well but then something bad happend when comes to the line :

while ((readLine = read.readLine()) != null) {

It's null and everything ends.

public class Server {
    private String urlFile = "http://riemann.fmi.uni-ofia.bg/vladov/students/boil.txt";
    private ServerSocket serverSocket = null;
    private BufferedReader read = null;
    private BufferedWriter write = null;
    private FileReader fileReader = null;
    URLConnection urlConnection = null;

    void acceptConnection() {
        try {
            serverSocket = new ServerSocket(3000);
            Socket client = null;
            while (true) {
                client = serverSocket.accept();
                handleConnection(client);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void handleConnection(Socket clientSocket) {
        try {
            URL url = new URL(urlFile);
            urlConnection = url.openConnection();
            read = new BufferedReader(new InputStreamReader(
                    urlConnection.getInputStream()));

            write = new BufferedWriter(new OutputStreamWriter(
                    clientSocket.getOutputStream()));
            String readLine = null;
            while ((readLine = read.readLine()) != null) {
                write.write(readLine);
                write.flush();
            }

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void tearDownConnection() {
        try {
            write.close();
            read.close();
            serverSocket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

Client:

 public class Client {
    Socket client = null;
    BufferedReader reader = null;
    BufferedWriter writer = null;

    void connectToServer(String hostAddress, int port) {
        try {
            System.out.println("Client is waitting.");
            client = new Socket(hostAddress, port);
            reader = new BufferedReader(new InputStreamReader(
                    client.getInputStream()));
            writer = new BufferedWriter(new OutputStreamWriter(
                    client.getOutputStream()));
            String readedLine = null;
            readedLine = reader.toString();
            while ((readedLine = reader.readLine()) != null) {
                System.out.println(readedLine);
            }

        } catch (UnknownHostException e) {
            System.out.println("Host name is unkown.");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    void tearDownConnection() {
        try {
            if (client != null) {
                client.close();
            }
            if (writer != null) {
                writer.close();
            }
            if (reader != null) {
                reader.close();
            }

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

    }
}

Upvotes: 4

Views: 602

Answers (1)

user1885297
user1885297

Reputation: 586

The client is waiting for a line (ie. terminated by a return/newline character) whereas server is not sending this. You could add the newline yourself in Server:

while ((readLine = read.readLine()) != null) {
  write.write(readLine+"\n");
  write.flush();
}

Upvotes: 3

Related Questions