📄 eightgame.java
字号:
}
}else{
try{
Scanner reader=new Scanner(s.getInputStream());
String message=reader.nextLine();
Info info=new Info(message,0);
Connection c=new Connection(s,info);
connections.add(c);
others.add(info);
PrintWriter out=new PrintWriter(s.getOutputStream());
out.print("400 ");
out.flush();
for(int i=0;i<10;i++){
out.print(curStatus[i]+" ");
}
for(Info i:others){
out.print(i.userid+" ");;
}
out.println();
out.flush();
c.start();
for(Connection i:connections){
i.sendUpdate(message);
}
}catch(IOException e){
}
}
}
public void close(){
if(isServer()){
for(Connection i:connections){
i.sendServerClose();
i.stopConnection();
}
}else if(clientConnection!=null){
clientConnection.stopClient();
}
}
/**
*
* @author Ray#
*/
public class Listener extends Thread{
ServerSocket listen_socket;
int port;
volatile boolean stop=false;
/** Creates a new instance of Listener */
public Listener(ThreadGroup group,int port) throws IOException{
super(group,"EightGame Listener:"+port);
listen_socket=new ServerSocket(port);
this.port=port;
}
public void stopListener(){
this.stop=true;
this.interrupt();
try{
listen_socket.close();
}catch(IOException e){
EightUtil.log(e);
}
}
public void run(){
while(!stop){
try{
Socket client=listen_socket.accept();
System.out.println(client.getInetAddress());
addConnection(client);
}catch(InterruptedIOException e){
EightUtil.log(e);
}catch(IOException e){
EightUtil.log(e);
}
}
}
}
/**
* 400 connect successed
* 401 wait players + userid
* 402 start game
* 403 update info + userinfos eg. 403+steps+win / 403+userid+steps+win?
* 404 connect closed 404+userid
* 405 someone win the game
* 500 server closed
* 501 fail to connect
*/
public class Connection extends Thread{
Socket client;
volatile boolean stop=false;
Info userInfo;
PrintWriter out;
public Connection(Socket client,Info user){
super("EightGame.Connection: "+client.getInetAddress()+":"+client.getPort());
this.client=client;
userInfo=user;
}
public void sendServerClose(){
out.println("501");
out.flush();
}
public void stopConnection(){
stop=true;
try{
sendClose(userInfo.userid);
client.close();
}catch(IOException e){
EightUtil.log(e);
}
}
public void sendUpdate(String userid){
if(out==null){
try{
out=new PrintWriter(client.getOutputStream());
}catch(IOException e){
EightUtil.log(e);
}
}
out.println("401+"+userid);
out.flush();
}
public void sendStartGame(){
out.println("402");
out.flush();
}
public void sendUserInfos(){
out.print("403+");
for(Info i:others){
out.print(i.userid+"+"+i.step+"+"+i.win+"?");
if(i.win){
startgame=false;
}
}
out.println();
if(!startgame){
out.println("405");
}
out.flush();
}
public void sendClose(String name){
out.println("404+"+name);
out.flush();
}
public void sendWin(){
out.println("405+"+userInfo.userid);
out.flush();
}
public void run(){
try{
Scanner jin=new Scanner(client.getInputStream());
if(out==null)
out=new PrintWriter(client.getOutputStream());
String line;
String[] message;
while(!stop && client.isConnected() && jin.hasNext()){
line=jin.nextLine();
if(line!=null){
message=line.split("[+]+");
message[0]=message[0].toLowerCase();
if(message[0].startsWith("400")){
}else if(message[0].startsWith("401")){
}else if(message[0].startsWith("402")){
startGame();
}else if(message[0].startsWith("403")){
try{
userInfo.step=Integer.parseInt(message[1]);
userInfo.win=Boolean.parseBoolean(message[2]);
}catch(Exception e){
EightUtil.log(e);
}
}else if(message[0].startsWith("404")){
for(Connection i:connections){
i.sendClose(userInfo.userid);
}
others.remove(userInfo);
stop=true;
connections.remove(this);
}else if(message[0].startsWith("405")){
for(Connection i:connections){
i.sendWin();
}
win=false;
userInfo.setWin();
}
}
}
for(Connection i:connections){
i.sendClose(userInfo.userid);
}
others.remove(userInfo);
stop=true;
connections.remove(this);
}catch(IOException e){
EightUtil.log(e);
}
}
}
public class Client extends Thread{
Socket client;
PrintWriter out;
private volatile boolean stop=false;
public Client(Socket s){
super("EightGame.Clien: "+s.getInetAddress()+":"+s.getPort());
client=s;
}
public void stopClient(){
stop=true;
out.println("404");
out.flush();
out.close();
try{
client.close();
}catch(IOException e){
EightUtil.log(e);
}
}
public void updateInfo(String tuser,int tstep,boolean twin){
if(tuser!=null){
for(Info i:others){
if(i.userid.equals(tuser)){
i.step=tstep;
i.win=twin;
break;
}
}
}
out.println("403+"+steps+"+"+win);
if(win)startgame=false;
out.flush();
}
public void run(){
try{
Scanner jin=new Scanner(client.getInputStream());
out=new PrintWriter(client.getOutputStream());
String[] message;
while(!stop && client.isConnected()){
message=jin.nextLine().split("[+?]+");
if(message!=null){
message[0]=message[0].toLowerCase();
if(message[0].startsWith("400")){
}else if(message[0].startsWith("401")){
int i;
for(i=0;i<others.size();i++){
if(others.get(i).userid.equals(message[1]))
break;
}
if(i==others.size())
others.add(new Info(message[1],0));
}else if(message[0].startsWith("402")){
startGame();
}else if(message[0].startsWith("403")){
int i=1;
while(i<message.length && message[i].length()>0){
String tuser=message[i++];
int tstep=Integer.parseInt(message[i++]);
boolean twin=Boolean.parseBoolean(message[i++]);
updateInfo(tuser,tstep,twin);
}
}else if(message[0].startsWith("404")){
for(int i=0;i<others.size();i++){
if(others.get(i).userid.equals(message[1])){
others.remove(i);
break;
}
}
}else if(message[0].startsWith("405") || message[0].startsWith("500")){
startgame=false;
stop=true;
out.close();
try{
client.close();
}catch(IOException e){
EightUtil.log(e);
}
}else if(message[0].startsWith("501")){
startgame=false;
}
}
}
}catch(IOException e){
EightUtil.log(e);
}finally{
startgame=false;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -