nets.java

来自「java编程开发技巧与实例的编译测试通过的所有例程」· Java 代码 · 共 117 行

JAVA
117
字号
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 MumServers;
	public static void main(String args[]) throws IOException
	{
		System.out.println("Server coming up . . . ");
		try
		{
			SS	=	new ServerSocket(1237);
		}
		catch	(IOException eos)
		{
			System.out.println("Error opening server 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();
		}
	}
	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 getting information from client. Exiting");
			System.exit(1);
		}
		try
		{
			NumRows	=	datain.read();
		}
		catch	(IOException e)
		{
			System.out.println("I/O error while getting info from client. Exiting.");
			System.exit(1);
		}
		A	=	new int[NumRows][TotNum];
		B	=	new int[TotNum][TotNum];
		Result	=	new int[NumRows][TotNum];
		System.out.println("Receiving Matrix A from client.");
		for (i = 0; i < NumRows; i ++)
			for (j = 0; j < TotNum; j ++)
				{
					try
					{	A[i][j] = datain.readInt();	}
					catch	(IOException e)
					{
						System.out.println("I/O error while getting info from client. Exiting");
						System.exit(1);
					}
					System.out.println("receiving matrix B from client.");
				}
		for (i = 0; i < TotNum; i ++)
			for (j = 0; j < TotNum; j++)
			{
				try
				{	B[i][j]	=	datain.readInt();	}
				catch	(IOException e)
				{
					System.out.println("I/O error while getting info from client. Exiting");
					System.exit(1);
				}
			}
		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 while writing to client. Exiting");
					System.exit(1);
				}
			}
		dataout.flush();
		System.out.println("Resultant Matrix sent to client.");
	}
}

⌨️ 快捷键说明

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