77范文网 - 专业文章范例文档资料分享平台

网络编程五子棋设计报告(4)

来源:网络收集 时间:2020-04-18 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

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页

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库网络编程五子棋设计报告(4)在线全文阅读。

网络编程五子棋设计报告(4).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/989701.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: