因此,我已经编程了一段时间,并且我第一次尝试制作多人游戏。我现在面临的主要问题是同时更新服务器端多个客户端的数据。
这是我的代码背后的概念:
我的主要问题是第3步,我的问题是我主线程中的语法错误:
public static void updatethings() {
for (ServerSocket i : ssList) {
i.calculatePos();
}
}
错误消息是:对于ServerSocket类型,方法calculatePos()未定义。
我尝试做的是创建一个ServerSockets数组,并每秒更新它们60次。每个套接字都是它自己的服务器类的线程。
我列出了下面的整个服务器程序:
这是服务器的主类。此类侦听客户端连接,并在每次客户端连接时创建一个新线程。
public class ServerMain {
static ArrayList<ServerSocket> ssList = new ArrayList<ServerSocket>();
static int oh = 0;
public static void main(String[] args) throws IOException {
int portNumber = 9000;
boolean listening = true;
UpdaterThread update = new UpdaterThread();
update.start();
try (ServerSocket serverSocket = new ServerSocket(portNumber)) {
while (listening) {
new ServerThread(serverSocket.accept()).start();
ssList.add(serverSocket);
}
} catch (IOException e) {
System.err.println("Could not listen on port " + portNumber);
System.exit(-1);
}
}
//does not work
public static void updatethings() {
for (ServerSocket i : ssList) {
ServerSocket.calculatePos();
}
}
}
服务器线程类:
public class ServerThread
extends Thread {
private Socket socket = null;
byte input;
private float playery;
private float playerx;
public ServerThread(Socket socket) {
this.socket = socket;
}
public void calculatepos() {
if (input == 0) {
}
if (input == 1) {
playery = playery + 1;
}
if (input == 2) {
playerx = playerx - 1;
}
if (input == 3) {
playery = playery - 1;
}
if (input == 4) {
playerx = playerx + 1;
}
if (input == 5) {
playerx -= 0.7;
playery += 0.7;
}
if (input == 6) {
playerx -= 0.7;
playery -= 0.7;
}
if (input == 7) {
playerx += 0.7;
playery -= 0.7;
}
if (input == 8) {
playerx += 0.7;
playery += 0.7;
}
System.out.println(playerx);
System.out.println(playery);
}
public void run() {
try (
// PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
) {
String inputLine;
while ((inputLine = in.readLine()) != null) {
input = Byte.parseByte(inputLine);
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
更新程序线程类:
class UpdaterThread
extends Thread {
float time = 0;
float lasttime = 0;
public void run() {
while (true) {
time = Time.getTime();
if (time > 1000 / 60 + lasttime) {
lasttime = time;
ServerMain.updatethings();
}
try {
Thread.sleep(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
答案 0 :(得分:0)
您的错误来自服务器。您一遍又一遍地存储相同的服务器套接字。并且您不存储ServerThread
对象
ServerSocket accept() - http://download.java.net/jdk7/archive/b123/docs/api/java/net/ServerSocket.html#accept()
侦听与此套接字的连接并接受它。该方法将阻塞,直到建立连接。一个新的Socket是 如果有安全管理器,则创建安全管理器 使用s.getInetAddress()调用checkAccept方法.getHostAddress() 和s.getPort()作为其参数,以确保允许操作。 这可能会导致SecurityException。
返回:新的Socket
因此,您应该如何处理客户端是保存此 ServerThread 对象。如果你也想要相关的返回套接字。 !
public class ServerMain {
static ArrayList <Socket> sSocketList = new ArrayList<Socket>();
static ArrayList <ServerThread> sObjectList = new ArrayList<ServerThread>();
static int oh = 0;
public static void main(String[] args) throws IOException {
int portNumber =9000;
boolean listening = true;
UpdaterThread update =new UpdaterThread();
update.start();
try (ServerSocket serverSocket = new ServerSocket(portNumber)) {
while (listening) {
Socket s= serverSocket.accept();
ServerThread st= new ServerThread(s);
sSocketList.add(s);
sObjectList.add(st);
st.start();
}
} catch (IOException e) {
System.err.println("Could not listen on port " + portNumber);
System.exit(-1);
}
}
//This might work now
public static void updatethings() {
for (ServerThread i : sObjectList){
i.calculatePos();
}
}
}