clienthandlethread.java
来自「软件工程实践课程的答案哦」· Java 代码 · 共 114 行
JAVA
114 行
/***/
import java.io.*;
import java.net.*;
/**This file describes the thread on server
*which handles the request from client computer
*
*
*/
public class ClientHandleThread extends Thread
{
/**The ID of a specific client*/
private int No;
/**The client socket*/
private Socket socket;
/**The network I/O stream*/
private BufferedReader in;
private PrintWriter printWriter;
/**The request information from client computer*/
private String clientRequest;
private int maxSize=4;
public ClientHandleThread(Socket clientSocket, int ID)
{
this.socket=clientSocket;
No=ID;
try
{
printWriter=new PrintWriter(socket.getOutputStream());
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
catch(IOException e)
{
System.out.println("Failure of opening the network stream ");
}
clientRequest=new String();
}
public void run()
{ /*send the names of existing clients to this client*/
Server.sendNameToThisClient(printWriter);
/*send the starting information to the client*/
Server.sendStartInformationToThisClient(printWriter);
while(true)
{
/*read the request from client, and dispatch it to a
*specific method in Server to handle*/
try
{
clientRequest=in.readLine();
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
if(clientRequest.equals("send name"))
Server.sendName(No,in);
if(clientRequest.equals("chat"))
Server.chat(No,in);
if(clientRequest.equals("Ready"))
Server.clientStartGame();
if(clientRequest.equals("request for a random number"))
Server.sendNumber(No,printWriter);
if(clientRequest.equals("playingField"))
Server.sendPlayingField(No,in);
if(clientRequest.equals("clear"))
Server.clear(No,in);
if(clientRequest.equals("set"))
Server.set(No,in);
if(clientRequest.equals("score"))
Server.setScore(No,in);
if(clientRequest.equals("IamOver"))
Server.IamOver(No);
clientRequest=null;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?