In my client server application, client is supposed to input some command in a special format given (i.e., vp(size, transport, days)
). Now if the client input wrong command in wrong formate, it is able to ask the user to input again (that's how I have set the logic in else
). But after that, once the client input again,the program can't read and send the command. Actually nothing happens when the user input the request again.
Please ask me to post the server side code if it's neede.
Client:
public class TCPClient {
static boolean run = true;
public static void main(String args[]) {
Socket client = null;
int portnum = 8080;
if (args.length >= 1) {
portnum = Integer.parseInt(args[0]);
}
new TCPClient(client, portnum);
}
public TCPClient(Socket client, int portNumber) {
try {
String answer = "";
String command = "";
// Creating client socket
client = new Socket(InetAddress.getLocalHost(), portNumber);
System.out.println("Client socket is made" + client);
// Creating output stream
OutputStream clientOut = client.getOutputStream();
PrintWriter pw = new PrintWriter(clientOut, true);
InputStream clientIn = client.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(clientIn));
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
while (run) {
System.out.println("Mention the Family Size (FZ), Means of Travel (MT), Number of Days (ND) in the given format below: "
+ "vp(FZ,MT,ND)");//example: vp(2,air,30,)
command = userInput.readLine().trim(); //sending a request to the server
pw.println(command);
answer = br.readLine(); //reading the response from the server
System.out.println("Server replied: " + answer);
if (answer != null && answer.startsWith("Price")) {
System.out.println("Write your Name (N), ID number (ID) and Phone Number (PN) in the format: "
+ "PersonDetalis(N,ID,PN)");//
} else {
System.out.println("Program initializing.. Try again!");
new TCPClient(client, portNumber);
}
command = userInput.readLine().trim();//sending request
pw.println(command);
System.out.println("Server replied: " + br.readLine());
run = false;
}
pw.close();
br.close();
client.close();
} catch (IOException ie) {
System.out.println("I/O error. Run the program again ");
}
}
}
I think the problem is in your server which is communicating only with the first established connection. Inside your infinite loop
...
} else {
System.out.println("Program initializing.. Try again!");
new TCPClient(client, portNumber);
}
...
each time you create a new connection (socket), which you must accept on server. I wrote my own simple server
ServerSocket serverSocket = new ServerSocket(8080);
while (true) {
Socket clientSocket = serverSocket.accept();
PrintWriter out =
new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
String line = in.readLine();
System.out.println("SERVER: received " + line);
out.println("server error");
out.close();
in.close();
clientSocket.close();
}
and it worked.
I suggest you to move you complex logic from TCPClient constructor, also stop creating new TCPClient each time after System.out.println("Program initializing.. Try again!");
and use the same connection instead.
But if you do so it wouldn't work with my server code example, because my server closes connection right after receiving something and sending back a reply.