📄 serverhandler.java~26~
字号:
package GameServer;
import java.util.Vector;
import java.net.Socket;
import java.net.ServerSocket;
import java.util.Vector;
import java.util.Enumeration;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
import ClientMessage.RequestCard;
import java.lang.reflect.Array;
import ClientMessage.Hold;
import ServerMessage.WinMessage;
import ServerMessage.LoseMessage;
import ServerMessage.TieMessage;
import ServerMessage.TurnMessage;
import ServerMessage.StartAgain;
import ClientMessage.GameNotWant;
import ServerMessage.GameStart;
import ServerMessage.GameOver;
import java.io.Serializable;
import javax.swing.ImageIcon;
public class ServerHandler extends Thread implements Serializable{
Deck deck;
protected static Vector clientList = new Vector();
protected static Vector clientName=new Vector();
protected Socket sock1;
protected Socket sock2;
protected ObjectInputStream in1;
protected ObjectOutputStream out1;
protected ObjectInputStream in2;
protected ObjectOutputStream out2;
protected String name;
int result[]=new int[3];
boolean flag1;
boolean flag2;
boolean bomp1;
boolean bomp2;
boolean again;
boolean first1=true;
boolean first2=true;
protected Vector v1=new Vector();
protected Vector v2=new Vector();
public ServerHandler(Socket sock1,Socket sock2) throws IOException {
deck=new Deck();
this.sock1 = sock1;
this.sock2 = sock2;
InputStream is1 = sock1.getInputStream();
OutputStream os1 = sock1.getOutputStream();
out1 = new ObjectOutputStream(os1);
in1= new ObjectInputStream(is1);
InputStream is2 = sock2.getInputStream();
OutputStream os2 = sock2.getOutputStream();
out2 = new ObjectOutputStream(os2);
in2= new ObjectInputStream(is2);
bomp1=false;
bomp2=false;
again=true;
flag1=false;
flag2=false;
result[1]=0;
result[2]=0;
}
public void run() {
try {
while (again) {
//TurnMessage
out1.writeObject(new TurnMessage());
Object o1 = in1.readObject();
if(o1 instanceof Result)
out2.writeObject(o1);
if (o1 instanceof RequestCard) {
Card d1 = deck.getCard();
int i=d1.getNumber();
ImageIcon in=d1.getSuite();
out1.writeObject(new Card(i,in));
if(!first1)
{
out2.writeObject(new Result(d1));
first1=false;
}
result[1] += (int) (d1.getNumber());
if (result[1] > 21) {
bomp1 = true;
out1.writeObject(new LoseMessage());
out2.writeObject(new WinMessage());
out1.writeObject(new StartAgain());
Object oo1 = in1.readObject();
if (oo1 instanceof GameNotWant) {
again = false;
}
else{
out2.writeObject(new StartAgain());
Object oo2 = in2.readObject();
if (oo2 instanceof GameNotWant)
again = false;
if (again == true) {
out1.writeObject(new GameStart());
out2.writeObject(new GameStart());
deck = new Deck();
result[1]=0;
result[2]=0;
}
if (again == false) {
out1.writeObject(new GameOver());
out2.writeObject(new GameOver());
}
}
continue;
}
}
if (o1 instanceof Hold) {
flag1 = true;
}
if (!bomp1) {
out2.writeObject(new TurnMessage());
Object o2 = in2.readObject();
if(o2 instanceof Result)
out1.writeObject(o2);
if (o2 instanceof RequestCard) {
Card d2 = deck.getCard();
int j=d2.getNumber();
ImageIcon jm=d2.getSuite();
out2.writeObject(new Card(j,jm));
if(!first2)
{
out1.writeObject(new Result(d2));
first2=false;
}
result[2] += d2.getNumber();
if (result[2] > 21) {
bomp2 = true;
out2.writeObject(new LoseMessage());
out1.writeObject(new WinMessage());
out1.writeObject(new StartAgain());
Object oo1 = in1.readObject();
if (oo1 instanceof GameNotWant) {
again = false;
}
else{
out2.writeObject(new StartAgain());
Object oo2 = in2.readObject();
if (oo2 instanceof GameNotWant)
again = false;
if (again == true) {
out1.writeObject(new GameStart());
out2.writeObject(new GameStart());
deck = new Deck();
result[1]=0;
result[2]=0;
}
if (again == false) {
out1.writeObject(new GameOver());
out2.writeObject(new GameOver());
}
}
continue;
}
}
if (o2 instanceof Hold) {
flag2 = true;
}
}
if(flag1&&flag2&&!bomp1&&!bomp2)
{
if (result[1] > result[2]) {
out1.writeObject(new WinMessage());
out2.writeObject(new LoseMessage());
}
if (result[1] < result[2]) {
out2.writeObject(new WinMessage());
out1.writeObject(new LoseMessage());
}
if (result[1] == result[2]) {
out1.writeObject(new TieMessage());
out2.writeObject(new TieMessage());
}
out1.writeObject(new StartAgain());
Object oo1 = in1.readObject();
if (oo1 instanceof GameNotWant) {
again = false;
}
else {
out2.writeObject(new StartAgain());
Object oo2 = in2.readObject();
if (oo2 instanceof GameNotWant)
again = false;
}
if (again == true) {
out1.writeObject(new GameStart());
out2.writeObject(new GameStart());
deck = new Deck();
}
if (again == false) {
out1.writeObject(new GameOver());
out2.writeObject(new GameOver());
}
}
}
} catch (ClassNotFoundException cnfe) {
/* This happens if one of the other clients tries to send us
* an object whose type is not defined on our machines. */
cnfe.printStackTrace();
} catch (IOException ioe) {
System.err.println("Lost client connection: " +
sock1.getInetAddress());
System.err.println("Lost client connection: " +
sock2.getInetAddress());
} finally {
try {
sock1.close();
sock2.close();
} catch (IOException ex) {
// Problem closing the socket. Ignore.
}
}
}
// start game
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -