我制作的游戏涉及两个客户端,一个托管服务器,另一个托管服务器,然后他们可以一起玩。在我的代码中,我有一个客户端使用serversocket,另一个客户端使用套接字,然后serversocket通过serversocket.accept()
得到一个套接字。我的问题是,每当我使用PrintWriter.println(...)
时,其他客户端都不会收到该消息。我不确定它是在我发送消息时还是在收到消息时。这是主要的类:(我已经删除了大部分的GUI东西)
public class Base extends JPanel {
PrintWriter pw;
JButton host = new JButton("Host New Game");
JButton join = new JButton("Join Existing Game");
JButton ready = new JButton("Ready");
Base b = this;
String ip;
String port;
String message;
boolean otherready;
int textip = 0;
Socket s;
int count = 0;
public Base () {
Handler handler = new Handler();
host.addActionListener(handler);
join.addActionListener(handler);
text.addActionListener(handler);
}
public void notifyOfPlayer () {
game = true;
setUpGame();
}
public void sendMessage (String message) {
try {
if (!doneonce) {
pw = new PrintWriter(s.getOutputStream());
doneonce = true;
}
pw.println(message);
pw.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
public void setPlayer (Socket player) {
s = player;
}
private class Handler implements ActionListener, MouseListener, MouseMotionListener {
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == host) {
remove(host);
remove(join);
text.setText("Server Mode, Waiting for player");
text.setSize((int)text.getPreferredSize().getWidth(), 25);
text.setLocation((int)((Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2) - (text.getPreferredSize().getWidth() / 2)), 200);
Thread t = new Thread(new ServerMode(b));
t.start();
repaint();
} else if (e.getSource() == join) {
remove(host);
remove(join);
text.setText("Enter IP");
textip = 1;
/*text.setSize((int)text.getPreferredSize().getWidth(), 25);
text.setLocation((int)((Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2) - (text.getPreferredSize().getWidth() / 2)), 200);
*/text.setEditable(true);
repaint();
} else if (e.getSource() == text) {
if (textip == 1) {
textip ++;
ip = text.getText();
text.setText("Enter Port");
/*text.setSize((int)text.getPreferredSize().getWidth(), 25);
text.setLocation((int)((Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2) - (text.getPreferredSize().getWidth() / 2)), 200);
*/
} else {
textip = 0;
port = text.getText();
text.setText("Joining Game...");
text.setSize((int)text.getPreferredSize().getWidth(), 25);
text.setLocation((int)((Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2) - (text.getPreferredSize().getWidth() / 2)), 200);
try {
s = new Socket(ip, Integer.parseInt(port));
text.setText("Game Joined");
remove(text);
game = true;
setUpGame();
repaint();
Thread t = new Thread(new RecieveHandler(b, s));
t.start();
} catch (Exception e1) {
text.setText("Could not connect");
text.setSize((int)text.getPreferredSize().getWidth(), 25);
text.setLocation((int)((Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2) - (text.getPreferredSize().getWidth() / 2)), 200);
text.setEditable(false);
e1.printStackTrace();
}
}
} else if (e.getSource() == ready) {
sendMessage("ready");
}
}
}
public void setOtherReady () {
otherready = true;
JOptionPane.showMessageDialog(null, "Your opponent is ready!", "Ready!", JOptionPane.INFORMATION_MESSAGE);
}
}
这是我的RecieveHandler:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
public class RecieveHandler implements Runnable {
Base b;
Socket s;
BufferedReader r;
public RecieveHandler(Base base, Socket socket) {
try {
b = base;
s = socket;
r = new BufferedReader(new InputStreamReader(s.getInputStream()));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
while (true) {
String message = r.readLine();
System.out.println("Recieved message: " + message);
if (message.equals("ready")) {
b.setOtherReady();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
这是ServerMode:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerMode implements Runnable {
ServerSocket ss;
Base b;
PrintWriter pw;
BufferedReader r;
public ServerMode (Base base) {
b = base;
}
@Override
public void run() {
try {
ss = new ServerSocket(3602);
while (true) {
Socket s = ss.accept();
pw = new PrintWriter(s.getOutputStream());
pw.println("HI!!!");
InputStreamReader isr = new InputStreamReader(s.getInputStream());
r = new BufferedReader(isr);
b.notifyOfPlayer();
b.setPlayer(s);
Thread t = new Thread(new RecieveHandler(b, s));
t.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
这两个客户的代码相同。
我做错了什么?
答案 0 :(得分:0)
我对代码进行了彻底的彻底检查,发现发送邮件的按钮没有附加.addActionListener(...)
。哎呀:|