我正在创建一个国际象棋游戏,这就是我的服务器的样子:
public class Server {
public static void main(String[] args) throws IOException {
int port = 4444;
// server is listening on port 4444
ServerSocket ss = new ServerSocket(port);
// running infinite loop for getting
// client request
while (true) {
Socket s = null;
try {
// socket object to receive incoming client requests
s = ss.accept();
System.out.println("A new client is connected : " + s);
System.out.println("Assigning new thread for this client");
// create a new thread object
Thread t = new ClientHandler(s);
// Invoking the start() method
t.start();
}
catch (Exception e) {
s.close();
e.printStackTrace();
}
}
}
}
和ClientHandler
如下:
public class ClientHandler extends Thread {
protected Socket socket;
public ClientHandler(Socket clientSocket) {
this.socket = clientSocket;
}
public void run() {
InputStream inp = null;
BufferedReader brinp = null;
DataOutputStream out = null;
try {
inp = socket.getInputStream();
brinp = new BufferedReader(new InputStreamReader(inp));
out = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
return;
}
String line;
while (true) {
try {
line = brinp.readLine();
if ((line == null)) {
socket.close();
return;
} else {
System.out.println(line);
out.writeBytes(line + "\n");
out.flush();
}
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
}
我想做的是有两个客户端连接到服务器,因此它们可以收发棋盘上相互对应的棋子,这是象棋游戏的标准,这是发送数据的类:>
private class Sender extends AsyncTask<Void, Void, Void> {
@Override
protected void onPostExecute(Void aVoid) {
final Receiver messageReceiver = new Receiver();
messageReceiver.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
@Override
protected Void doInBackground(Void... params) {
try {
client = new Socket(CHAT_SERVER_IP, 4444); // Creating the server socket.
printwriter = new PrintWriter(client.getOutputStream(), true);
printwriter.write(message + "\n");
printwriter.flush();
} catch (Exception e) {
//Toast.makeText(SecondActivity.getAppContext(), "ERROR", Toast.LENGTH_LONG).show();
Log.d(": ", "SERVER ERROR");
}
return null;
}
}
以下是接收数据的类:
private class Receiver extends AsyncTask<Void, Void, Void> {
@Override
protected void onPostExecute(Void aVoid) {
boardView.removeAllViews();
renderBoard();
}
@Override
protected Void doInBackground(Void... params) {
/* RECEIVED MESSAGE */
try {
InputStreamReader inputStreamReader = new InputStreamReader(client.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader);
InputStream inputStream = new ByteArrayInputStream(bufferedReader.readLine().getBytes(Charset.forName("UTF-8")));
BufferedInputStream bis = new BufferedInputStream(inputStream);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result = bis.read();
while (result != -1) {
buf.write((byte) result);
result = bis.read();
}
// StandardCharsets.UTF_8.name() > JDK 7
String message = buf.toString("UTF-8");
Log.d("SERVER_MESSAGE: ", message);
parseMessageRedrawBoard(message);
} catch (Exception ex) {
Log.d(": ", "Problem in message reading");
}
return null;
}
}
我该如何调整它们,以便它们可以创建相互连接并交换信息,以便重新绘制/渲染游戏状态?