Til inspiration:
package gamesim;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class Server {
private int expected;
private List<Client> cli = new ArrayList<Client>();
private int curr;
public Server(int expected) {
this.expected = expected;
}
public void runGame() {
try {
ServerSocket ss = new ServerSocket(12345);
while(cli.size() < expected) {
Socket s = ss.accept();
cli.add(new Client(s));
}
curr = 0;
updateState();
} catch (IOException ex) {
ex.printStackTrace();
}
}
private void updateState() {
cli.get(curr).send("Your turn");
}
private void next() {
curr = (curr + 1) % cli.size();
updateState();
}
public static void main(String[] args) {
Server srv = new Server(2);
srv.runGame();
}
public class Client {
private PrintWriter pw;
private BufferedReader br;
public Client(Socket s) {
try {
pw = new PrintWriter(s.getOutputStream());
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
(new ReadFromClient()).start();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void send(String msg) {
pw.println(msg);
pw.flush();
}
public class ReadFromClient extends Thread {
public void run() {
try {
String line;
while((line = br.readLine()) != null) {
switch(line) {
case "Move" : next();
break;
default: // ignore
break;
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
}
package gamesim;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Client extends JFrame {
private static final long serialVersionUID = 1L;
private JButton move;
private boolean myturn;
Socket s;
private PrintWriter pw;
private BufferedReader br;
public Client() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Client");
getContentPane().setLayout(new BorderLayout());
move = new JButton("Move");
myturn = false;
updateState();
move.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pw.println("Move");
pw.flush();
myturn = false;
updateState();
}
});
getContentPane().add(move, BorderLayout.CENTER);
pack();
try {
s = new Socket("localhost", 12345);
pw = new PrintWriter(s.getOutputStream());
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
(new ReadFromServer()).start();
} catch (IOException ex) {
ex.printStackTrace();
}
}
private void updateState() {
move.setEnabled(myturn);
move.setBackground(myturn ? Color.GREEN : Color.RED);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame f = new Client();
f.setVisible(true);
}
});
}
public class ReadFromServer extends Thread {
public void run() {
try {
String line;
while((line = br.readLine()) != null) {
switch(line) {
case "Your turn" : myturn = true;
updateState();
break;
default: // ignore
break;
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}