server.java

来自「软件工程实践课程的答案哦」· Java 代码 · 共 894 行 · 第 1/2 页

JAVA
894
字号
/**This file describes the
 *class responsible for server function*/
import java.io.*;
import java.net.*;

public class Server extends Thread
{   
    /**The socket sent from the client computer*/
	private Socket clientSocket;
	
	/**The server socket*/
	private ServerSocket server;
	
	/**The port through which the server offers service*/
	private int port;
	
	/**The array holding multiple sockets from various clients*/
	public static Socket[] clientSockets;
	
	
	
	/**The array holding multiple threads handling various clients*/
	public static ClientHandleThread[] threads;
	
	/**The name of each client*/
	public static String[] names;
	
	/**The score of each client*/
	public static int[] scores;
	
	/**The ranking order, holding the competition places of each client*/
	public static int[] orders;
	
	/**Holding game over information of each client*/
	public static boolean[] over;
	
	/**The current number of clients*/
	private static int currentSize;
	
	/**The largest number of clients permitted*/
	private static int MAX=4;
	
	public static long currentTime=0;

	/**The number of clients who have already started the game*/
	private static int startNumber=0;
	
	/**The flag indicating whether the game has begun*/
	public static boolean gameBegin=false;
	
	/**The flag indicating whether all the clients' games are over*/
	public static boolean AllAreOver=false;
	
	/**The timer used for timing*/
	public static GameTimer gameTimer=new GameTimer();
	
	
	
	/**The network stream */
	private static BufferedReader in;
	private static PrintWriter printWriter;
	
	/**The thread used to create random numbers*/
	NumberCreator num=new NumberCreator();
    
    /**The number of online players*/
    private static int amount=0;
	
	/**The constructor*/
	public Server() 
	{
		clientSocket=new Socket();
		
		port=8001;
		
		try
		{
		
		server=new ServerSocket(port);
		}catch(IOException e)
		{
			System.out.println("The port is closed");
		}
		
		currentSize=1;
		
		clientSockets=new Socket[MAX+1];
		
		for(int i=1;i<=MAX;i++)
		   clientSockets[i]=null;
		
		threads=new ClientHandleThread[MAX+1];
		
		names=new String[MAX+1];
		
		for(int i=1;i<=MAX;i++)
		   names[i]=null;
		   
		scores=new int[MAX+1];
		
		 for(int i=1;i<=MAX;i++)
		   scores[i]=0;
		   
		   
		over=new boolean[MAX+1];
		
		orders=new int[MAX+1];
		
		for(int i=1;i<=MAX;i++)
		   over[i]=false;
	
	   
		
     }

   public void run()
   {
   	startUp();
   }	
	
	/**Start up the server*/
	public void startUp()
	{   
	  
	  
	  
	   amount=0;
	   
	   num.numCreatorStart();
		
		while(true)
		{  
		  if(amount<MAX && !gameBegin  )
		   try
		  {
		    /*Listen to accept the connection of a client*/
			clientSocket=server.accept();
			
		  }catch(IOException e)
		  {
		  	System.out.println("Wrong");
		  }
		  
		  
		  
		  InetAddress address=clientSocket.getInetAddress();
		  
		  System.out.println(address+" connects");
			
			amount++;
			
			
			if(amount<=MAX)
			{
			  /*Hold the connected client socket in the array*/
			  clientSockets[currentSize]=clientSocket;
			  
			  /*Create a thread to handle the request of the client*/
			  threads[currentSize]=new ClientHandleThread( clientSockets[currentSize],currentSize);
			  
			  threads[currentSize].start();
			  
			  
			  currentSize++;
			  
			  
			}
			

		
			
			
			
			
		}
	}
	
	/**Send the game starting information to all the clients*/
	public static synchronized void GameStart()
	{   
	   
	       
		for(int i=1;i<=amount;i++)
				   {  
				   	try
				   	{
				   	
				   	 printWriter = new PrintWriter(clientSockets[i].getOutputStream());
				   	 printWriter.println("The game begins!");
				   	 printWriter.flush(); 
				   	}
				   	catch(IOException e)
				   	{
				   	}
				   	 
				   }
				   
				   /*start the timer for timing*/
				   gameTimer.start();
	}
	
	/**send the game over information to all the clients*/
	public static synchronized void GameOver()
	{
		if(AllAreOver)
		  {
		  	for(int i=1;i<=amount;i++)
				   {  
				   	 
				   	try
				   	{
				   	
				   	 printWriter = new PrintWriter(clientSockets[i].getOutputStream());
				   	 printWriter.println("gameOver");
				   	 printWriter.flush(); 
				   	}
				   	catch(IOException e)
				   	{
				   	}
				   
				 
				   	 
				   }
				   
				   /*Sorting in accordance with the scores of the clients*/
				   Rank();
		  }
	}
    
