我假设您希望玩家互相玩,即使他们都没有坐在服务器“游戏”所在的计算机上。在这种情况下,您应该做的是将游戏与网络分开。我还将假设您已将所有游戏部分都按其应有的方式工作。所以这就是你可以做的。
1)首先创建一个只连接到服务器部分的类,您可以将其命名为服务器(参见下面的示例代码)。
2)创建另一个类来制作这些客户端的对象(在你的情况下是玩家)。(再次查看下面的代码)
3) 然后你可以让他们与游戏互动。
如果您在修复它时遇到问题,请告诉我,也许我可以为您提供更多详细信息。
你这个简单的服务器部分。
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
/**
* Created by baljit on 03.05.2015.
* @author Baljit Sarai
*/
public class Server {
ServerSocket serverSocket;
public static List<ClientConnection> clientConnectionList = new ArrayList<ClientConnection>();
public Server() {
try {
serverSocket = new ServerSocket(12345);
System.out.println("Server started");
} catch (IOException ioe) {
System.out.println("Something went wrong");
}
}
public void serve(){
Socket clientSocket;
try {
while((clientSocket = serverSocket.accept()) != null){
System.out.println("got client from"+clientSocket.getInetAddress());
ClientConnection clientConnection = new ClientConnection(clientSocket);
clientConnectionList.add(clientConnection);
ClientHandler clientHandler = new ClientHandler(clientConnection,serverSocket);
clientHandler.start();
}
}catch (IOException ioe){
System.out.println("Something went wrong");
}
}
}
在这种情况下,clientConnection 可以是您应该制作的播放器对象,只是为了让您自己更轻松。
clientHandler是可以同时处理每个玩家的类。
这里的“clientConnectionList”只是用来存储连接,以便您知道在哪里可以找到它们。
所以这里是 ClientConnection 类。
import java.io.*;
import java.net.Socket;
/**
* Created by baljit on 03.05.2015.
* @author Baljit Sarai
*/
public class ClientConnection {
private Socket connection;
private DataInputStream streamIn;
private DataOutputStream streamOut;
private BufferedInputStream bufferedInputStream;
public ClientConnection(Socket socket){
connection = socket;
try{
bufferedInputStream = new BufferedInputStream(connection.getInputStream());
streamIn = new DataInputStream(bufferedInputStream);
streamOut = new DataOutputStream(connection.getOutputStream());
}catch (IOException ioe){
System.out.println("Something went wrong when trying create the Connection Object");
}
}
public boolean isBound(){
return connection.isBound();
}
public DataInputStream getStreamIn() {
return streamIn;
}
public DataOutputStream getStreamOut() {
return streamOut;
}
public void close(){
try{
connection.close();
}catch (IOException ioe){
System.out.print("Something went wrong trying to close the connection");
}
}
public String getAddress(){
return connection.getInetAddress().toString();
}
}
这里是 ClientHandler 类
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Created by baljit on 03.05.2015.
* @author Baljit Sarai
*/
public class ClientHandler extends Thread{
private ServerSocket serverSocket;
String data;
ClientConnection connection;
public ClientHandler(ClientConnection clientConnection, ServerSocket serverSocket) {
connection = clientConnection;
}
public void makeMove(String data){
//This is where you put the logic for how the server can
//interpetate the readed data to a game move
}
@Override
public void run(){
System.out.println("ClientHandler running");
try{
while(connection.isBound()){
data= connection.getStreamIn().readUTF();
this.makeMove(data);
}
connection.close();
}catch (IOException ioe){
System.out.println("Connection dead "+connection.getAddress());
Server.clientConnectionList.remove(connection);
}
}
}
请让我知道它对您的效果如何。也许我可以更好地帮助你:)祝你好运:)