// srvrdoc.cpp : implementation of the CServerDoc class
//
// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (C) 1992-1997 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.
#include "stdafx.h"
#include "chatsrvr.h"
#include "srvrdoc.h"
#include "srvrvw.h"
#include "msg.h"
#include "dialogs.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CServerDoc
IMPLEMENT_DYNCREATE(CServerDoc, CDocument)
BEGIN_MESSAGE_MAP(CServerDoc, CDocument)
//{{AFX_MSG_MAP(CServerDoc)
ON_COMMAND(IDC_CHECK_CONNS, OnCheckConns)
//}}AFX_MSG_MAP
ON_UPDATE_COMMAND_UI(ID_MESSAGES, OnUpdateMessages)
ON_UPDATE_COMMAND_UI(ID_CONNECTIONS, OnUpdateConnections)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CServerDoc construction/destruction
CServerDoc::CServerDoc()
{
m_pSocket = NULL;
}
CServerDoc::~CServerDoc()
{
delete m_pSocket;
}
BOOL CServerDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
CDiscussionDlg Dialog;
if (Dialog.DoModal() == IDOK)
{
m_pSocket = new CListeningSocket(this);
if (m_pSocket->Create(Dialog.m_nPort+700))
{
if (m_pSocket->Listen())
return TRUE;
}
}
return FALSE;
}
void CServerDoc::DeleteContents()
{
delete m_pSocket;
m_pSocket = NULL;
CString temp;
if (temp.LoadString(IDS_SERVERSHUTDOWN))
m_msgList.AddTail(temp);
while(!m_connectionList.IsEmpty())
{
CClientSocket* pSocket = (CClientSocket*)m_connectionList.RemoveHead();
CMsg* pMsg = AssembleMsg(pSocket);
pMsg->m_bClose = TRUE;
SendMsg(pSocket, pMsg);
if (!pSocket->IsAborted())
{
pSocket->ShutDown();
BYTE Buffer[50];
while (pSocket->Receive(Buffer,50) > 0);
delete pSocket;
}
}
m_msgList.RemoveAll();
if (!m_viewList.IsEmpty())
((CEditView*)m_viewList.GetHead())->SetWindowText(_T(""));
CDocument::DeleteContents();
}
/////////////////////////////////////////////////////////////////////////////
// CServerDoc serialization
void CServerDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// CEditView contains an edit control which handles all serialization
((CEditView*)m_viewList.GetHead())->SerializeRaw(ar);
}
}
/////////////////////////////////////////////////////////////////////////////
// CServerDoc diagnostics
#ifdef _DEBUG
void CServerDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CServerDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CServerDoc Operations
void CServerDoc::UpdateClients()
{
for(POSITION pos = m_connectionList.GetHeadPosition(); pos != NULL;)
{
CClientSocket* pSocket = (CClientSocket*)m_connectionList.GetNext(pos);
CMsg* pMsg = AssembleMsg(pSocket);
if (pMsg != NULL)
SendMsg(pSocket, pMsg);
}
}
void CServerDoc::ProcessPendingAccept()
{
CClientSocket* pSocket = new CClientSocket(this);
if (m_pSocket->Accept(*pSocket))
{
pSocket->Init();
m_connectionList.AddTail(pSocket);
}
else
delete pSocket;
}
void CServerDoc::ProcessPendingRead(CClientSocket* pSocket)
{
do
{
CMsg* pMsg = ReadMsg(pSocket);
if (pMsg->m_bClose)
{
CloseSocket(pSocket);
break;
}
}
while (!pSocket->m_pArchiveIn->IsBufferEmpty());
UpdateClients();
}
CMsg* CServerDoc::AssembleMsg(CClientSocket* pSocket)
{
static CMsg msg;
msg.Init();
if (pSocket->m_nMsgCount >= m_msgList.GetCount())
return NULL;
for (POSITION pos1 = m_msgList.FindIndex(pSocket->m_nMsgCount); pos1 != NULL;)
{
CString temp = m_msgList.GetNext(pos1);
msg.m_msgList.AddTail(temp);
}
pSocket->m_nMsgCount = m_msgList.GetCount();
return &msg;
}
CMsg* CServerDoc::ReadMsg(CClientSocket* pSocket)
{
static CMsg msg;
TRY
{
pSocket->ReceiveMsg(&msg);
Message(msg.m_strText);
m_msgList.AddTail(msg.m_strText);
}
CATCH(CFileException, e)
{
CString strTemp;
if (strTemp.LoadString(IDS_READERROR))
Message(strTemp);
msg.m_bClose = TRUE;
pSocket->Abort();
}
END_CATCH
return &msg;
}
void CServerDoc::SendMsg(CClientSocket* pSocket, CMsg* pMsg)
{
TRY
{
pSocket->SendMsg(pMsg);
}
CATCH(CFileException, e)
{
pSocket->Abort();
CString strTemp;
if (strTemp.LoadString(IDS_SENDERROR))
Message(strTemp);
}
END_CATCH
}
void CServerDoc::CloseSocket(CClientSocket* pSocket)
{
pSocket->Close();
POSITION pos,temp;
for(pos = m_connectionList.GetHeadPosition(); pos != NULL;)
{
temp = pos;
CClientSocket* pSock = (CClientSocket*)m_connectionList.GetNext(pos);
if (pSock == pSocket)
{
m_connectionList.RemoveAt(temp);
break;
}
}
delete pSocket;
}
void CServerDoc::Message(LPCTSTR lpszMessage)
{
((CServerView*)m_viewList.GetHead())->Message(lpszMessage);
}
/////////////////////////////////////////////////////////////////////////////
// CServerDoc Handlers
void CServerDoc::OnUpdateMessages(CCmdUI* pCmdUI)
{
pCmdUI->Enable(TRUE);
CString strFmt;
if (strFmt.LoadString(IDS_MESSAGESFMT))
{
CString strTemp;
wsprintf(strTemp.GetBuffer(50),strFmt,m_msgList.GetCount());
strTemp.ReleaseBuffer();
pCmdUI->SetText(strTemp);
}
}
void CServerDoc::OnUpdateConnections(CCmdUI* pCmdUI)
{
pCmdUI->Enable(TRUE);
CString strFmt;
if (strFmt.LoadString(IDS_CONNECTIONSFMT))
{
CString strTemp;
wsprintf(strTemp.GetBuffer(50),strFmt,m_connectionList.GetCount());
strTemp.ReleaseBuffer();
pCmdUI->SetText(strTemp);
}
}
void CServerDoc::OnCheckConns()
{
// TODO: Add your command handler code here
UINT nClosedClientSockets = 0;
POSITION pos, temp;
for(pos = m_connectionList.GetHeadPosition(); pos != NULL;)
{
temp = pos;
CClientSocket* pSock = (CClientSocket*)m_connectionList.GetNext(pos);
if (pSock->HasConnectionDropped())
{
m_connectionList.RemoveAt(temp);
pSock->Close();
delete pSock;
nClosedClientSockets++;
}
}
CHAR szMessage[150] = "";
if( nClosedClientSockets > 1 )
{
sprintf( szMessage, "%d client sockets were closed because of dropped connections!",
nClosedClientSockets );
}
else if( nClosedClientSockets == 1 )
{
sprintf( szMessage, "%d client socket was closed because of dropped connection!",
nClosedClientSockets );
}
else
{
sprintf( szMessage, "No client sockets were closed." );
}
AfxMessageBox( szMessage );
}
没有合适的资源?快使用搜索试试~ 我知道了~
socket编程实例
共58个文件
h:20个
cpp:18个
bmp:4个
需积分: 0 24 下载量 74 浏览量
2008-10-12
08:03:28
上传
评论
收藏 56KB ZIP 举报
温馨提示
利用 socket编程,实现客户断和服务器断的通信。实现了客户断,服务器断程序。利用C++编写
资源详情
资源评论
资源推荐
收起资源包目录
chatsource.zip (58个子文件)
chatsrvr
lstnsock.h 1KB
resource.h 1KB
chatsrvr.cpp 4KB
stdafx.h 669B
srvrvw.cpp 2KB
chatsrvr.clw 2KB
ChatSrvr.rc 9KB
chatsrvr.dsw 514B
msg.h 998B
mainfrm.h 1KB
dialogs.h 1KB
srvrvw.h 2KB
mainfrm.cpp 3KB
makefile 561B
msg.cpp 2KB
clntsock.h 1KB
stdafx.cpp 522B
srvrdoc.h 2KB
chatsrvr.mak 17KB
clntsock.cpp 3KB
chatsrvr.h 1KB
chatsrvr.dsp 8KB
res
bitmap1.bmp 384KB
chatsrvr.ico 1KB
toolbar.bmp 838B
bitmap2.bmp 37KB
chatsrvr.rc2 400B
dialogs.cpp 1KB
lstnsock.cpp 1KB
srvrdoc.cpp 7KB
chatter
chatsock.h 832B
chatdoc.h 2KB
resource.h 3KB
setupdlg.cpp 2KB
sendvw.cpp 4KB
stdafx.h 669B
Chatter.rc 10KB
chatvw.h 1KB
msg.h 998B
mainfrm.h 2KB
chatter.h 1KB
setupdlg.h 1KB
chatdoc.cpp 6KB
mainfrm.cpp 3KB
makefile 559B
msg.cpp 2KB
stdafx.cpp 521B
chatsock.cpp 902B
chatter.mak 17KB
chatvw.cpp 2KB
chatter.clw 2KB
res
chatter.ico 1KB
chatter.rc2 399B
toolbar.bmp 478B
chatter.dsp 7KB
sendvw.h 2KB
chatter.dsw 512B
chatter.cpp 4KB
共 58 条
- 1
hylovewfw
- 粉丝: 1
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0