⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 netc.java

📁 关于并行计算服务器客户端与服务器端
💻 JAVA
字号:
//package Matrix;

import java.io.*;
import java.net.*;

import javax.swing.*;

public class Netc
      implements Runnable {
    static Socket sock[]; //定义一个socket数组。
    static InetAddress Serveraddr[]; //定义一个数组用来存储所有servers的ip地址。
    static DataInputStream datain[]; //定义一个输入流数组。
    static DataOutputStream dataout[]; //定义一个输出流数组。
    static int NumServers; //存储服务器的数目。
    static JTextArea consoleOutput; //显示程序运行时的提示信息。

    private Thread runner;

    public void run() {
        try {
            init();
        }
        catch (IOException ex) {
            consoleOutput.append("\n 有IO异常抛出");
        }
    }

    public void start() {
        runner = new Thread(this);
        runner.start();
    }

    public void init() throws IOException {
        int i;
        DataInputStream ServerConfigFile; /*用来读取服务器配置文件srvcfg.txt*/
        String IntString = null, Servernames[];
        ServerConfigFile = new DataInputStream(new FileInputStream("srvcfg.txt"));

        try {
            IntString = ServerConfigFile.readLine();
        }
        catch (IOException ioe) {
            consoleOutput.append("\n Error reading the # servers");
            System.exit(1);
        }
        try {
            NumServers = Integer.parseInt(IntString); //把字符串转化为整数。
        }
        catch (NumberFormatException nfe) {
            consoleOutput.append("\n  r servers is not an interger.");
            System.exit(1);
        }
        //现在我们知道了服务器的数目,可以实例化数组了。
        Servernames = new String[NumServers];
        sock = new Socket[NumServers];
        Serveraddr = new InetAddress[NumServers];
        datain = new DataInputStream[NumServers];
        dataout = new DataOutputStream[NumServers];

        for (i = 0; i < NumServers; i++) {
            try {
                Servernames[i] = ServerConfigFile.readLine();
            }
            catch (IOException e) {
                consoleOutput.append("\n Error reading server names");
                System.exit(1);
            }
            Servernames[i] = Servernames[i].trim();
        }

        try {
            ServerConfigFile.close();
        }
        catch (IOException e) {
            consoleOutput.append("\n Error reading server names");
            System.exit(1);
        }

        //打开对server的socket,并设置流。
        try {
            for (i = 0; i < NumServers; i++) { //得到IP地址。
                Serveraddr[i] = InetAddress.getByName(Servernames[i]);
                sock[i] = new Socket(Serveraddr[i], 1237);
                datain[i] = new DataInputStream(
                      new BufferedInputStream(sock[i].getInputStream()));
                dataout[i] = new DataOutputStream(
                      new BufferedOutputStream(sock[i].getOutputStream()));
            }
        }
        catch (IOException E) {
            consoleOutput.append("\n I/O Error openning stream sockets.");
            System.exit(1);
        }

        //调用ClientBody过程来解决问题。
        ClientBody();

        try { //关闭流和sockets。
            for (i = 0; i < NumServers; i++) {
                dataout[i].close();
                datain[i].close();
                sock[i].close();
            }
        }
        catch (IOException E) {
            consoleOutput.append("\n error closing streams and Sockets");
            System.exit(1);
        }
    }

    public static void ClientBody() throws IOException {
        int i, j, k;
        int TotNum = 0; //矩阵(方阵)的维数。
        int NumRows = 0; //根据服务器的数目,对矩阵划分后的每个矩阵的行数。
        int A[][], B[][], C[][];

        DataInputStream ClientConfigFile;
        String IntString = null;
        ClientConfigFile = new DataInputStream(new FileInputStream("clicfg.txt"));

        try {
            IntString = ClientConfigFile.readLine();
        }
        catch (IOException ioe) {
            consoleOutput.append("\n error reading N from file.");
            System.exit(1);
        }

        try {
            TotNum = Integer.parseInt(IntString); //把字符串转换成整数。
        }
        catch (NumberFormatException nfe) {
            consoleOutput.append("\n the value for N is not an integer.");
            System.exit(1);
        }
 /*///////////////////////////////
        
        try{
        	consoleOutput.append(""+ClientConfigFile.read());
        	System.out.println(Integer.valueOf(Byte.valueOf(ClientConfigFile.readByte())));
        }
         catch (IOException ioe) {
            consoleOutput.append("\n error reading N from file.");
            System.exit(1);
        }

*/
        try {
            ClientConfigFile.close();
        }
        catch (IOException e) {
            consoleOutput.append("\n I/O error closing config file.");
            System.exit(1);
        }

        NumRows = TotNum / NumServers;

        A = new int[TotNum][TotNum];
        B = new int[TotNum][TotNum];
        C = new int[TotNum][TotNum];

        for (i = 0; i < TotNum; i++) { //给矩阵赋初值。
            for (j = 0; j < TotNum; j++) {
                A[i][j] = i;
                B[i][j] = j;
                C[i][j] = 0;
            }
        }

        consoleOutput.append("\n Sending information to servers.");

        try {
            for (i = 0; i < NumServers; i++) {
                dataout[i].write(TotNum);
                dataout[i].write(NumRows);
                dataout[i].flush(); //给每个服务器依次传送方阵的维数和矩阵A行数。

                //下面两个循环分别传送每个服务器所需计算的矩阵。
                //对矩阵A按行划分为若干部分,每部分为NumRows行,最后一部分可能不足
                //NumRows行,则单独传送。
                for (j = NumRows * i; j < NumRows * (i + 1); j++) {
                    for (k = 0; k < TotNum; k++) {
                        dataout[i].writeInt(A[j][k]);
                    }
                }
                dataout[i].flush();

                //对矩阵B没有划分,对每个服务器传送整个矩阵B。
                for (j = 0; j < TotNum; j++) {
                    for (k = 0; k < TotNum; k++) {
                        dataout[i].writeInt(B[j][k]);
                    }
                }
                dataout[i].flush();
            }
        }
        catch (IOException ioe) {
            consoleOutput.append("\n I/O error reading matrix to server");
            System.exit(1);
        }

        try { //从每个服务器读取计算结果。
            for (i = 0; i < NumServers; i++) {
                for (j = NumRows * i; j < NumRows * (i + 1); j++) {
                    for (k = 0; k < TotNum; k++) {
                        C[j][k] = datain[i].readInt();
                    }
                }
            }
        }
        catch (IOException ioe) {
            consoleOutput.append("\n I/O error receiving result from server");
            System.exit(1);
        }
        consoleOutput.append("\n Resultant Matrix");
        consoleOutput.append("\n");

        for (i = 0; i < TotNum; i++) { //输出计算结果。
            for (j = 0; j < TotNum; j++) {
                consoleOutput.append(C[i][j] + " ");
            }
            consoleOutput.append("\n");
        }
    }

    public Netc(JTextArea console) {
        consoleOutput = console;
        start();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -