Hello everyone,
I have 4 files that execute server multithread. 1 for server, 1 for client, 1 for multithread and the last one for protocol to do calculations entered by user. My question is how can I terminate the thread started by the server? I have no idea of how to return "false" from the multithread to the server to close the serverSocket connection.
Below are the files..
SERVER...
import java.net.*;
import java.io.*;
public class Server
{
public static void main(String[] args) throws IOException
{
ServerSocket serverSocket = null;
boolean listening = true;
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
int Port;
System.out.print("Enter Port : ");
Port = Integer.parseInt(stdin.readLine());
try
{
serverSocket = new ServerSocket(Port);
}
catch (IOException e)
{
System.err.println("Could not listen on port: " + Port);
System.exit(-1);
}
while (listening)
{
new calcMultiServerThread(serverSocket.accept()).start();
}
serverSocket.close();
}
}
CLIENT
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) throws IOException {
Socket clSocket = null;
PrintWriter out = null;
BufferedReader in = null;
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
int Port;
String Hostname;
System.out.print("Enter Port : ");
Port = Integer.parseInt(stdin.readLine());
System.out.print("Enter Hostname : ");
Hostname = stdin.readLine();
try
{
clSocket = new Socket(Hostname, Port);
out = new PrintWriter(clSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clSocket.getInputStream()));
}
catch (UnknownHostException e)
{
System.err.println("Don't know about host: " + Hostname);
System.exit(1);
}
catch (IOException e)
{
System.err.println("Couldn't get I/O for the connection to: " + Hostname);
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;
while ((fromServer = in.readLine()) != null)
{
System.out.println("Server: " + fromServer);
if (fromServer.equals("BYE: OK"))
break;
fromUser = stdIn.readLine();
if (fromUser != null)
{
System.out.println("Client: " + fromUser);
out.println(fromUser);
}
}
out.close();
in.close();
stdIn.close();
clSocket.close();
}
}
MULTISERVERTHREAD
import java.net.*;
import java.io.*;
public class MultiServerThread extends Thread
{
private Socket socket = null;
public calcMultiServerThread(Socket socket)
{
super("calcMultiServerThread");
this.socket = socket;
}
public void run()
{
try
{
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String inputLine, outputLine;
calcProtocol protocol = new calcProtocol();
outputLine = protocol.processInput(null);
out.println(outputLine);
while ((inputLine = in.readLine()) != null)
{
outputLine = protocol.processInput(inputLine);
out.println(outputLine);
if (outputLine.equals("BYE: OK"))
{
break;
}
}
out.close();
in.close();
socket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
PROTOCOL
import java.net.*;
import java.io.*;
public class calcProtocol
{
private static final int WAITING = 0;
private static final int ASK = 1;
private static final int CALC = 2;
private static final int ANOTHER = 3;
private static final int first = 4;
private static final int second = 5;
private static final int third = 6;
private static final int result = 7;
private static final int MS = 8;
private int state = WAITING;
private String input1 = null;
private String input2 = null;
private double input3 = 0;
private double value = 0;
private String operator = null;
public String processInput(String theInput)
{
String theOutput = null;
if (state == WAITING)
{
theOutput = "Enter CALC, MS, MC, BYE or END";
state = ASK;
}
else if (state == ASK)
{
if (theInput.equalsIgnoreCase("CALC"))
{
theOutput = "CALC: OK";
state = CALC;
}
else if (theInput.equalsIgnoreCase("MS"))
{
theOutput = " ";
state = MS;
}
else if (theInput.equalsIgnoreCase("MC"))
{
input3 = 0;
theOutput = "CLEAR: OK" + " Continue? (y/n)";
state = ANOTHER;
}
else if (theInput.equalsIgnoreCase("BYE"))
{
theOutput = "BYE: OK";
state = WAITING;
}
else if (theInput.equalsIgnoreCase("END"))
{
theOutput = "END: OK";
state = WAITING;
}
else
{
theOutput = "Incorrect entry. Enter CALC, MS, MC, BYE or END";
}
}
else if (state == CALC)
{
if (theInput.equalsIgnoreCase("MR"))
{
input1 = Double.toString(input3);
theOutput = input1;
state = second;
}
else
{
input1 = theInput;
theOutput = input1;
state = second;
}
}
else if (state == second)
{
operator = theInput;
theOutput = operator;
state = third;
}
else if (state == third)
{
if (theInput.equalsIgnoreCase("MR"))
{
input2 = Double.toString(input3);
theOutput = input2;
state = result;
}
else
{
input2 = theInput;
theOutput = input2;
state = result;
}
}
else if (state == result)
{
if (operator.equals("+"))
{
value = Double.parseDouble(input1) + Double.parseDouble(input2);
theOutput = "= " + value + " Continue? (y/n)";
state = ANOTHER;
}
else if (operator.equals("-"))
{
value = Double.parseDouble(input1) - Double.parseDouble(input2);
theOutput = "= " + value + " Continue? (y/n)";
state = ANOTHER;
}
else if (operator.equals("*"))
{
value = Double.parseDouble(input1) * Double.parseDouble(input2);
theOutput = "= " + value + " Continue? (y/n)";
state = ANOTHER;
}
else if (operator.equals("/"))
{
value = Double.parseDouble(input1) / Double.parseDouble(input2);
theOutput = "= " + value + " Continue? (y/n)";
state = ANOTHER;
}
}
else if (state == MS)
{
input3 = Double.parseDouble(theInput);
theOutput = "Store: OK" + " Continue? (y/n)";
state = ANOTHER;
}
else if (state == ANOTHER)
{
if (theInput.equalsIgnoreCase("y"))
{
theOutput = "Enter CALC, MS, MC, BYE or END";
state = ASK;
}
else
{
theOutput = "BYE: OK";
state = WAITING;
}
}
return theOutput;
}
}
gavon,
MCP
I have 4 files that execute server multithread. 1 for server, 1 for client, 1 for multithread and the last one for protocol to do calculations entered by user. My question is how can I terminate the thread started by the server? I have no idea of how to return "false" from the multithread to the server to close the serverSocket connection.
Below are the files..
SERVER...
import java.net.*;
import java.io.*;
public class Server
{
public static void main(String[] args) throws IOException
{
ServerSocket serverSocket = null;
boolean listening = true;
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
int Port;
System.out.print("Enter Port : ");
Port = Integer.parseInt(stdin.readLine());
try
{
serverSocket = new ServerSocket(Port);
}
catch (IOException e)
{
System.err.println("Could not listen on port: " + Port);
System.exit(-1);
}
while (listening)
{
new calcMultiServerThread(serverSocket.accept()).start();
}
serverSocket.close();
}
}
CLIENT
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) throws IOException {
Socket clSocket = null;
PrintWriter out = null;
BufferedReader in = null;
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
int Port;
String Hostname;
System.out.print("Enter Port : ");
Port = Integer.parseInt(stdin.readLine());
System.out.print("Enter Hostname : ");
Hostname = stdin.readLine();
try
{
clSocket = new Socket(Hostname, Port);
out = new PrintWriter(clSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clSocket.getInputStream()));
}
catch (UnknownHostException e)
{
System.err.println("Don't know about host: " + Hostname);
System.exit(1);
}
catch (IOException e)
{
System.err.println("Couldn't get I/O for the connection to: " + Hostname);
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;
while ((fromServer = in.readLine()) != null)
{
System.out.println("Server: " + fromServer);
if (fromServer.equals("BYE: OK"))
break;
fromUser = stdIn.readLine();
if (fromUser != null)
{
System.out.println("Client: " + fromUser);
out.println(fromUser);
}
}
out.close();
in.close();
stdIn.close();
clSocket.close();
}
}
MULTISERVERTHREAD
import java.net.*;
import java.io.*;
public class MultiServerThread extends Thread
{
private Socket socket = null;
public calcMultiServerThread(Socket socket)
{
super("calcMultiServerThread");
this.socket = socket;
}
public void run()
{
try
{
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String inputLine, outputLine;
calcProtocol protocol = new calcProtocol();
outputLine = protocol.processInput(null);
out.println(outputLine);
while ((inputLine = in.readLine()) != null)
{
outputLine = protocol.processInput(inputLine);
out.println(outputLine);
if (outputLine.equals("BYE: OK"))
{
break;
}
}
out.close();
in.close();
socket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
PROTOCOL
import java.net.*;
import java.io.*;
public class calcProtocol
{
private static final int WAITING = 0;
private static final int ASK = 1;
private static final int CALC = 2;
private static final int ANOTHER = 3;
private static final int first = 4;
private static final int second = 5;
private static final int third = 6;
private static final int result = 7;
private static final int MS = 8;
private int state = WAITING;
private String input1 = null;
private String input2 = null;
private double input3 = 0;
private double value = 0;
private String operator = null;
public String processInput(String theInput)
{
String theOutput = null;
if (state == WAITING)
{
theOutput = "Enter CALC, MS, MC, BYE or END";
state = ASK;
}
else if (state == ASK)
{
if (theInput.equalsIgnoreCase("CALC"))
{
theOutput = "CALC: OK";
state = CALC;
}
else if (theInput.equalsIgnoreCase("MS"))
{
theOutput = " ";
state = MS;
}
else if (theInput.equalsIgnoreCase("MC"))
{
input3 = 0;
theOutput = "CLEAR: OK" + " Continue? (y/n)";
state = ANOTHER;
}
else if (theInput.equalsIgnoreCase("BYE"))
{
theOutput = "BYE: OK";
state = WAITING;
}
else if (theInput.equalsIgnoreCase("END"))
{
theOutput = "END: OK";
state = WAITING;
}
else
{
theOutput = "Incorrect entry. Enter CALC, MS, MC, BYE or END";
}
}
else if (state == CALC)
{
if (theInput.equalsIgnoreCase("MR"))
{
input1 = Double.toString(input3);
theOutput = input1;
state = second;
}
else
{
input1 = theInput;
theOutput = input1;
state = second;
}
}
else if (state == second)
{
operator = theInput;
theOutput = operator;
state = third;
}
else if (state == third)
{
if (theInput.equalsIgnoreCase("MR"))
{
input2 = Double.toString(input3);
theOutput = input2;
state = result;
}
else
{
input2 = theInput;
theOutput = input2;
state = result;
}
}
else if (state == result)
{
if (operator.equals("+"))
{
value = Double.parseDouble(input1) + Double.parseDouble(input2);
theOutput = "= " + value + " Continue? (y/n)";
state = ANOTHER;
}
else if (operator.equals("-"))
{
value = Double.parseDouble(input1) - Double.parseDouble(input2);
theOutput = "= " + value + " Continue? (y/n)";
state = ANOTHER;
}
else if (operator.equals("*"))
{
value = Double.parseDouble(input1) * Double.parseDouble(input2);
theOutput = "= " + value + " Continue? (y/n)";
state = ANOTHER;
}
else if (operator.equals("/"))
{
value = Double.parseDouble(input1) / Double.parseDouble(input2);
theOutput = "= " + value + " Continue? (y/n)";
state = ANOTHER;
}
}
else if (state == MS)
{
input3 = Double.parseDouble(theInput);
theOutput = "Store: OK" + " Continue? (y/n)";
state = ANOTHER;
}
else if (state == ANOTHER)
{
if (theInput.equalsIgnoreCase("y"))
{
theOutput = "Enter CALC, MS, MC, BYE or END";
state = ASK;
}
else
{
theOutput = "BYE: OK";
state = WAITING;
}
}
return theOutput;
}
}
gavon,
MCP