Server.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:1k
- // Server.cpp: implementation of the CServer class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "Example1.h"
- #include "Server.h"
- #include "Example1Dlg.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CServer::CServer()
- {
- m_hSocket=NULL;
- }
- CServer::~CServer()
- {
- WSAAsyncSelect(m_hSocket, m_hWnd, 0, 0);
- }
- BOOL CServer::InitAndListen(HWND hwnd,UINT port)
- {
- m_uPort=port;
- m_hWnd=hwnd;
- if(m_hSocket != NULL)
- {
- 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, 5);
- if(ret == SOCKET_ERROR)
- {
- AfxMessageBox("Listen Error");
- return FALSE;
- }
- return TRUE;
- }
- void CServer::ServerInit()
- {
- if(WSAAsyncSelect(m_hSocket, m_hWnd, SER_MESSAGE, FD_ACCEPT|FD_READ|FD_WRITE|FD_CLOSE)>0)
- AfxMessageBox("error select");
- }