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. 设计并实现一对一网络版小游戏,本次课程设计实现的是五子棋。
资源详情
资源评论
资源推荐
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/DOC.png)
共 32 条
- 1
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![avatar](https://profile-avatar.csdnimg.cn/33a9216094ce454a9e0573e6be5ab5f4_x1135299049.jpg!1)
xlily508
- 粉丝: 2
- 资源: 5
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
- 自用111111111111111
- Node+Express+MongoDB+Mongoose打造属于自己的个人博客系统,前后端分离
- VS-QT开发问题手册...开发中遇到的qt配置问题
- 凯撒java版SaaS-OA协同办公软件.zip
- 简易java开源订销管理系统.zip
- Java网络爬虫(蜘蛛)源码.zip
- 基于STM32的人脸识别系统设计与实现(正点原子STM32F4探索者开发板)
- CGAL3.8手册 CGAL - Computational Geometry Algorithms Library (计算几何
- Lealfet整合geoman开发WebGIS自由标绘实战
- 539416676075354base.apk
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)
评论0