出于教育目的,我尝试创建服务器和客户端,服务器从多个客户端接收数据并回显每条消息.问题是当我尝试让服务器立即向所有客户端发送回声时.
public class SocketServer {
ArrayList<MyRunnable> ts = new ArrayList<MyRunnable>();
ServerSocket serv;
static MainServerThread mst = new MainServerThread();
// ^ IDE(eclipse) underlines this as the problem
SocketServer() {
EventQueue.invokeLater(mst);
}
public static void main(String[] args) {
Thread tr = new Thread(mst);
tr.start();
}
void send(String s) {
for (int i = 0; i < ts.size(); i++) {
MyRunnable tmp = ts.get(i);
tmp.sendToClient(s);
}
}
class MainServerThread implements Runnable {
public void run() {
try {
serv = new ServerSocket(13131);
boolean …Run Code Online (Sandbox Code Playgroud) 我认为这是可能的,这是问题,也是stackoverflow,它确认了它.
但是我在尝试使它工作时失败了,所以我在寻求你的帮助.
我怎样才能做到这一点?