    /**Sorting in accordance with the scores of the clients after the game is over*/
    public static synchronized void Rank()
    {   System.out.println("RANK");
        OutputStream output=null;
	   
	    PrintWriter printWriter=null;
    	
    	int i;
    	int j;
    	int order=0;
    	int max=0;
    	int k=0;
    	int lastMax=-1;
    	
    	for(i=1;i<=amount;i++)
    	{
    		System.out.println(scores[i]);
    	}
    	
    	System.out.println("order");
    	for(i=1;i<=amount;i++)
    	{  
    	  max=0;
    		for(j=1;j<=amount;j++)
    		{
    			if(scores[j]>=max)
    			  {
    			  	max=scores[j];
    			  	order=j;
    			  }
    				
    			
    		}
    		
    		System.out.println(order);
    		
    		if(max<lastMax || lastMax==-1)
    		    k++;
    		  
    		 lastMax=max; 
    		
    		orders[order]=k;//第order个人第k名
    		scores[order]=-1;
    	}
    	
    	  int m=0;
    	  int n=0;
		  String rank="";
		  
		  for(n=1;n<=amount;n++)
		     rank+=new Integer(orders[n]).toString();
		  
		
		  
		  for(m=1;m<=amount;m++)
		  {  
		      try
		  	  {
		  	  
		  		output=(clientSockets[m]).getOutputStream();
		  		
		  		printWriter=new PrintWriter(output);
		  		
		  		
		  	  }
		  	  catch(IOException e)
		  	  {
		  	  	System.out.println(e.getMessage());
		  	  }	
		  	  
		  	  
		  	   
		  	   printWriter.println("rank");
		  	  	
		  	   printWriter.flush();
		  	   
		  	   printWriter.println(new Integer(m).toString());
		  	   
		  	   printWriter.flush();
		  	  	
		  	  	printWriter.println(rank);
		  	  	
		  	  	printWriter.flush();
		  	  	
		 }
    	
    	
    }



    
	/**Store the game over information of a client
	 * @param No the id of the client whose game is over
	 */
	public static synchronized void IamOver(int No)
	{   
	    over[No]=true;
	    
	    boolean AllOver=true;
	    
	    for(int i=1;i<=amount;i++)
	      if(over[i]==false)
	          AllOver=false;
	          
	          if(AllOver)
	              AllAreOver=true;
	              
	              
	              GameOver();
		
      
	}
	
	
	
	
	/**As soon as a client connects to the server, the server immediately sends the names
	 of existing clients to this client
	 * @param printWriter the output stream
	 */
	public static synchronized void sendNameToThisClient(PrintWriter printWriter)
	{
		int i;
		for(i=1;i<=MAX;i++)
		{
			if(names[i]!=null)
			  { 
			    printWriter.println("name");
			    printWriter.flush();
			    
			  	printWriter.println(names[i]);
			  	printWriter.flush();
			  	
			  	printWriter.println(new Integer(i).toString());
			  	printWriter.flush();
			  }
		}
	}
	
	/**send start information to the connected client
	 * @param printWriter the output stream
	 */
	public static synchronized void sendStartInformationToThisClient(PrintWriter printWriter)
	{
		printWriter.println("Start");
		printWriter.flush();
	}
	
	
	/**A client sends his name to other clients
	 * @param No the id of the client
	 * @param in the input stream
	 */
	public static synchronized void sendName(int No, BufferedReader in)
	{   
		OutputStream output=null;
	   
	    PrintWriter printWriter=null;
	    
	    String s=new String();
	    
		try
		  {
		  	s=in.readLine();
		  }
		  catch(IOException e)
		  {
		  	System.out.println(e.getMessage());
		  	
		  }
		  
		  names[No]=new String(s);
		  
		  int i=0;
		  
		  for(i=1;i<=MAX;i++)
		  {
		  	if(clientSockets[i]!=null)
		  	{  
		  	  try
		  	  {
		  	  
		  		output=(clientSockets[i]).getOutputStream();
		  		
		  		printWriter=new PrintWriter(output);
		  		
		  		
		  	  }
		  	  catch(IOException e)
		  	  {
		  	  	System.out.println(e.getMessage());
		  	  }	
		  	 
		  	   printWriter.println("name");
		  	  	
		  	  	printWriter.flush();
		  	  	
		  	  	printWriter.println(s);
		  	  	
		  	  	printWriter.flush();
		  	  	
		      
		        
		        if(i!=No)
		        {
		        
		  	  	printWriter.println(new Integer(No).toString());
		  	  	
		  	  	printWriter.flush();
		  	  	}
		  	  	 else
		  	  	    {
		  	  	    	printWriter.println("-1");
		  	  	    	printWriter.flush();
		  	  	    }
		  	 } 
		  	}	

⌨️ 快捷键说明

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