example9_7.java

来自「书中的例题」· Java 代码 · 共 311 行

JAVA
311
字号
/* 客户端程序 */
import java.net.*;
import java.io.*;
import java.awt.*;
import java.lang.*;

public class Netc
{
  static  sock[];//定义Socket数组
  static InetAddress Serveraddr[ ];//定义IP地址数组,存放所有服务器IP地址
  static DataInputStream datain[ ];//定义输入流数组
  static DataOutputStream dataout[ ];//定义输出流数组
  static int NumServers;

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

  try { IntString =ServerConfigFile.readLine();}
  catch  (IOException ioe)
      { 
        System.out.println("Error reading the # servers");
        System.exit(1);
      }
  try{
        NumServers =Integer.parseInt(IntString); 
     }
  catch (NumberFormatException nfe)
     {
        System.out.println("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)
       {  System.out.println("读取服务器名称错误");
          System.exit(1);
       }
      Servernames[i]=Servernames[i].trim();
    }
  try
    {
     ServerConfigFile.close();
    }
  catch(IOException e)
   {
       System.out.println("读取服务器名称错误");
       System.exit(1);
   } 
  //建立套接字对象和建立输入输出流
  try
    {
     for (i=0;i<NumServers;i++)
       {  //get IP address
         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)
   {
      System.out.println("I/O Error ,建立套接字连接不成功");
      System.exit(1);
   }

  ClientBody();
  try{
     for(i=0;i<NumServers;i++){
       dataout[i].close();
       datain[i].close();
       sock[i].close();
      };
    }
  catch(IOException E){
      System.out.println("关闭连接不成功");
      System.exit(1);
    }
 }

public static void ClientBody() throws IOException
 {
  int i,j,k;
  int TotNum=0,NumRows=0;
  int A[][],B[][],C[][];

  DataInputStream ClientConfigFile;
  String IntString =null;
  //读取客户机端的任务分配等技术参数配置文件
  ClientConfigFile=new DataInputStream(new FileInputStream("clicfg.txt"));
  //读取配置文件中矩阵N*N的参数N
  try{IntString =ClientConfigFile.readLine();}
  catch(IOException ioe)
    {
       System.out.println("读取配置文件中的参数N时发生错误");
       System.exit(1);
    }
  try{TotNum =Integer.parseInt(IntString); }
  catch(NumberFormatException nfe){
       System.out.println("配置文件中的参数N不是整型数值.");
       System.exit(1);
   }
 try{  ClientConfigFile.close();}
 catch(IOException e){
       System.out.println("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;
      }

 System.out.println("正在发送数据给servers....\n");
 try
  {
   for(i=0;i<NumServers;i++)
    {
     dataout[i].write(TotNum);
     dataout[i].write(NumRows);
     dataout[i].flush();

     for(j=NumRows*i;j<NumRows*(i+1);j++)
       for(k=0;k<TotNum;k++)
          dataout[i].writeInt(A[j][k]);
       dataout[i].flush();

     for(j=0;j<TotNum;j++)
		 for (k=0;k<TotNum;k++)
          dataout[i].writeInt(B[j][k]);
       dataout[i].flush();		 
    }
  }
 catch(IOException ioe){
    System.out.println("I/O error ,发送数据至server不成功");
    System.exit(1);
  }

 System.out.println("接收servers传回的数据...."); 
 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){
    System.out.println("I/O error 接收服务器端返回数据不成功");
    System.exit(1);
  }
 System.out.println();
 System.out.println("矩阵运算结果:");
 System.out.println();
 
 for(i=0;i<TotNum;i++)
   {
	 for(j=0;j<TotNum;j++)
        System.out.print(C[i][j]+"  ");
     System.out.println();
   }
 }
}



/* 服务器端程序 */
import java.net.*;
import java.io.*;
import java.awt.*;

public class Nets
{
 static Socket mySocket;  
 static ServerSocket SS;   
 
 static DataInputStream datain; 
 static DataOutputStream dataout;
 static int NumServers;

 public static void main(String args[]) throws IOException
 {
   System.out.println("服务器正在等待连接…..");

   try{
        SS =new ServerSocket(1237);
      }
   catch(IOException eos){
        System.out.println("打开服务器端socket错误。");
        System.exit(1);
      }
   while(true)
	 {
	  try{
           mySocket =SS.accept();
         }
      catch(IOException e)
         {
           System.out.println("I/O error waiting.Exiting.");
           System.exit(1);
         }
     
   datain=new DataInputStream(
          new BufferedInputStream(mySocket.getInputStream()));
   dataout =new DataOutputStream(
            new BufferedOutputStream(mySocket.getOutputStream()));
   ServerBody();
  }  //while_end
 }

 public static void ServerBody() throws IOException
{
 int i,j,k,sum;
 int TotNum =0,NumRows =0;
 int A[][],B[][],Result[][];
 try{
       TotNum = datain.read();
    }
 catch (IOException e){ 
       System.out.println("I/O error ,客户机已经断开连接");
       System.exit(1);
    }
 try{
        NumRows =datain.read();
    }
 catch(IOException e){
        System.out.println("I/O error ,客户机已经断开连接");
        System.exit(1);
    }

 //
 A=new int[NumRows][TotNum];
 B=new int[TotNum][TotNum];
 Result = new int [NumRows][TotNum];

 System.out.println("接收客户机发送来的矩阵A。");
 for(i=0;i<NumRows;i++)
  {
   for(j=0;j<TotNum;j++)
	 try{
        A[i][j] = datain.readInt();
		System.out.print( A[i][j]+"  ");
      }
     catch(IOException e){
       System.out.println("I/O error ,客户机已经断开连接");
       System.exit(1);
      }
    System.out.println( );
   }

 System.out.println("接收客户机发送来的矩阵B。");
 for(i=0;i<TotNum;i++)
   {
   for(j =0;j<TotNum;j++)
     try{
          B[i][j]= datain.readInt();
          System.out.print( B[i][j]+"  ");
        }
     catch(IOException e){
         System.out.println("I/O Error ,客户机已经断开连接");
         System.exit(1);
        }
    System.out.println( );
   }

 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;
    };
 for(i = 0;i<NumRows;i++)
 for(j=0;j<TotNum;j++)
    try{
        dataout.writeInt(Result[i][j]);
       }
	catch(IOException e){
        System.out.println("I/O error,客户机已经断开连接");
        System.exit(1);
       }
 dataout.flush();
 System.out.println("计算结果发回给客户机....");
}   //ServerBody()_end 

}

⌨️ 快捷键说明

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