www.gusucode.com > VC++ 四国军旗网络游戏+毕业论文-源码程序 > VC++ 四国军旗网络游戏+毕业论文-源码程序/code/网络游戏-四国军棋/源程序/CServerFrame/Server.cpp
//Download by http://www.NewXing.com // Server.cpp: implementation of the CServer class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "CServerFrame.h" #include "CServerFrameView.h" #include "Server.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CServer::CServer() { } CServer::~CServer() { WSAAsyncSelect(m_hSocket,m_hWnd,0,0); } void CServer::ServerInit() { //设置socket的异步模式 if(WSAAsyncSelect(m_hSocket, m_hWnd, SER_MESSAGE, FD_ACCEPT|FD_READ|FD_WRITE|FD_CLOSE)>0) AfxMessageBox("error select"); } BOOL CServer::InitAndListen(HWND hWnd, UINT port) { m_uPort=port; m_hWnd=hWnd; if(m_hSocket!=NULL) { //先关闭已经打开的socket closesocket(m_hSocket); m_hSocket=NULL; } //创建面向连接的流方式的套接字 m_hSocket=socket(AF_INET,SOCK_STREAM,0); ASSERT(m_hSocket!=NULL); ServerInit(); m_addr.sin_family=AF_INET; m_addr.sin_addr.S_un.S_addr=INADDR_ANY; m_addr.sin_port=htons(m_uPort); int ret=0; int error=0; //绑定套接字到本机的某个端口 ret=bind(m_hSocket,(LPSOCKADDR)&m_addr,sizeof(m_addr)); if(ret == SOCKET_ERROR) { AfxMessageBox("Binding Error"); return FALSE; } ret = listen(m_hSocket, 64); if(ret == SOCKET_ERROR) { AfxMessageBox("Listen Error"); return FALSE; } return TRUE; }