Matrix Multiplication Client/Server

Write a client/server program whereby the server accepts commands from the client and does the computation as requested.
Specifically, the client will send to the server two integers along with one of the following operations: +, -, *, and /.
The server will response with the result and the client will print the result on client’s screen. In the meantime, the server will also print the same result on its screen.
You can use either TCP or UDP to exchange the messages. Of course your client program should prompt the user for entering the two operands, an operator, and the IP address of the server.
Available programming languages and operating systems: C, C++ or Java language over Windows OS or Linux/UNIX OS.

Code : TCPServerMatrix.java

import java.io.*;
import java.net.*;
//Udin Harun – M9607801
//TCPServerMatrix.java
public class TCPServerMatrix {
public static void main (String argv[]) throws Exception
{

String clientInput1;
String clientInput2;
String clientInput3;
String clientInput4;
String clientInput;
//String clientOperator;
String serverResult;
ServerSocket welcomeSocket = new ServerSocket(6769);
while(true) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader (new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());

clientInput1 = inFromClient.readLine();
int rowsMatrixA = Integer.parseInt(clientInput1);

clientInput2 = inFromClient.readLine();
int colsMatrixA = Integer.parseInt(clientInput2);

clientInput3 = inFromClient.readLine();
int rowsMatrixB = Integer.parseInt(clientInput3);

clientInput4 = inFromClient.readLine();
int colsMatrixB = Integer.parseInt(clientInput4);

if (colsMatrixA == rowsMatrixB)
{

// To Create matrices
double[][] matrixA = new double[rowsMatrixA][colsMatrixA];
double[][] matrixB = new double[rowsMatrixB][colsMatrixB];
double[][] matrixC = new double[rowsMatrixA][colsMatrixB];

// Get input from client To fill Matrices
// MatrixA
for (int rowA=0;rowA<rowsMatrixA;rowA++) {
for (int colA=0;colA<colsMatrixA;colA++){
//System.out.println(“Insert Element Matrix A ” + “[” + rowA + “]” + “[” + colA + “]” + “: \n”);
//System.out.println(‘rowA’ ‘colA’);
clientInput = inFromClient.readLine();
//outToServer.writeBytes(input + ‘\n’);
matrixA[rowA][colA] = Integer.parseInt(clientInput);
}
}

// MatrixB
for (int rowB=0;rowB<rowsMatrixB;rowB++) {
for (int colB=0;colB<colsMatrixB;colB++){
//System.out.println(“Insert Element Matrix B ” + “[” + rowB + “]” + “[” + colB + “]” + “: \n”);
clientInput = inFromClient.readLine();
matrixB[rowB][colB] = Integer.parseInt(clientInput);
}
}

// Multiplication C =A.B
for(int rowA=0; rowA<rowsMatrixA; rowA++){
for(int colB=0; colB<colsMatrixB; colB++){
for(int element=0; element<colsMatrixA; element++){
matrixC[rowA][colB] += matrixA[rowA][element]*matrixB[element][colB];
}
}
}

// To output matrixC (C = A.B)
System.out.println(“on Server’s Screen :” + ‘\n’);
System.out.println(“Matrix C = AxB” + ‘\n’);
for(int rowC=0; rowC<rowsMatrixA; rowC++){
for(int colC=0; colC<colsMatrixB; colC++){
//System.out.print(matrixC[rowC][colC]+”\t”);
serverResult = matrixC[rowC][colC]+”\t”;
outToClient.writeBytes(serverResult);
System.out.print(serverResult);

}
System.out.println(” “);
serverResult = (‘\n’ + ” “);
outToClient.writeBytes(serverResult);
}

//System.exit(0);
} //close if
else
{
System.out.println(“on Server’s Screen : Wrong Matrix”);
serverResult = (“Wrong Matrix” + ‘\n’);
outToClient.writeBytes(serverResult);
}
}
}
}

Code : TCPClientMatrix.java

import java.io.*;
import java.net.*;
//Udin Harun – M9607801
//TCPClientMatrix.java
public class TCPClientMatrix {
public static void main (String argv[]) throws Exception
{
String input1;
String input2;
String input3;
String input4;
String result;
String server;

BufferedReader inFromUser = new BufferedReader (new InputStreamReader (System.in));

System.out.println(“Enter IP Server : \n”);
server = inFromUser.readLine();

Socket clientSocket = new Socket(server,6769);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

System.out.println( ” Input Rows Matrix A :  \n” );
input1   =   inFromUser.readLine();             //get integer rowsMatrixA
outToServer.writeBytes(input1 + ‘\n’);
int rowsMatrixA    =   Integer.parseInt( input1 );

System.out.println( ” Input Cols Matrix A : \n” );
input2   =   inFromUser.readLine();             //get integer colsMatrixA
outToServer.writeBytes(input2 + ‘\n’);
int colsMatrixA    =   Integer.parseInt( input2 );

System.out.println( ” Input Rows Matrix B : \n” );
input3   =   inFromUser.readLine();             //get integer rowsMatrixB
outToServer.writeBytes(input3 + ‘\n’);
int rowsMatrixB    =   Integer.parseInt( input3 );

System.out.println( ” Input Cols Matrix B : \n” );
input4   =   inFromUser.readLine();             //get integer colsMatrixB
outToServer.writeBytes(input4 + ‘\n’);
int colsMatrixB    =   Integer.parseInt( input4 );

if (colsMatrixA == rowsMatrixB)
{

// To fill Matrices
// MatrixA
for (int rowA=0;rowA<rowsMatrixA;rowA++) {
for (int colA=0;colA<colsMatrixA;colA++){
System.out.println(“Insert Element Matrix A ” + “[” + rowA + “]” + “[” + colA + “]” + “: \n”);
String input = inFromUser.readLine();
outToServer.writeBytes(input + ‘\n’);
}
}

// MatrixB
for (int rowB=0;rowB<rowsMatrixB;rowB++) {
for (int colB=0;colB<colsMatrixB;colB++){
System.out.println(“Insert Element Matrix B ” + “[” + rowB + “]” + “[” + colB + “]” + “: \n”);
String input = inFromUser.readLine();
outToServer.writeBytes(input + ‘\n’);
}
}

// To output matrixC (C = A.B)
System.out.println(“on Client’s Screen :”);
System.out.println(“Matrix C = AxB” + “\n”);
for(int rowC=0; rowC<rowsMatrixA; rowC++){
for(int colC=0; colC<colsMatrixB; colC++){
result = inFromServer.readLine();
System.out.println(result);

}
result = inFromServer.readLine();
System.out.println(result);
}

clientSocket.close();

} //close if

else
{
System.out.println(“on Client’s Screen :”);
result = inFromServer.readLine();
System.out.println(result);
}
}
}

Reference :

http://en.wikipedia.org/wiki/Matrix_multiplication

Udin Harun

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: