-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
147 lines (106 loc) · 3.03 KB
/
Server.java
File metadata and controls
147 lines (106 loc) · 3.03 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package chessGame;
import java.io.*;
import java.net.*;
import java.nio.ByteBuffer;
public class Server {
public ServerSocket srvSock = null;
private Socket sock1, sock2 = null;
private int port;
int client1, client2;
private boolean twoClients;
public DataInputStream in1, in2 = null;
public DataOutputStream out1, out2 = null;
String move1, move2;
public Server(int port, int PlayerCount) {
this.port = port;
if (PlayerCount == 2) {
twoClients = true;
}
else {
twoClients = false;
}
}
public void run() throws IOException {
srvSock = new ServerSocket(port);
if (twoClients) {
System.out.print("Waiting for two connections...\n");
Socket sock1 = srvSock.accept();
System.out.print("Client 1 connected\n");
in1 = new DataInputStream(new BufferedInputStream(sock1.getInputStream()));
out1 = new DataOutputStream(new BufferedOutputStream(sock1.getOutputStream()));
//write player color to connected client1
out1.writeUTF("0");
out1.flush();
Socket sock2 = srvSock.accept();
System.out.print("Client 2 connected\n");
in2 = new DataInputStream(new BufferedInputStream(sock2.getInputStream()));
out2 = new DataOutputStream(new BufferedOutputStream(sock2.getOutputStream()));
//write player color to connected client2
out2.writeUTF("1");
out2.flush();
}
else {
System.out.print("Waiting for client connection...\n");
Socket sock1 = srvSock.accept();
System.out.print("Client connected\n");
in1 = new DataInputStream(new BufferedInputStream(sock1.getInputStream()));
out1 = new DataOutputStream(new BufferedOutputStream(sock1.getOutputStream()));
}
out1.writeUTF("WELCOM");
out2.writeUTF("WELCOME");
out1.flush();
out2.flush();
System.out.print("WELCOME\n");
}
public void ServerRead(int client) throws IOException {
if(client == 1) {
move1 = in1.readUTF();
}
else {
move2 = in2.readUTF();
}
}
public void ServerWrite(int client, String move) {
if(client == 1) {
try {
out1.writeUTF(move);
out1.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else {
try {
out2.writeUTF(move);
out2.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void close() {
try {
srvSock.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
Server srv = new Server(111, 2);
try {
srv.run();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
srv.ServerWrite(1, "Test1");
srv.ServerWrite(2, "Test2");
srv.ServerWrite(1, "More tests1");
srv.ServerWrite(2, "More tests2");
srv.ServerWrite(1, "quit");
srv.ServerWrite(2, "quit");
}
}