-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
98 lines (74 loc) · 1.9 KB
/
Client.java
File metadata and controls
98 lines (74 loc) · 1.9 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
package chessGame;
import java.io.*;
import java.net.*;
public class Client {
private String userName;
private String ipAddress;
private int port;
public Socket sock = null;
private DataInputStream in;
private DataOutputStream out;
int color;
public RemoteBoard board;
public void setColor(int color) {
this.color = color;
}
public Client(String userName, String ipAddress, int port) {
this.userName = userName;
this.ipAddress = ipAddress;
this.port = port;
//board = new RemoteBoard(color);
}
public void run() throws IOException {
sock = new Socket(ipAddress, port);
in = new DataInputStream(new BufferedInputStream(sock.getInputStream()));
out = new DataOutputStream(new BufferedOutputStream(sock.getOutputStream()));
}
public int dataAvailable() throws IOException{
return in.available();
}
public String ClientRead() {
try {
return in.readUTF();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
}
public void ClientWrite(String move) {
try {
out.writeUTF(move);
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void close() {
try {
sock.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
String test = "";
Client c1 = new Client("Client1", "127.0.0.1", 111);
//Client c2 = new Client("Client2", "127.0.0.1", 111);
try {
c1.run();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(!test.equals("quit")) {
System.out.print("Client reading..\n");
test = c1.ClientRead();
System.out.print(test);
System.out.print("\n");
}
c1.close();
}
}