📄 server.java
字号:
package Login;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Random;
public class Server {
/*
* 共同游戏的人数
*/
int allowNum = 2;
/*
* 食物的位置
*/
static int x = 0, y = 0;
/*
* 客户端的游戏界面的大小和蛇身的宽高
*/
public static final int canvasWidth = 600;
public static final int canvasHeight = 600;
public static final int nodeWidth = 10;
public static final int nodeHeight = 10;
int maxX = canvasWidth / nodeWidth;
int maxY = canvasHeight / nodeHeight;
ServerSocket ss;
Socket s;
ObjectInputStream ois;
ObjectOutputStream oos;
int num = 0;
String content = "";
Thread[] thread = new Thread[allowNum];
LinkedList[] list = new LinkedList[allowNum];
int[] score = new int[allowNum];
boolean[][] matrixFood;
boolean[] running = new boolean[allowNum];
public Server() {
for (int i = 0; i < running.length; i++) {
running[i] = true;
}
matrixFood = new boolean[maxX][];
for (int i = 0; i < maxX; ++i) {
matrixFood[i] = new boolean[maxY];
Arrays.fill(matrixFood[i], false);
}
try {
ss = new ServerSocket(5555);
while (num < allowNum) {
System.out.println("Wait to content...");
s = ss.accept();
System.out.println(s.getInetAddress() + ":" + s.getPort()
+ "::" + num);
oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject(canvasWidth + "");
oos.writeObject(canvasHeight + "");
oos.writeObject(nodeWidth + "");
oos.writeObject(nodeHeight + "");
list[num] = new LinkedList();
thread[num] = new ServerThread(s, num);
if (num == 0) {
Random r = new Random();
x = r.nextInt(maxX);
y = r.nextInt(maxY);
matrixFood[x][y] = true;
}
num++;
}
for (int i = 0; i < allowNum; i++) {
thread[i].start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Server();
}
class ServerThread extends Thread {
ObjectInputStream ois;
ObjectOutputStream oos;
Socket s;
int num;
public ServerThread(Socket s, int num) {
this.s = s;
this.num = num;
}
public void run() {
while (true) {
try {
ois = new ObjectInputStream(s.getInputStream());
String str = (String) ois.readObject();
if (str.equals("update")) {
list[num] = (LinkedList) ois.readObject();
// running[num]=(Boolean) ois.readObject();
oos = new ObjectOutputStream(s.getOutputStream());
if (num == 0) {
oos.writeObject(list[1]);
oos.writeObject(score[1]);
oos.writeObject(running[1]);
}
if (num == 1) {
oos.writeObject(list[0]);
oos.writeObject(score[0]);
oos.writeObject(running[0]);
}
oos.writeObject(x);
oos.writeObject(y);
oos.writeObject(matrixFood);
} else if (str.equals("food")) {
score[num] = Integer.parseInt(ois.readObject()
.toString());
for (int i = 0; i < maxX; ++i) {
matrixFood[i] = new boolean[maxY];
Arrays.fill(matrixFood[i], false);
}
oos = new ObjectOutputStream(s.getOutputStream());
Random r = new Random();
x = r.nextInt(maxX);
y = r.nextInt(maxY);
matrixFood[x][y] = true;
oos.writeObject(x);
oos.writeObject(y);
oos.writeObject(matrixFood);
} else if (str.equals("start")) {
oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject(x + "");
oos.writeObject(y + "");
oos.writeObject(matrixFood);
} else if (str.equals("stop")) {
running[0] = false;
running[1] = false;
}
} catch (Exception e) {
System.out.println(num + "用户退出");
break;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -