-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
140 lines (109 loc) · 3.71 KB
/
Server.java
File metadata and controls
140 lines (109 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//package clientserver;
import java.io.*;
import java.util.*;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author msaqib
*/
class ServerThread extends Thread {
int resrow[];
int colno;
int rowno;
Server s;
Socket clientSocket;
public ServerThread(Server s, int rowno,int colno,Socket clientSocket) {
this.s = s;
this.colno = colno;
this.rowno = rowno;
this.clientSocket = clientSocket;
}
public void run() {
try {
this.s.sendtoClient(this.rowno,this.colno, clientSocket);
} catch (IOException ex) {
// Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public class Server {
public static int mat1[][];
public static int mat2[][];
public static int r1, c1, r2, c2;
public static int little2big(int i) {
return (i & 0xff) << 24 | (i & 0xff00) << 8 | (i & 0xff0000) >> 8 | (i >> 24) & 0xff;
}
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
Socket clientSocket = null;
serverSocket = new ServerSocket(1234); // 1234 is port number
System.out.println("Waiting for client at " + serverSocket);
clientSocket = serverSocket.accept(); // blocks and listen until a connection is made.
System.out.println("Client connected at" + clientSocket);
System.out.println("\nWaiting to receive data from client...\n");
DataInputStream ds = new DataInputStream(clientSocket.getInputStream());
/** receive dimesion*/
r1 = little2big(ds.readInt());
c1 = little2big(ds.readInt());
r2 = little2big(ds.readInt());
c2 = little2big(ds.readInt());
mat1 = new int[r1][c1];
mat2 = new int[r2][c2];
/** Receive data*/
int temprow[];
ServerThread t[][] = new ServerThread[r1][c2];
Server s = new Server();
for (int k = 0; k < r1; k++) {
temprow = new int[c1];
for (int i = 0; i < c1; i++) {
temprow[i] = little2big(ds.readInt());
}
for (int i = c1; i < c1 + r2 * c2; i++) {
mat2[(i - c1) / c2][(i - c1) % c2] = little2big(ds.readInt());
}
int r = little2big(ds.readInt());
mat1[r] = temprow;
System.out.println("\nReceived row " + (r + 1) + " of Matrix A\nReceived matrix B\n");
for (int i = 0; i < c2; i++) {
System.out.println("Creating thread " + (r+1)+","+(i + 1));
t[k][i] = new ServerThread(s,r,i,clientSocket);
t[k][i].start();
}
}
/**Blocking further access*/
try {
for(int i = 0; i < r1; i++)
for(int j=0;j<c2;j++)
t[i][j].join();
} catch (Exception e) {
} finally {
serverSocket.close();
clientSocket.close();
System.out.println("\nConnections Closed....!");
}
}
synchronized void sendtoClient(int rowno,int colno, Socket clientSocket) throws IOException {
DataOutputStream ds = new DataOutputStream(clientSocket.getOutputStream());
int res = 0;
System.out.println("\nStarting thread " +(rowno + 1)+","+(colno + 1));
System.out.println("Multiplying... Row "+(rowno + 1)+" of matrix A with ,"+"Column " + (colno + 1) + " of matrix B");
for (int j = 0; j < r2; j++) {
res += mat1[rowno][j] * mat2[j][colno];
}
ds.write(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(colno).array());
ds.write(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(res).array());
System.out.println("\nCalculated element[" +(rowno+1)+","+(colno + 1) + "] of final matrix sent to client...");
System.out.println("Thread " +(rowno + 1)+","+ (colno + 1) + " finished..");
}
}