1
我正在写一个程序,基本上是涉及一个服务器,处理游戏逻辑和多个客户端(最多四个),基本上是球员的纸牌游戏。客户端使用JFrame制作的所有GUI。无论如何,一部分涉及按下其中一个JFrame上的按钮。这应该向服务器发送一个字符串,并且服务器应该向所有客户端返回一个新字符串。但是,我的代码不起作用。这里简要介绍一下我到目前为止有:服务器与多个客户端(Java) - 发送字符串
服务器代码:
public class Server {
ServerSocket ss = null;
ArrayList<Game> clients; //'Game' is essentially my Handle A Client class
//bunch of other variables that store things like players, etc
public Server() {
try {
ss = new ServerSocket(7777);
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
idGen = 0;
numPs = 0;
tPlayers = new ArrayList<Player>();
cPlayers = new ArrayList<Player>();
clients = new ArrayList<Game>();
while (true) {
try {
Socket s = ss.accept();
Game g = new Game(s, this, idGen);
clients.add(g);
Thread thr = new Thread(g);
thr.start();
idGen++;
} catch (Exception e) {
System.out.println("got an exception" + e.getMessage());
}
System.out.println("got a connection");
try {
Player obj = null;
FileInputStream fis = new FileInputStream("User_Database.sav"); //serializes the player object and reads from file
ObjectInputStream oi = new ObjectInputStream(fis);
while ((obj = (Player) oi.readObject()) != null) {
tPlayers.add(obj);
}
oi.close();
} catch (IOException ie) {
} catch (ClassNotFoundException e) {
System.out.println("Class not found exception.");
}
}
}
public class Game implements Runnable {
int id;
Socket mySocket;
Server serv;
PrintWriter out;
BufferedReader in;
public Game(Socket s, Server ser, int i) {
mySocket = s;
id = i;
serv = ser;
try {
out = new PrintWriter(mySocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
public void run() {
while (true) {
try {
String msg = in.readLine();
if (msg == "p") {
serv.paintGame(); //this is the thing that should send out multiple strings
} else {
//does some other stuff
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
这里是服务器中的paintGame()。它创建一些字符串,然后把这些给客户端:
public void paintGame(){
String tCards = "";
System.out.println("Adding words to strings");
if(numPs == 2){
tCards = "" + h1.get(0).gif + "_" + h2.get(0).gif + "";
}
else if(numPs == 3){
tCards = "" + h1.get(0).gif + "_" + h2.get(0).gif + "_" + h3.get(0).gif + "";
}
else if(numPs == 4){
tCards = "" + h1.get(0).gif + "_" + h2.get(0).gif + "_" + h3.get(0).gif + "_" + h4.get(0).gif + "";
}
System.out.println("Finished adding");
for(int i = 0; i < clients.size(); i++){
try{
clients.get(i).out.println(tCards);
clients.get(i).out.flush();
}
catch (Exception e){}
}
}
最后这里是客户端应该发出和读取琴弦的部分:
public void paintGame() {
String s = "p";
String[] c;
String d = "_";
try {
out5 = new PrintWriter(socket.getOutputStream(), true);
in5 = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out5.println(s);
} catch (IOException e) {
e.printStackTrace();
}
while (s != null) {
try {
System.out.println(s);
s = in5.readLine();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("Separating string!");
if (s != null) {
c = s.split(d);
for (int i = 0; i < c.length; i++) {
System.out.println(c[i]);
cPaint.add(c[i]);
}
System.out.println("Strings separated!");
} else {
System.out.println("ERROR! Null string");
}
}
我大多不知道关于最后一部分。我实际上不知道其他客户端如何获取发送和读取的字符串,因为没有客户端持续发送/读取字符串(如果按下按钮,它们只发送字符串)。
编辑:抱歉关于奇怪的格式._。
你能详细点吗?服务器或游戏中哪一个是可观察的(主题)? – ixe013
实际上,我在客户端的构造函数中发现了一个涉及while循环的不同方式,它不断从服务器读取数据。不管怎么说,还是要谢谢你! :) – user1748578