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

📄 communication.java

📁 软件工程实践课程的答案哦
💻 JAVA
字号:
/**This file describes the 
 *network communication class
 *on client computer
 */
import java.io.*;
import java.net.*;
import java.util.*;

public class Communication
{   
    /**The random colorset sent by server which is used to create a bar*/
	private static ColorSet randomColorSet;
	/**The client socket*/
	static Socket socket;
	
	/**The network data stream*/
    static BufferedReader in;
	static PrintWriter printWriter;
	
	/**The reference to the game interface*/
	private static GameFace gameFace;
	
	/**The IP address of the server*/
	private static String serverIP;
	
	/**The port of the server*/
	private static int port=8001;
	
	/**The name of the client*/
	private static String name;
	
	static String s;
	
	/**Create the client socket and open the network stream*/
	public static void init()
	{   
	    boolean success=false;
	    
	    s=new String();
	    
		randomColorSet=null;
		
	

	
		
		try
		  {
		  	socket=new Socket(serverIP,port);
		  	success=true;
		  }
		  catch(UnknownHostException e)
		  {
		  	System.out.println("The server does not exists! ");
		  }
		  catch(IOException e)
		  {
		  	System.out.println("The port is closed. Please select another port. ");
		  }
		  
		  
		  if(success)
		  {
		  	try
		  	{
		  	
		  	    printWriter=new PrintWriter(socket.getOutputStream());
		  		in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
		  	    
		  	    GameFace.otherThread.interrupt();
		  	    
		  	    //System.out.println("input stream create success");	
		  		
		  		//System.out.println("input create success");
		  	}
		  	catch(IOException e)
		  	{
		  		System.out.println("Failure of opening the network stream ");
		  	}
		  }
		  
	
		  
	}
	
	
	public static void setGameFace(GameFace game)
	{
		gameFace=game;
	}
	
	
	/**set the IP of the server
	 * @param s the serverIP
	 */
	public static void setServerIP(String s)
	{
		serverIP=s;
	}
	
	
	/**Set the name of the client
	 * @param s
	 */
	public static void setName(String s)
	{
		name=s;
	}
	
	
	/**send the client's name to the server
	 * 
	 */
	public static void sendName()
	{
		printWriter.println("send name");
		printWriter.flush();
		
		printWriter.println(name);
		printWriter.flush();
	}
	

	
	/**send the chating information to other clients
	 * @param s the chatting information
	 */
	public static void sendChatContent(String s)
	{   
		printWriter.println("chat");
		printWriter.flush();
		printWriter.println(s);
		printWriter.flush();
	}
	
	
	/**send the playing field of the client to the server in the form of a string following some protocal
	 * 
	 */
	public static void sendPlayingField()
	{
		String s="";
		
		int i=0;
		int j=0;
		
		for(i=0;i<18;i++)
		  for(j=0;j<11;j++)
		  {  
		    if(GamePool.gamePool[i][j]!=0)
		   {
		       
		  	s+=i;
		  	s+=" ";
		  	s+=j;
		  	s+=" ";
		  	s+=GamePool.gamePool[i][j];
		  	s+=" ";
		   }
		   
		  }
		  
		  
		  printWriter.println("playingField");
	
		  printWriter.flush();
		  
		  printWriter.println(s);
		  
		  printWriter.flush();
	}
	
	
	/**request a set of random numbers from the server to create a bar
	 * 
	 */
	public static void requestRandomNumber()
	{  
		printWriter.println("request for a random number");
		printWriter.flush();
		
	  
	}
	
	
	
	
	/**The client starts the game
	 * @param s the ready information
	 */
	public static void ClientReady(String s)
	{   
	    //System.out.println("ClientReady");
		printWriter.println(s);
		printWriter.flush();
	}
	
	
	/**send the message of a location cleared on the client to the server
	 * @param location the specific location to be cleared
	 */
	public static void clear(Location location)
	{
		printWriter.println("clear");
		printWriter.flush();
		
		String s="";
		
		int x=location.getX();
		int y=location.getY();
		int color=location.getColor();
		
		s+=x;
		s+=" ";
		s+=y;
		s+=" ";
		s+=color;
		
		System.out.println("***clear");
		System.out.println(s);
		printWriter.println(s);
		printWriter.flush();
		
	}
	
	
	/**send the message of a bar cleared on the client to the server
	 * @param bar the specific bar to be cleared
	 */
	public static void clear(Bar bar)
	{
		printWriter.println("clear");
		printWriter.flush();
		
		String s="";
		
		int x,y,color;
		
		for(int i=0;i<3;i++)
		{
			x=bar.squares[i].getX();
			y=bar.squares[i].getY();
			color=bar.squares[i].getColor();
			
			s+=x;
			s+=" ";
			s+=y;
			s+=" ";
			s+=color;
			s+=" ";
		}
		
		printWriter.println(s);
		printWriter.flush();
	}
	
	
	/**send the message of a location colored on the client to the server
	 * @param location the location of the bar
	 */
	public static void set(Location location)
	{  System.out.println("Communication.set");
		printWriter.println("set");
		printWriter.flush();
		
		String s="";
		
		int x=location.getX();
		int y=location.getY();
		int color=location.getColor();
		
		s+=x;
		s+=" ";
		s+=y;
		s+=" ";
		s+=color;

		printWriter.println(s);
		printWriter.flush();
	
		
	}
   	
   	
   /**send the message of a bar colored on the client to the server
 * @param bar the specific bar to be colored
 */
public static void set(Bar bar)
	{
		printWriter.println("set");
		printWriter.flush();
		
		String s="";
		
		int x,y,color;
		
		for(int i=0;i<3;i++)
		{
			x=bar.squares[i].getX();
			y=bar.squares[i].getY();
			color=bar.squares[i].getColor();
			
			s+=x;
			s+=" ";
			s+=y;
			s+=" ";
			s+=color;
			s+=" ";
		}
		
		printWriter.println(s);
		printWriter.flush();
	}
   
    
    /**send the score of the client to the server
     * @param score the score to be sent
     */
    public static void setScore(int score)
    {
    		printWriter.println("score");
    	    	printWriter.flush();
    	    printWriter.println(new Integer(score).toString());
    	        printWriter.flush();
    	
    }
	
	
	/**The client's game is over, send the information to the server
	 * 
	 */
	public static void IamOver()
	{
			printWriter.println("IamOver");
			printWriter.flush();
	}
	
	
	
	
	
}

⌨️ 快捷键说明

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