游戏类
public class Game {
private Integer target;
private Integer out = -1;
public synchronized Integer getOut() {
return out;
}
public synchronized Integer Result(Integer input) {
return (target - input);
}
public Game() {
this.target = (int)(Math.random() * 100);
System.out.println(target);
}
public synchronized void setOut(Integer out) {
this.out = out;
}
}
工具类
import java.io.BufferedInputStream;
import java.io.IOException;
public class Tools {
public Integer getMessage(BufferedInputStream buf) throws Exception {
return Integer.parseInt(this.getStringMessage(buf));
}
public String getStringMessage(BufferedInputStream buf) throws IOException {
int len = 0;
int i;
String dd = "";
boolean flag = true;
byte[] d = new byte[1024];
while(flag && (len = buf.read(d)) != -1 ) {
for(i=0;i<d.length;i++) {
if(d[i] == '*') {
flag = false;
break;
}
}
dd += new String(d,0,i);
//System.out.print(dd);
}
return dd;
}
}
客户端代码
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost",12138);
OutputStream outputStream = socket.getOutputStream();
InputStream inputStream = socket.g