-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetClient.java
51 lines (48 loc) · 1.58 KB
/
NetClient.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
import java.net.*;
import javafx.scene.text.Text;
import javafx.application.Platform;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.OutputStream;
public class NetClient extends Thread{
private Main parent;
public NetClient(Main _parent){
this.parent = _parent;
}
public OutputStream op;
public void NetMakeMove(int x, int y, int x1, int y1){
byte[] output = {(byte)x,(byte)y,(byte)x1,(byte)y1};
parent.chessBoard.movesEnabled = false;
try{
op.write(output);
}
catch(Exception e){
System.out.println(e);
}
}
public void run(){
int port = 4444;
try{
Platform.runLater(() -> parent.UITimeout(false));
String ip = parent.ip.getText();
Socket socket = new Socket(ip,port);
op = socket.getOutputStream();
DataInputStream in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
op.write(0x02);
Platform.runLater(() -> parent.UITimeout(true));
Platform.runLater(() -> {parent.chessBoard.movesEnabled = false;});
System.out.println(ip);
parent.chessBoard.nc = this;
while(true){
byte[] buffer = new byte[4];
if(in.read(buffer,0,4) != 4)
continue;
Platform.runLater(() -> parent.chessBoard.NetMovePiece(buffer));
}
}
catch(Exception e){
Platform.runLater(() -> parent.UITimeout(true));
System.out.println(e);
}
}
}