-
Notifications
You must be signed in to change notification settings - Fork 5
/
Start.java
82 lines (73 loc) · 1.92 KB
/
Start.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
public class Start extends Frame implements ActionListener {
private static final long serialVersionUID = 0L;
private TextField server;
private String host;
private int port;
private Button c, start, stop;
private Server s = null;
public Start() {
super("Tic-Tac-Toe");
setLayout(new GridLayout(0,2));
c = new Button("Start Client");
c.addActionListener(this);
add(c);
server = new TextField("localhost:31137");
add(server);
start = new Button("Start Server");
start.addActionListener(this);
add(start);
stop = new Button("Stop Server");
stop.addActionListener(this);
add(stop);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing (WindowEvent e) {
System.exit(0);
}
});
setSize(200, 200);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((int)(Math.random()*(dim.getHeight()-200)), (int)(Math.random()*(dim.getHeight()-200)));
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == c) {
try {
parseHost();
new Client(host, port);
} catch (IOException io) {
new MessageBox(getTitle(), "Error connecting to Server: " + io.getMessage());
}
} else if (e.getSource() == start) {
if (s != null)
s.interrupt();
parseHost();
s = new Server(port);
s.start();
} else if (e.getSource() == stop) {
if (s != null)
s.interrupt();
s = null;
}
}
public void parseHost() {
String[] socket = server.getText().split(":");
host = socket[0];
if (socket.length > 1) {
try {
int port = Integer.parseInt(socket[1]);
this.port = (port >= 1024 && port < 0xFFFF) ? port : Protocol.DEFAULTPORT;
} catch (NumberFormatException e) {
this.port = Protocol.DEFAULTPORT;
}
} else {
this.port = Protocol.DEFAULTPORT;
}
}
public static void main(String[] args) {
new Start();
}
}