package server;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.StringTokenizer;
import server.GameServer.ServerMsgPanel;
public class ServerThread extends Thread{
Socket clientSocket;
Hashtable<Socket, DataOutputStream> clientDataHash=new Hashtable(50);//将客户端套接口和输出流绑定
Hashtable<Socket, String> clientNameHash=new Hashtable(50);//将客户端套接口和客户名绑定
Hashtable<String, String> chessPeerHash=new Hashtable(50);//将游戏创建者和游戏加入者绑定 ,要是无匹配者值为wait
ServerMsgPanel serverMsgPanel;
boolean isClientClosed=false;
public ServerThread(Socket clientSocket, Hashtable clientDataHash, Hashtable clientNameHash,
Hashtable chessPeerHash, ServerMsgPanel serverMsgPanel) {
// TODO Auto-generated constructor stub
this.chessPeerHash=chessPeerHash;
this.clientDataHash=clientDataHash;
this.clientNameHash=clientNameHash;
this.serverMsgPanel=serverMsgPanel;
this.clientSocket=clientSocket;
}
// 处理客户端发来的消息
public void dealWithMsg(String msgReceived) {
String clientName=clientNameHash.get(clientSocket);
String peerName;
if(msgReceived.startsWith("/")) {
// 收到的信息为更新用户列表
if(msgReceived.equals("/list")) {
serverMsgPanel.msgTextArea.append(clientName+"的信息为更新用户列表 \n");
Feedback(getUserList());
}
// 收到的信息为创建游戏
else if(msgReceived.startsWith("/creategame [inchess]")) {
String gameCreaterName=msgReceived.substring(msgReceived.indexOf("]")+1);
serverMsgPanel.msgTextArea.append(gameCreaterName+"的信息为创建游戏 \n");
synchronized (clientNameHash) {
// 将用户端口放到用户列表中
clientNameHash.put(clientSocket, msgReceived.substring(msgReceived.indexOf(" ")+1));
}
synchronized (chessPeerHash) {
// 将主机设置为等待状态
chessPeerHash.put(gameCreaterName, "wait");
}
sendGamePeerMsg(gameCreaterName,"/OK");
sendPublicMsg(getUserList());
}
// 收到的信息为放弃创建游戏
else if(msgReceived.startsWith("/giveupcreategame ")) {
serverMsgPanel.msgTextArea.append(clientName+"的信息为放弃创建游戏 \n");
String createName=msgReceived.substring(msgReceived.indexOf(" ")+1);
closeClient();
sendGamePeerMsg(createName,"/giveupcreategame");
}
// 收到的信息为加入游戏时
else if(msgReceived.startsWith("/joingame ")) {
StringTokenizer userTokens=new StringTokenizer(msgReceived," ");
String userToken;
String gameCreatorName;
String gamePaticipantName;
String[]playerNames={"0","0"};
int nameIndex=0;
while(userTokens.hasMoreTokens()) {
userToken=(String)userTokens.nextToken(" ");
if(nameIndex>=1&&nameIndex<=2) {
// 取得游戏者命
playerNames[nameIndex-1]=userToken;
}
nameIndex++;
}
gameCreatorName=playerNames[0];
gamePaticipantName=playerNames[1];
serverMsgPanel.msgTextArea.append(clientName+"的信息为加入游戏 创建者"+gameCreatorName+" 加入者"+gamePaticipantName+"\n");
if(chessPeerHash.containsKey(gameCreatorName)&&chessPeerHash.get(gameCreatorName).equals("wait")) {
// 游戏已创建
synchronized (clientNameHash) {
clientNameHash.put(clientSocket, "[inchess]"+gamePaticipantName);
}
synchronized (chessPeerHash) {
// 增加或修改游戏创建者与游戏加入者的名称的对应
chessPeerHash.put(gameCreatorName, gamePaticipantName);
}
sendPublicMsg(getUserList());
// 发送信息给游戏加入者
sendGamePeerMsg(gamePaticipantName,"/peer "+"[inchess]"+gameCreatorName);
// 发送游戏给游戏创建者
sendGamePeerMsg(gameCreatorName,"/peer "+"[inchess]"+gamePaticipantName);
}else {
// 若游戏未创建则拒绝加入游戏
sendGamePeerMsg(gamePaticipantName,"/reject");
closeClient();
}
}
// 收到的信息为游戏中时 ,处理棋盘下棋同步
else if(msgReceived.startsWith("/[inchess]")){
int firstLocation=0,lastLocation;
lastLocation=msgReceived.indexOf(" ",0);
peerName=msgReceived.substring(firstLocation+1,lastLocation);
msgReceived=msgReceived.substring(lastLocation+1);
serverMsgPanel.msgTextArea.append(peerName+"下棋中 \n");
if(sendGamePeerMsg(peerName,msgReceived)) {
Feedback("/error");
}
}
// 收到的信息为放弃游戏时
else if(msgReceived.startsWith("/giveup ")) {
serverMsgPanel.msgTextArea.append(clientName+"的信息为放弃游戏 \n");
String chessClientName=msgReceived.substring(8);
if(chessPeerHash.containsKey(chessClientName)&&!((String)chessPeerHash.get(chessClientName)).equals("wait")){
// 胜利方为游戏加入者,发送胜利信息
sendGamePeerMsg((String) chessPeerHash.get(chessClientName), "/youwin");
synchronized (chessPeerHash)
{ // 删除退出游戏的用户
chessPeerHash.remove(chessClientName);
}
}
if(chessPeerHash.containsValue(chessClientName)){
sendGamePeerMsg((String)getHashKey(chessPeerHash,chessClientName),"/youwin");
synchronized (chessPeerHash) {
chessPeerHash.remove((String)getHashKey(chessPeerHash,chessClientName));
}
}
}
// 收到的信息为其它命令时
else {
int lastLocation = msgReceived.indexOf(" ", 0);
if (lastLocation == -1) {
Feedback("无效命令");
return;
}
//目标用户聊天
peerName=msgReceived.substring(1, lastLocation);
String message=msgReceived.substring(lastLocation+1,msgReceived.length());
message=clientName+">"+peerName+" "+message;
serverMsgPanel.msgTextArea.append(clientName+"与"+peerName+"正在聊天"+"\n");
try {
DataOutputStream dataOutputStream=new DataOutputStream(clientSocket.getOutputStream());
dataOutputStream.writeUTF(message);
} catch (IOException e) {
}
sendGamePeerMsg(peerName, message);
serverMsgPanel.msgTextArea.setCaretPosition(serverMsgPanel.msgTextArea.getText().length());
}
}
else {
//公屏聊天
msgReceived=clientNameHash.get(clientSocket)+">"+msgReceived;
serverMsgPanel.msgTextArea.append(msgReceived+"\n");
sendPublicMsg(msgReceived);
serverMsgPanel.msgTextArea.setCaretPosition(serverMsgPanel.msgTextArea.getText().length());
}
}
private void closeClient() {
serverMsgPanel.msgTextArea.append("用户断开连接:" + clientSocket + "\n");
synchronized (chessPeerHash)
{ //如果是游戏客户端主机
if (chessPeerHash.containsKey(clientNameHash.get(clientSocket)))
{
chessPeerHash.remove((String) clientNameHash.get(clientSocket));
}
if (chessPeerHash.containsValue(clientNameHash.get(clientSocket)))
{
chessPeerHash.put((String) getHashKey(chessPeerHash,
(String) clientNameHash.get(clientSocket)),
"tobeclosed");
}
}
synchronized (clientDataHash)
{ // 删除客户数据
clientDataHash.remove(clientSocket);
}
synchronized (clientNameHash)
{ // 删除客户数据
clientNameHash.remove(clientSocket);
}
sendPublicMsg(getUserList());
serverMsgPanel.statusLabel.setText("当前连接数:" + clientDataHash.size());
try
{
clientSocket.close();
}
catch (IOException exx)
{
exx.printStackTrace();
}
isClientClosed = true;
}
public void sendPublicMsg(String publicMsg) {
synchronized (clientDataHash) {
for(Enumeration enu=clientDataHash.elements();enu.hasMoreElements();) {
DataOutputStream outputStream=(DataOutputStream)enu.nextElement();
try {
outputStream.writeUTF(publicMsg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private boolean sendGamePeerMsg(String gamePeerTarget, String gamePeerMsg) {
for(Enumeration enu=clientDataH
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
具体思路https://blog.csdn.net/x1135299049/article/details/122906221 一.【设计目的】 1. 熟悉开发工具(eclipse)的基本操作; 2. 掌握应用程序的编写过程; 3. 对于 Socket 编程建立初步的概念。 二.【设计要求】 1. 熟悉 Sock API 主要函数的使用; 2. 掌握相应开发工具对 Socket API 的封装; 3. 设计并实现一对一网络版小游戏,本次课程设计实现的是五子棋。
资源详情
资源评论
资源推荐
收起资源包目录
1.4计算机网络课设.zip (32个子文件)
源程序
webgame
src
client
ClientServer.java 8KB
ClientThread.java 3KB
server
ServerThread.java 10KB
GameServer.java 5KB
chess
PointBlack.java 379B
Pad.java 9KB
PointWhite.java 361B
PadThread.java 1KB
user
UserInputPanel.java 625B
UserListPanel.java 432B
UserChatPanel.java 660B
UserControlPanel.java 964B
bin
client
ClientServer.class 8KB
ClientThread.class 4KB
ClientServer$1.class 1KB
server
GameServer.class 4KB
GameServer$ServerMsgPanel.class 1KB
ServerThread.class 9KB
GameServer$1.class 656B
GameServer$ServerForClientThread.class 2KB
chess
PadThread.class 2KB
Pad.class 8KB
PointBlack.class 697B
PointWhite.class 697B
user
UserChatPanel.class 999B
UserControlPanel.class 1KB
UserListPanel.class 948B
UserInputPanel.class 1KB
.classpath 301B
.settings
org.eclipse.jdt.core.prefs 598B
.project 383B
《计算机网络课程设计》报告.doc 956KB
共 32 条
- 1
xlily508
- 粉丝: 2
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Swift语言教程:从基础语法到高级特性的全面讲解
- 常用工具合集(包括汉字转拼音工具、常用数据格式相互转换工具、尺寸相关的工具类).zip
- Delphi编程教程:从入门到精通Windows应用程序开发
- 视觉化编程入门指南:Visual Basic语言教程及其应用领域
- 纯代码实现的3d爱心.zip学习资料语言
- 儿童编程教育中Scratch语言的基础教学及实战示例
- 批量文件编码格式转换工具.zip学习资料
- 在不同操作系统下编译Android源码需要更改一些Android源码的配置项,脚本用于自动化更改配置项.zip
- 基于vue3的春节烟花许愿代码.zip学习资料
- Apache Kafka 的 Python 客户端.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0