📄 nets.java
字号:
//package Matrix;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class Nets
implements Runnable {
static Socket mySocket; //定义一个连接客户端的socket。
static ServerSocket SS; //定义一个Server Socket。
static DataInputStream datain; //定义一个输入流。
static DataOutputStream dataout; //定义一个输出流。
static int MumServers; //
static JTextArea consoleOutput; //显示程序运行时的提示信息。
private Thread runner;
public void run() {
try {
while (true) { //无限等待连接。
try {
mySocket = SS.accept(); //接受客户端连接。
}
catch (IOException e) {
consoleOutput.append("\n I/O error waiting. Exiting.");
System.exit(1);
}
//设置输入/输出流。
datain = new DataInputStream(
new BufferedInputStream(mySocket.getInputStream()));
dataout = new DataOutputStream(
new BufferedOutputStream(mySocket.getOutputStream()));
ServerBody(); //实际的计算代码。
}
}
catch (IOException e) {
consoleOutput.append("\n 有异常。");
}
}
public void start() {
runner = new Thread(this);
runner.start();
}
public void init() throws IOException {
consoleOutput.append("\n Server coming up ...");
try {
SS = new ServerSocket(1237);
}
catch (IOException eos) {
consoleOutput.append("\n Error opening server socket.");
System.exit(1);
}
start();
}
public static void ServerBody() throws IOException {
int i, j, k, sum;
int TotNum = 0; //方阵的维数。
int NumRows = 0; //矩阵A的行数。
int A[][], B[][], Result[][];
try {
TotNum = datain.read(); //读入方阵的维数。
}
catch (IOException e) {
consoleOutput.append(
"\n I/O error getting information from client.Exiting");
System.exit(1);
}
try {
NumRows = datain.read(); //读入矩阵A的行数。
}
catch (IOException e) {
consoleOutput.append(
"\n I/O error while getting info from client. Exiting.");
System.exit(1);
}
//现在知道了方阵的维数和矩阵A的行数,可以实际计算了。
A = new int[NumRows][TotNum];
B = new int[TotNum][TotNum];
Result = new int[NumRows][TotNum];
consoleOutput.append("\n Receiving Matrix A from client.\n");
//读入矩阵A的各元素的值。
for (i = 0; i < NumRows; i++) {
for (j = 0; j < TotNum; j++) {
try {
A[i][j] = datain.readInt();
consoleOutput.append(String.valueOf(A[i][j]) + " ");
}
catch (IOException e) {
consoleOutput.append(
"\n I/O error while getting info from client. Exiting");
System.exit(1);
}
}
consoleOutput.append("\n");
}
consoleOutput.append("\n receiving matrix B from client.\n");
//读入方阵B的各元素的值。
for (i = 0; i < TotNum; i++) {
for (j = 0; j < TotNum; j++) {
try {
B[i][j] = datain.readInt();
consoleOutput.append(String.valueOf(B[i][j]) + " ");
}
catch (IOException e) {
consoleOutput.append(
"\n I/O Error while getting info from client. Exiting");
System.exit(1);
}
}
consoleOutput.append("\n");
}
//计算两个矩阵相乘。
for (i = 0; i < NumRows; i++) {
for (j = 0; j < TotNum; j++) {
sum = 0;
for (k = 0; k < TotNum; k++) {
sum += A[i][k] * B[k][j];
}
Result[i][j] = sum;
}
}
//将计算结果返回客户端。
consoleOutput.append("\n Resultant Matrix \n");
for (i = 0; i < NumRows; i++) {
for (j = 0; j < TotNum; j++) {
try {
dataout.writeInt(Result[i][j]);
consoleOutput.append(String.valueOf(Result[i][j]) + " ");
}
catch (IOException e) {
consoleOutput.append(
"\n I/O error while writing to client. Exiting");
System.exit(1);
}
}
consoleOutput.append("\n");
}
dataout.flush();
consoleOutput.append("\n Resultant Matrix sent to client\n");
}
public Nets(JTextArea console) {
consoleOutput = console;
try {
init();
}
catch (IOException ex) {
consoleOutput.append("\n有异常发生.");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -