I am trying to write a client server program to handle multiple client requests using a server (Server1), which in turn, requests from another server (computation server) for computation.
The client asks for an expression in the form a1b1+a2b2+... where a1, b1 etc. are 2X2 matrices. The user then enters the expression, followed by the values of the 2X2 matrices. The client concatenates each value with ',' while appending each matrix with ';' and sends the resulting string to the server (Server1) .
The server (Server1), on receiving this expression, breaks it down into tokens(separated by '+') and forwards the tokens(values of a1 and b1) to another computation server. The computation server (1)receives the token from Server1, (2) extracts the 2X2 matrices and performs their matrix multiplication and (3) returns the result of matrix multiplication to Server1. Server1 then computes the sum of the values returned by the computation server and sends the final result back to the client.
For example if a user enters an expression like 'a1b1+a2b2' (no spaces), and values 1,1,1,1 (for matrix a1) etc., the client sends to Server1 a string in the form of '1,1,1,1;2,2,2,2;+3,3,3,3;4,4,4,4;+'. Server1 sends 1,1,1,1;2,2,2,2; to the computation server. The computation server computes the matrix multiplication of {1,1,1,1} and {2,2,2,2} and returns the result {4,4,4,4} to Server1. Server1 again sends 3,3,3,3;4,4,4,4; to the computation server and so on. Finally Server1 returns the result of the expression {28,28,28,28} to the client.
My code is as follows:
TCPClient.java
import java.io.*;
import java.net.*;
public class TCPClient{
public static void main(String [] args) throws Exception{
Socket s = new Socket("127.0.0.1",5555);
DataOutputStream out = new DataOutputStream(s.getOutputStream());
System.out.print("Enter an expression of the form a1b1+a2b2... : ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String exp = br.readLine();
String exp2 = "";
int terms = exp.length() - exp.replace("+", "").length() + 1;
int i = 1;
String input;
do{
System.out.print("\nEnter value for element 00 of a" + i + ": ");
input = br.readLine();
exp2 += input + ",";
System.out.print("Enter value for element 01 of a" + i + ": ");
input = br.readLine();
exp2 += input + ",";
System.out.print("Enter value for element 10 of a" + i + ": ");
input = br.readLine();
exp2 += input + ",";
System.out.print("Enter value for element 11 of a" + i + ": ");
input = br.readLine();
exp2 += input + ";";
System.out.print("\nEnter value for element 00 of b" + i + ": ");
input = br.readLine();
exp2 += input + ",";
System.out.print("Enter value for element 01 of b" + i + ": ");
input = br.readLine();
exp2 += input + ",";
System.out.print("Enter value for element 10 of b" + i + ": ");
input = br.readLine();
exp2 += input + ",";
System.out.print("Enter value for element 11 of b" + i + ": ");
input = br.readLine();
exp2 += input + ";+";
i++;
}while(i <= terms);
System.out.println("\nExpression sent to server = "+exp2);
out.writeUTF(exp2);
DataInputStream in = new DataInputStream(s.getInputStream());
String result = in.readUTF();
System.out.println("\nResult of expression is "+ result);
s.close();
}
}
TCPServer1.java
import java.io.*;
import java.net.*;
public class TCPServer1{
public static void main(String [] args) throws Exception{
ServerSocket ss = new ServerSocket(5555);
System.out.println("Server started...");
while(true){
Socket s = ss.accept();
Connection c = new Connection(s);
}
}
}
class Connection extends Thread{
Socket c;
DataInputStream in;
DataOutputStream out;
public Connection(Socket c) throws Exception{
this.c = c;
in = new DataInputStream(c.getInputStream());
out = new DataOutputStream(c.getOutputStream());
this.start();
}
public void run(){
try{
String data = new String();
data = in.readUTF();
System.out.println("Received from client: " + data);
data = data.substring(0, data.length()-1);
System.out.println("data: " + data);
String[] tokens = data.split("\\+");
System.out.println("tokens[0]: " + tokens[0]);
int[][] finalsum = new int [2][2];
Socket s = new Socket("127.0.0.1",6666);
DataOutputStream out2 = new DataOutputStream(s.getOutputStream());
int numtokens = tokens.length;
int i = 0;
while (i < numtokens) {
System.out.println("Writing to CM: " + tokens[i]);
out2.writeUTF(tokens[i]);
DataInputStream in2 = new DataInputStream(s.getInputStream());
String matmul = in2.readUTF();
System.out.println("Received from Computation Machine: " + matmul);
findSum(finalsum, matmul);
System.out.println("Finalsum intermediate: " + finalsum[0][0] + " " + finalsum[0][1] + " " + finalsum[1][0] + " " + finalsum[1][1]);
i++;
}
String finalres = String.valueOf(finalsum[0][0]) + "," +String.valueOf(finalsum[0][1]) + ",";
finalres += String.valueOf(finalsum[1][0]) + "," + String.valueOf(finalsum[1][1]);
System.out.print("finalres to be sent to client: " + finalres);
out.writeUTF(finalres);
}catch(Exception e){
e.printStackTrace();
}finally{
try{
c.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void findSum(int [][] finalsum, String matmul) {
int [][] arr = new int [2][2];
String[] tokens = matmul.split(",");
arr[0][0] = Integer.parseInt(tokens[0]);
arr[0][1] = Integer.parseInt(tokens[1]);
arr[1][0] = Integer.parseInt(tokens[2]);
arr[1][1] = Integer.parseInt(tokens[3]);
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
finalsum[i][j] += arr[i][j];
}
}
ComputationServer.java
import java.io.*;
import java.net.*;
public class ComputationServer {
public static void main(String [] args) throws Exception{
ServerSocket ss = new ServerSocket(6666);
System.out.println("Computation Server started...");
int count = 1;
Socket s = ss.accept();
DataInputStream in;
DataOutputStream out;
in = new DataInputStream(s.getInputStream());
out = new DataOutputStream(s.getOutputStream());
while(true){
System.out.println("Entered CM " + (count++) + " times");
String data = in.readUTF();
System.out.println("Received from server: " + data);
String[] tokens = data.split(";");
System.out.println("tokens[0]: " + tokens[0]);
System.out.println("tokens[1]: " + tokens[1]);
String[] subtoken1 = tokens[0].split(",");
String[] subtoken2 = tokens[1].split(",");
int[][] first = new int [2][2];
int[][] second = new int [2][2];
first[0][0] = Integer.parseInt(subtoken1[0]);
first[0][1] = Integer.parseInt(subtoken1[1]);
first[1][0] = Integer.parseInt(subtoken1[2]);
first[1][1] = Integer.parseInt(subtoken1[3]);
second[0][0] = Integer.parseInt(subtoken2[0]);
second[0][1] = Integer.parseInt(subtoken2[1]);
second[1][0] = Integer.parseInt(subtoken2[2]);
second[1][1] = Integer.parseInt(subtoken2[3]);
int[][] res = new int [2][2];
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
for(int k = 0; k < 2; k++)
res[i][j] += first[i][k] * second[k][j];
String matmul = String.valueOf(res[0][0]) + "," +String.valueOf(res[0][1]) + ",";
matmul += String.valueOf(res[1][0]) + "," + String.valueOf(res[1][1]);
System.out.println("matmul: " + matmul);
out.writeUTF(matmul);
//out.flush();
}
}
}
For execution I start the Computation server first, then Server1 and then the client program.
The problem I'm facing is my program works fine for the first client, but if I try to compute this for the second client, the computation server doesn't accept further requests.
And if I change my computation server as follows, then it doesn't accept the second token from Server1 (for the first client).
ComputationServer.java
import java.io.*;
import java.net.*;
public class ComputationServer {
public static void main(String [] args) throws Exception{
ServerSocket ss = new ServerSocket(6666);
System.out.println("Computation Server started...");
int count = 1;
while(true){
System.out.println("Entered CM " + (count++) + " times");
Socket s = ss.accept();
DataInputStream in;
DataOutputStream out;
in = new DataInputStream(s.getInputStream());
out = new DataOutputStream(s.getOutputStream());
String data = in.readUTF();
System.out.println("Received from server: " + data);
//rest of the code same as above
Any help would be much appreciated. Thanks in advance!