我正在尝试使用jmonkey制作视频游戏,但无法使客户端与服务器一起使用。它连接到服务器并创建一个线程,但是该线程不会接收客户端发送的内容。我不确定是什么问题,请帮助我。
这是我的代码:
客户端主类
package mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.CameraNode;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.CameraControl.ControlDirection;
import com.jme3.util.SkyFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
public class Main extends SimpleApplication {
public static void main(String[] args) {
Main app = new Main();
app.start();
}
protected Spatial xwing;
protected Spatial tie;
protected Node playerNode;
protected Spatial player;
protected Spatial player1;
protected Spatial player2;
protected Spatial player3;
protected Spatial player4;
protected Spatial player5;
protected Spatial player6;
protected Spatial player7;
boolean gameStarted = false;
String myid;
String id1;
String id2;
String id3;
String id4;
String id5;
String id6;
String id7;
float value2 = 2;
String where;
@Override
public void simpleInitApp() {
xwing = assetManager.loadModel("Models/X-Wing model.j3o");
tie = assetManager.loadModel("Models/TieFighter.j3o");
System.out.print("Enter username: ");
Scanner type = new Scanner(System.in);
myid = type.nextLine();
type.close();
while (myid == null) {}
simpleUpdate(0);
while (gameStarted == false) {}
getRootNode().attachChild(SkyFactory.createSky(getAssetManager(), "Textures/space/space.jpg", SkyFactory.EnvMapType.EquirectMap));
DirectionalLight dl = new DirectionalLight();
dl.setColor(ColorRGBA.White);
dl.setDirection(new Vector3f(2.8f, -2.8f, -2.8f).normalizeLocal());
rootNode.addLight(dl);
playerNode = new Node("playerNode");
playerNode.attachChild(player);
rootNode.attachChild(playerNode);
rootNode.attachChild(player1);
rootNode.attachChild(player2);
rootNode.attachChild(player3);
rootNode.attachChild(player4);
rootNode.attachChild(player5);
rootNode.attachChild(player6);
rootNode.attachChild(player7);
initKeys();
flyCam.setEnabled(false);
CameraNode camNode = new CameraNode("Camera Node", cam);
camNode.setControlDir(ControlDirection.SpatialToCamera);
playerNode.attachChild(camNode);
camNode.setLocalTranslation(new Vector3f(0, 1, -5));
camNode.lookAt(player.getLocalTranslation(), Vector3f.UNIT_Y);
}
@Override
public void simpleUpdate(float tpf) {
InputStream in;
BufferedReader read;
PrintStream out;
String line;
try {
Socket socket = new Socket("192.168.1.171", ServerConnect.PORT);
in = socket.getInputStream();
read = new BufferedReader(new InputStreamReader(in));
out = new PrintStream(socket.getOutputStream());
if (gameStarted == false) {
out.print(myid);
out.flush();
out.print(myid);
out.flush();
System.out.print("Connected");
}
line = read.readLine();
if (line.equals("begin")) {
gameStarted = true;
}
if(gameStarted == false) {
if(line.contains(myid)) {
line = line.replaceAll("\\pL+","");
if (line.equals("1")) {
player = xwing;
}
else {
player = tie;
}
}
if(!line.contains(myid) && id1 == null) {
id1 = line.replaceAll("[^A-Za-z]", "");
line = line.replaceAll("\\pL+","");
if (line.equals("1")) {
player1 = xwing;
}
else {
player1 = tie;
}
}
else if((!line.contains(id1)) && id2 == null) {
id2 = line.replaceAll("[^A-Za-z]", "");
line = line.replaceAll("\\pL+","");
if (line.equals("1")) {
player2 = xwing;
}
else {
player2 = tie;
}
}
else if((!line.contains(id2)) && id3 == null) {
id3 = line.replaceAll("[^A-Za-z]", "");
line = line.replaceAll("\\pL+","");
if (line.equals("1")) {
player3 = xwing;
}
else {
player3 = tie;
}
}
else if((!line.contains(id3)) && id4 == null) {
id4 = line.replaceAll("[^A-Za-z]", "");
line = line.replaceAll("\\pL+","");
if (line.equals("1")) {
player4 = xwing;
}
else {
player4 = tie;
}
}
else if((!line.contains(id4)) && id5 == null) {
id5 = line.replaceAll("[^A-Za-z]", "");
line = line.replaceAll("\\pL+","");
if (line.equals("1")) {
player5 = xwing;
}
else {
player5 = tie;
}
}
else if((!line.contains(id5)) && id6 == null) {
id6 = line.replaceAll("[^A-Za-z]", "");
line = line.replaceAll("\\pL+","");
if (line.equals("1")) {
player6 = xwing;
}
else {
player6 = tie;
}
}
else if((!line.contains(id6)) && id7 == null) {
id7 = line.replaceAll("[^A-Za-z]", "");
line = line.replaceAll("\\pL+","");
if (line.equals("1")) {
player7 = xwing;
}
else {
player7 = tie;
}
}
}
if (gameStarted == true) {
if (where != null) {
out.print(myid + where);
out.flush();
where = null;
}
if (line.contains(id1)) {
if (line.contains("Up")) {
player1.rotate(-value2 * speed, 0, 0);
}
if (line.contains("Left")) {
player1.rotate(0, value2 * speed, 0);
}
if (line.contains("Right")) {
player1.rotate(0, -value2 * speed, 0);
}
if (line.contains("Down")) {
player1.rotate(value2 * speed, 0, 0);
}
if (line.contains("Foward")) {
player1.move(player1.getLocalRotation().getRotationColumn(2).mult(1 * tpf));
}
}
else if (line.contains(id2)) {
if (line.contains("Up")) {
player2.rotate(-value2 * speed, 0, 0);
}
if (line.contains("Left")) {
player2.rotate(0, value2 * speed, 0);
}
if (line.contains("Right")) {
player2.rotate(0, -value2 * speed, 0);
}
if (line.contains("Down")) {
player2.rotate(value2 * speed, 0, 0);
}
if (line.contains("Foward")) {
player2.move(player2.getLocalRotation().getRotationColumn(2).mult(1 * tpf));
}
}
else if (line.contains(id3)) {
if (line.contains("Up")) {
player3.rotate(-value2 * speed, 0, 0);
}
if (line.contains("Left")) {
player3.rotate(0, value2 * speed, 0);
}
if (line.contains("Right")) {
player3.rotate(0, -value2 * speed, 0);
}
if (line.contains("Down")) {
player3.rotate(value2 * speed, 0, 0);
}
if (line.contains("Foward")) {
player3.move(player3.getLocalRotation().getRotationColumn(2).mult(1 * tpf));
}
}
else if (line.contains(id4)) {
if (line.contains("Up")) {
player4.rotate(-value2 * speed, 0, 0);
}
if (line.contains("Left")) {
player4.rotate(0, value2 * speed, 0);
}
if (line.contains("Right")) {
player4.rotate(0, -value2 * speed, 0);
}
if (line.contains("Down")) {
player4.rotate(value2 * speed, 0, 0);
}
if (line.contains("Foward")) {
player4.move(player4.getLocalRotation().getRotationColumn(2).mult(1 * tpf));
}
}
else if (line.contains(id5)) {
if (line.contains("Up")) {
player5.rotate(-value2 * speed, 0, 0);
}
if (line.contains("Left")) {
player5.rotate(0, value2 * speed, 0);
}
if (line.contains("Right")) {
player5.rotate(0, -value2 * speed, 0);
}
if (line.contains("Down")) {
player5.rotate(value2 * speed, 0, 0);
}
if (line.contains("Foward")) {
player5.move(player5.getLocalRotation().getRotationColumn(2).mult(1 * tpf));
}
}
else if (line.contains(id6)) {
if (line.contains("Up")) {
player6.rotate(-value2 * speed, 0, 0);
}
if (line.contains("Left")) {
player6.rotate(0, value2 * speed, 0);
}
if (line.contains("Right")) {
player6.rotate(0, -value2 * speed, 0);
}
if (line.contains("Down")) {
player6.rotate(value2 * speed, 0, 0);
}
if (line.contains("Foward")) {
player6.move(player6.getLocalRotation().getRotationColumn(2).mult(1 * tpf));
}
}
else if (line.contains(id7)) {
if (line.contains("Up")) {
player7.rotate(-value2 * speed, 0, 0);
}
if (line.contains("Left")) {
player7.rotate(0, value2 * speed, 0);
}
if (line.contains("Right")) {
player7.rotate(0, -value2 * speed, 0);
}
if (line.contains("Down")) {
player7.rotate(value2 * speed, 0, 0);
}
if (line.contains("Foward")) {
player7.move(player7.getLocalRotation().getRotationColumn(2).mult(1 * tpf));
}
}
}
} catch (IOException ex) {
}
}
@Override
public void simpleRender(RenderManager rm) {
//TODO: add render code
}
private void initKeys() {
// You can map one or several inputs to one named action
inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping("Foward", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_S));
// Add the names to the action listener.
inputManager.addListener(analogListener, "Left", "Foward", "Down", "Right", "Up");
}
private final AnalogListener analogListener = new AnalogListener() {
@Override
public void onAnalog(String name, float value, float tpf) {
value2 = value;
where = name;
if (name.equals("Foward")) {
playerNode.move(player.getLocalRotation().getRotationColumn(2).mult(1 * tpf));
}
if (name.equals("Left")) {
playerNode.rotate(0, value * speed, 0);
}
if (name.equals("Down")) {
playerNode.rotate(value * speed, 0, 0);
}
if (name.equals("Right")) {
playerNode.rotate(0, -value * speed, 0);
}
if (name.equals("Up")) {
playerNode.rotate(-value * speed, 0, 0);
}
}
};
}
ServerConnect类
package mygame;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class ServerConnect {
public static int PORT = 1111;
public static int clients = 0;
public static boolean start = false;
public static void main(String args[]) throws IOException {
ServerSocket serverSocket = new ServerSocket(PORT);
Socket socket;
Scanner type = new Scanner(System.in);
while (clients < 8 && start == false) {
try {
socket = serverSocket.accept();
System.out.print("New Client");
clients++;
// new thread for a client
new Server(socket).start();
if (type.nextLine().equals("begin")) {
start = true;
}
} catch (IOException e) {
System.out.println("I/O error: " + e);
}
}
}
}
服务器类
package mygame;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Random;
public class Server extends Thread {
Socket socket;
boolean started = false;
int x = 0;
int t = 0;
public Server(Socket clientSocket) {
socket = clientSocket;
}
@Override
public void run() {
InputStream in;
BufferedReader read;
PrintStream out;
String line;
Random random = new Random();
int n = random.nextInt(2)+1;
try{
in = socket.getInputStream();
read = new BufferedReader(new InputStreamReader(in));
out = new PrintStream(socket.getOutputStream());
line = read.readLine();
System.out.print(line);
if (ServerConnect.start == false) {
if (x == t){
line = line + n;
if (n == 1){
x++;
} else {
t++;
}
}
else if(x < t) {
line = line + 1;
x++;
}
else if (x > t) {
line = line + 2;
t++;
}
}
if (ServerConnect.start == true && started == false) {
started = true;
line = "begin";
}
if (line == null) {
socket.close();
} else {
out.print(line);
out.flush();
}
} catch (IOException e) {
}
}
}