public GameThread(GamePad currPad) {
this.currPad = currPad; }
// 处理取得的信息
public void dealWithMsg(String msgReceived) {
if (msgReceived.startsWith(\)) { // 收到的信息为下棋
StringTokenizer userMsgToken = new StringTokenizer(msgReceived, \);
// 表示棋子信息的数组、0索引为:x坐标;1索引位:y坐标;2索引位:棋子颜色
String[] chessInfo = { \, \, \ }; int i = 0; // 标志位 String chessInfoToken;
while (userMsgToken.hasMoreTokens()) {
chessInfoToken = (String) userMsgToken.nextToken(\); if (i >= 1 && i <= 3) {
chessInfo[i - 1] = chessInfoToken; } i++; }
currPad.paintNetFirPoint(Integer.parseInt(chessInfo[0]), Integer
.parseInt(chessInfo[1]), Integer.parseInt(chessInfo[2])); }
else if (msgReceived.startsWith(\)) { // 收到的信息为改名
currPad.chessSelfName = msgReceived.substring(10); }
else if (msgReceived.equals(\)) { // 收到的为错误信息
currPad.statusText.setText(\用户不存在,请重新加入!\); } }
// 发送信息
public void sendMessage(String sndMessage) {
第 12 页 共 4页
}
}
try {
currPad.outputData.writeUTF(sndMessage); }
catch (Exception ea) {
ea.printStackTrace();; }
public void run() {
String msgReceived = \; try {
while (true)
{ // 等待信息输入
msgReceived = currPad.inputData.readUTF(); dealWithMsg(msgReceived); } }
catch (IOException es){} }
3.2.6服务器信息面板
用于提供各种信息的显示,用于显示服务器状态和信息输出. public class ServerMsgPanel extends Panel {
public TextArea msgTextArea = new TextArea(\, 22, 50, TextArea.SCROLLBARS_VERTICAL_ONLY);
public JLabel statusLabel = new JLabel(\当前连接数:\, Label.LEFT); public Panel msgPanel = new Panel(); public Panel statusPanel = new Panel(); public ServerMsgPanel() {
setSize(350, 300);
setBackground(Color.LIGHT_GRAY); setLayout(new BorderLayout());
msgPanel.setLayout(new FlowLayout()); msgPanel.setSize(210, 210);
statusPanel.setLayout(new BorderLayout()); statusPanel.setSize(210, 50);
第 13 页 共 4页
}
}
msgPanel.add(msgTextArea);
statusPanel.add(statusLabel, BorderLayout.WEST); add(msgPanel, BorderLayout.CENTER); add(statusPanel, BorderLayout.NORTH);
3.2.7 服务器线程
为支持多组用户同时进行网络对弈,需要为服务器添加多线程的功能。其中包含三个hash表,clientDataHash用于保存客户端套接字与输出流对应,其中套接口为key,输出流为value,clientNameHash为客户端套接口和客户名的对应,其中套接口为Key,客户名为value。Chesspeerhash用于保存游戏创建者和游戏加入者的对应,游戏创建者为key,游戏加入者为value。
Feedback方法用于发送反馈信息给当前线程链接到主机的用户,首先从clientDataHash中取得当前的套接口和数据输出流,接着调用writeUtf方法输出。 public void Feedback(String feedBackMsg) {
synchronized (clientDataHash) {
DataOutputStream outputData = (DataOutputStream) clientDataHash
.get(clientSocket); try {
outputData.writeUTF(feedBackMsg); }
catch (Exception eb) {
eb.printStackTrace(); } } }
后面还包括像所有用户发送信息,向指定用户发送所有信息的方法
3.2.8 服务器端
服务器端用于生成服务器的界面,创建服务器以及初始化服务器线程中的三个哈希表,其中包含main方法,是服务器程序启动的入口。 // 用指定端口和面板创建服务器
public void createServer(int port, ServerMsgPanel serverMsgPanel) throws IOException {
第 14 页 共 4页
Socket clientSocket; // 客户端套接口
long clientAccessNumber = 1; // 连接到主机的客户数量 this.serverMsgPanel = serverMsgPanel; // 设定当前主机 try {
serverSocket = new ServerSocket(port);
serverMsgPanel.msgTextArea.setText(\服务器启动于:\ + InetAddress.getLocalHost() + \ //djr + serverSocket.getLocalPort() + \); while (true) {
// 监听客户端套接口的信息
clientSocket = serverSocket.accept();
serverMsgPanel.msgTextArea.append(\已连接用户:\ + clientSocket + \);
// 建立客户端输出流
DataOutputStream outputData = new DataOutputStream(clientSocket
.getOutputStream()); // 将客户端套接口和输出流绑定
clientDataHash.put(clientSocket, outputData); // 将客户端套接口和客户名绑定 clientNameHash
.put(clientSocket, (\新玩家\ + clientAccessNumber++)); // 创建并运行服务器端线程
GameServerThread thread = new GameServerThread(clientSocket,
clientDataHash, clientNameHash, chessPeerHash, serverMsgPanel);
thread.start(); } }
catch (IOException ex) {
ex.printStackTrace(); } }
public void actionPerformed(ActionEvent e) {
if (e.getSource() == clearMsgButton) { // 清空服务器信息
serverMsgPanel.msgTextArea.setText(\); }
第 15 页 共 4页
}
if (e.getSource() == serverStatusButton) { // 显示服务器信息 try {
serverMsgPanel.msgTextArea.append(\服务器信息:\ + InetAddress.getLocalHost() + \
+ serverSocket.getLocalPort() + \); }
catch (Exception ee) {
ee.printStackTrace(); } }
if (e.getSource() == closeServerButton) { // 关闭服务器
System.exit(0); }
3.2.9 客户端线程
因为是支持多组用户,所以要开发客户端的多线程,其原理类似于服务器的多线程,线程启动后将调用客户端的readUTF方法进行监听,病进入等待状态,当取得消息就调用dealWithMSg进行处理。
public void dealWithMsg(String msgReceived) {
if (msgReceived.startsWith(\)) { // 若取得的信息为用户列表 StringTokenizer userToken = new StringTokenizer(msgReceived, \);
int userNumber = 0; // 清空客户端用户列表
firClient.userListPad.userList.removeAll(); // 清空客户端用户下拉框
while (userToken.hasMoreTokens())
{ // 当收到的用户信息列表中存在数据时
String user = (String) userToken.nextToken(\); // 取得用户信息
if (userNumber > 0 && !user.startsWith(\)) { // 用户信息有效时
firClient.userListPad.userList.add(user);// 将用户信息添加到用户列表中 }
userNumber++;
第 16 页 共 4页