《局域网五子棋网络课程设计报告》由会员分享,可在线阅读,更多相关《局域网五子棋网络课程设计报告(15页珍藏版)》请在金锄头文库上搜索。
1、辽 宁 科 技 大 学课 程 设 计 说 明 书设计题目: 局域网五子棋设计 学院、系: 计算机科学与工程学院 专业班级: 计试点 2005 学生姓名: 王 红 辉 指导教师: 周 传 胜 成 绩: 2009年 3 月 5 日 1目录一 题目1二 设计的任务、目的和作用1三 课程设计的内容1四 开发环境及实现所用语言1五 实现要点1A 数据通信指令(协议)及各指令的处理内容1B 套接字使用方法51.服务器端套接字的实现 52.客户端套接字的实现 6C 棋盘及棋子画法6D 棋盘存储结构及胜负判定算法8E 程序实现中定义的其他重要数据及意义、定义的函数或过程首部及意义12六 设计体会13 2一、题
2、目计算机网络课程设计局域网五子棋的实现二、设计的任务、目的和作用计算机网络课程设计是计算机网络原理课程之后的时间加强环节,通过本设计的训练使学生加深对网络协议的理解,并实现制定通信规则,同时通过实践,进一步掌握套接字编程方法,为大型网络编程打下坚实基础。三、课程设计的内容五子棋的局域网对战四、开发环境及实现所用语言开发环境:Microsoft Visual C+ 6.0语言: C+ 五、实现要点A、数据通信指令(协议)及各指令的处理内容客户与服务器采用 TCP 连接指令及处理:#define LOGINSERVER login /连接服务器#define LEAVESERVER leave /
3、离开服务器#define REPEATENAME rpeat /名字重复#define SERVERCLOSE svrcl /服务器关闭#define GETCLIENTIP getip /获取客户的 IP#define SENDCLIENTIP sndip /发送客户的 IP#define IPINFOMATION #IP:# /发送 IP 的信息#define MSGTYPELENGTH 5 /关键字长度enum LOGINSVR, GCLIENTIP, SCLIENTIP, LEAVESVR ; /进入服务器,取客户的 IP,发送客户的 IP,离开服务器客户发送数据给服务器void CG
4、obangGameClientDlg:SendMessageToServer(unsigned int nMsgType /* = LOGINSVR */, CString strMsg /* = */)CString strSendMsg;switch( nMsgType )case LOGINSVR: strSendMsg = LOGINSERVER + strMsg;break;case GCLIENTIP:strSendMsg = GETCLIENTIP + strMsg;break; 3case SCLIENTIP:strSendMsg = SENDCLIENTIP + strMsg
5、;break;case LEAVESVR:strSendMsg = LEAVESERVER + strMsg;break;default:break;m_pClientSocket-Send( strSendMsg, strSendMsg.GetLength();服务器端处理收到的数据void CGobangGameDlg:ProcessPendingRead(CClientSocket *pClientSocket)/定义缓冲区char bufferBUFFER_SIZE;/接收数据int nReceived = pClientSocket-Receive(buffer,BUFFER_SIZ
6、E,0);buffernReceived = 0;CString strReceived;strReceived.Format(%s, buffer);CString strMsgType = strReceived.Left( MSGTYPELENGTH );CString strMsgInfo = strReceived.Right( strReceived.GetLength() - MSGTYPELENGTH ) ;if( strMsgType = LOGINSERVER )if( isRepeatName( strMsgInfo ) = FALSE )ClientLoginServe
7、r( strMsgInfo , pClientSocket);UpdateClients(buffer, nReceived, pClientSocket);elsestrReceived = REPEATENAME;pClientSocket-Send(strReceived, strReceived.GetLength();else if( strMsgType = GETCLIENTIP)GetClientIP( strMsgInfo, pClientSocket ); 4else if( strMsgType = SENDCLIENTIP)SendClientIP( strMsgInf
8、o, pClientSocket );else if( strMsgType = LEAVESERVER )ClientLeaveServer( strMsgInfo, pClientSocket);UpdateClients(buffer, nReceived, pClientSocket);/从服务器收到数据的处理void CGobangGameClientDlg:ReceiveMessageFromServer()char bufferBUFFER_SIZE;unsigned int nReceive = m_pClientSocket-Receive(buffer, BUFFER_SI
9、ZE);buffernReceive = 0;CString strReceived;strReceived.Format(%s, buffer);CString strMsgType = strReceived.Left( MSGTYPELENGTH );CString strMsgInfo = strReceived.Right( strReceived.GetLength() - MSGTYPELENGTH ) ;if( strMsgType = LOGINSERVER )ClientLoginServer( strMsgInfo );else if( strMsgType = GETC
10、LIENTIP)GetClientIP( strMsgInfo );else if( strMsgType = SENDCLIENTIP)SendClientIP( strMsgInfo );else if( strMsgType = LEAVESERVER )ClientLeaveServer( strMsgInfo );else if( strMsgType = REPEATNAME )RepeatName();客户与客户采用 UDP 形式进行聊天,下棋 5客户与客户采用 TCP 连接指令及处理:#define SENDCHESSINFO chess / 发送下棋的棋子信息#define
11、SENDCHATINFO chati /发送聊天信息#define SENDCHESSLOSS closs /发送给别人我赢得了游戏#define SENDLEAVE leave /发送离开游戏的消息#define CRYCRAVEN ccven /发送认输申请#define REPENTANCE reptc /发送悔棋申请#define AGREECRYCRAVEN acven /发送对方回复的认输申请#define AGRESSREPENTANCE arept /发送对方回复的悔棋申请#define MSGTYPELENGTH 5 /关键字长度enum CHESSINFO, CHATINF
12、O, CHESSLOSS, LEAVECHESS, CRYCVN, REPTC, ACRYCVN, AREPTC ;/棋子信息, 聊天信息, 别人赢得游戏 , 离开游戏, 认输申请, 悔棋申请, 回复的认输申请, 回复的悔棋申请.发送信息给对手void CDlgGobang:SendMessageToOther(unsigned int nMsgType, CString strMsgInfo)switch( nMsgType )case CHATINFO:strMsgInfo = SENDCHATINFO + strMsgInfo;break;case CHESSINFO:strMsgInf
13、o = SENDCHESSINFO + strMsgInfo;break;case CHESSLOSS:strMsgInfo = SENDCHESSLOSS + strMsgInfo;break;case LEAVECHESS:strMsgInfo = SENDLEAVE + strMsgInfo;break;case CRYCVN:strMsgInfo = CRYCRAVEN + strMsgInfo;break;case REPTC:strMsgInfo = REPENTANCE + strMsgInfo;break;case ACRYCVN:strMsgInfo = AGREECRYCR
14、AVEN + strMsgInfo;break;case AREPTC:strMsgInfo = AGRESSREPENTANCE + strMsgInfo;break;default:break;m_pChatSocket-SendTo( strMsgInfo, strMsgInfo.GetLength(), m_nOtherPort, m_strOtherIPAddress );分析对方发过来的信息void CDlgGobang:ProcessPendingRead(CChatSocket* pChatSocket)/定义缓冲区char bufferBUFFER_SIZE;/接收数据int nReceived = pChatSocket-Receive(buffer,BUFFER_SIZE,0);buffernReceived = 0;CString strReceived;strReceived.Format(%s, buffer);CString strMsgType = strReceived.Left( MSGTYPELENGTH ); 6CString strMsgInfo = strReceived.Righ