patternjavaMinor
Loop for downloading file from the server (in client-server application)
Viewed 0 times
filetheapplicationdownloadingloopclientforserverfrom
Problem
In my client server application, client sends some commands and the server gives the results back.
Now my special concern is about the looping for downloading file from the server class, that is downloaded by using the
My question:
In my ClientSide there is a while loop for read/downlaod the named file. So how does the loop make sure that the client uses the size sent from the server, to control the receving of file contents? Am I doing that what I am supposed to do in this looping?
This is the complete code:
ClientSide:
```
package clientside;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class ClientSide {
private static Socket socket;
private static PrintWriter outputToServer;
private static BufferedReader inputFromServer;
private static InputStream is;
private static FileOutputStream fos;
private static final int PORT = 8000;
private static final String SERVER = "85.197.159.45";
boolean Connected;
DataInputStream serverInput;
public static void main(String[] args) throws InterruptedException {
String server = "localhost";
int port = PORT;
if (args.length >= 1) {
server = args[0];
}
if (args.length >= 2) {
port = Integer.parseInt(args[1]);
}
new ClientSide(server, port);
}
public ClientSide(String server, int port) {
try {
socket = new Socket(server, port);
serverInput = new DataInputStream(socket.getInputStream());
outputToServer = new PrintWriter(socket.getOutputStream(), true);
inputFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("Client is connected! ");
Connected = true;
String line = null;
Scanner sc = new Scanner(System.in);
System.out.print("Type command: ");
while (sc.hasNextLine()) {
Now my special concern is about the looping for downloading file from the server class, that is downloaded by using the
GET filename command.My question:
In my ClientSide there is a while loop for read/downlaod the named file. So how does the loop make sure that the client uses the size sent from the server, to control the receving of file contents? Am I doing that what I am supposed to do in this looping?
This is the complete code:
ClientSide:
```
package clientside;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class ClientSide {
private static Socket socket;
private static PrintWriter outputToServer;
private static BufferedReader inputFromServer;
private static InputStream is;
private static FileOutputStream fos;
private static final int PORT = 8000;
private static final String SERVER = "85.197.159.45";
boolean Connected;
DataInputStream serverInput;
public static void main(String[] args) throws InterruptedException {
String server = "localhost";
int port = PORT;
if (args.length >= 1) {
server = args[0];
}
if (args.length >= 2) {
port = Integer.parseInt(args[1]);
}
new ClientSide(server, port);
}
public ClientSide(String server, int port) {
try {
socket = new Socket(server, port);
serverInput = new DataInputStream(socket.getInputStream());
outputToServer = new PrintWriter(socket.getOutputStream(), true);
inputFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("Client is connected! ");
Connected = true;
String line = null;
Scanner sc = new Scanner(System.in);
System.out.print("Type command: ");
while (sc.hasNextLine()) {
Solution
The loop that you asked for specifically and marked in your post with a comment,
seems fine: it reads everything from the input stream and writes everything to the output stream.
Nothing really suspicious there to me.
But you have a couple of much bigger problems in this code.
A couple of pointers to get you started:
-
Review all your static variables. Take a hard look and check if they really need to be static (hint: most of them don't)
-
Try to make the content of all the try blocks smaller. Don't cover large blocks of code with a single catch. When many things can go wrong at many points, it's impossible to know the failure point, and it's impossible to recover gracefully. Try to handle each failure point individually.
-
Reorganize the code to not exit from anywhere other than the main method. Code with exit statements in constructors or non-toplevel methods is a very bad sign.
-
Avoid unnecessary operations. For example, the file exists method will iterate over all items, even after it already found a match. A better way is to return true immediately once you found a match. If the loop reaches the end, return false. No need for a boolean state variable.
-
The convention is to use
seems fine: it reads everything from the input stream and writes everything to the output stream.
Nothing really suspicious there to me.
But you have a couple of much bigger problems in this code.
A couple of pointers to get you started:
-
Review all your static variables. Take a hard look and check if they really need to be static (hint: most of them don't)
-
Try to make the content of all the try blocks smaller. Don't cover large blocks of code with a single catch. When many things can go wrong at many points, it's impossible to know the failure point, and it's impossible to recover gracefully. Try to handle each failure point individually.
-
Reorganize the code to not exit from anywhere other than the main method. Code with exit statements in constructors or non-toplevel methods is a very bad sign.
-
Avoid unnecessary operations. For example, the file exists method will iterate over all items, even after it already found a match. A better way is to return true immediately once you found a match. If the loop reaches the end, return false. No need for a boolean state variable.
-
The convention is to use
camelCase for variable names, which the "Connected" variable violates.Context
StackExchange Code Review Q#91308, answer score: 3
Revisions (0)
No revisions yet.