⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server1view.cpp

📁 好用的网络文件传输工具
💻 CPP
字号:
// server1View.cpp : implementation of the CServer1View class
//
/*********************************************************************
//作者:赵明
//EMAIL:zmpapaya@hotmail.com;papaya_zm@sina.com
主页:http://h2osky.126.com
*********************************************************************/
#include "stdafx.h"
#include "server1.h"
//#include "OnlineList.h"
#include "server1Doc.h"
#include "CntrItem.h"
#include "server1View.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CServer1View

IMPLEMENT_DYNCREATE(CServer1View, CRichEditView)

BEGIN_MESSAGE_MAP(CServer1View, CRichEditView)
	//{{AFX_MSG_MAP(CServer1View)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	ON_WM_DESTROY()
ON_COMMAND(ID_STAR, OnServerStart)
ON_COMMAND(ID_ADD, addfile)
	ON_MESSAGE(WM_AGE1, addmessage)
ON_COMMAND(IDC_SEND, OnSend)
	ON_MESSAGE(WM_KSEND,OnKSend)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CServer1View construction/destruction
LRESULT CServer1View::OnKSend(WPARAM wParam,LPARAM lParam)
{
	OnSend();
	return 0;
}
void CServer1View::OnSend() 
{//发消息
     CMainFrame* pWnd=(CMainFrame*)AfxGetMainWnd();
	CEdit* pEdit=(CEdit*)pWnd->m_sendbar.GetDlgItem(IDC_EDIT1);
	CString temp;
	pEdit->GetWindowText(temp);
	if(temp.IsEmpty())
	{
		MessageBox("不能发送空信息!!!","警告");
		pEdit->SetFocus();
		return;
	}
	
if(!m_chat)
{
	MessageBox("没有客户联入!!!","警告");
		pEdit->SetFocus();
		return;
}
		temp="服务端: "+temp;
		temp=temp+"\n";
Message((LPCTSTR)temp,RGB(255,0,0));
chatsocket->Send(temp.GetBuffer(0),255);

temp.ReleaseBuffer();
pWnd->m_sendbar.GetDlgItem(IDC_EDIT1)->SetFocus();
	pEdit->SetWindowText("");
}
CServer1View::CServer1View()
{
	m_chat=FALSE;
	m_start=TRUE;
	// TODO: add construction code here
strcpy(zmfile[0].name,"none");
count=0;
}
//显示信息
LRESULT CServer1View::addmessage(WPARAM wParam, LPARAM lParam)
{
//	MessageBox("aaa");
	LPCTSTR pStr = (LPCTSTR)wParam;

Message(pStr,RGB(0,0,0));
return 1;
}
CServer1View::~CServer1View()
{
}

BOOL CServer1View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CRichEditView::PreCreateWindow(cs);
}

void CServer1View::OnInitialUpdate()
{
	CRichEditView::OnInitialUpdate();
	cfm.cbSize=sizeof(cfm);
	cfm.bCharSet=GB2312_CHARSET;
	cfm.crTextColor=RGB(0,0,0);
	cfm.dwMask=CFM_CHARSET | CFM_COLOR ;
	cfm.dwEffects=0;
	GetRichEditCtrl().SetDefaultCharFormat(cfm);
 
	SetMargins(CRect(720, 720, 720, 720));
}

void CServer1View::OnDestroy()
{
	// Deactivate the item on destruction; this is important
	// when a splitter view is being used.
   CRichEditView::OnDestroy();
  COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
   if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)
   {
      pActiveItem->Deactivate();
      ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
   }
   
}


/////////////////////////////////////////////////////////////////////////////
// CServer1View diagnostics

#ifdef _DEBUG
void CServer1View::AssertValid() const
{
	CRichEditView::AssertValid();
}

void CServer1View::Dump(CDumpContext& dc) const
{
	CRichEditView::Dump(dc);
}
CServer1Doc* CServer1View::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CServer1Doc)));
	return (CServer1Doc*)m_pDocument;
}
#endif //_DEBUG
void CServer1View::Message(LPCTSTR lpszMessage,COLORREF clr)
{
	//在窗口中显示聊天信息
	cfm.cbSize=sizeof(cfm);
	cfm.crTextColor=clr;
	cfm.dwMask=CFM_COLOR;
	CString strTemp = lpszMessage;
	
	int len = GetWindowTextLength();
	GetRichEditCtrl().SetSel(len,len);
	
	GetRichEditCtrl().SetSelectionCharFormat(cfm);
	GetRichEditCtrl().ReplaceSel(strTemp);
}
BOOL  CServer1View::PreTranslateMessage(MSG* pMsg) 
{

if (pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_RETURN)
	
		if(GetFocus()->GetDlgCtrlID()==IDC_SEND ||GetFocus()->GetDlgCtrlID()==IDC_EDIT1)
		{
		//	AfxGetMainWnd()->SendMessageToDescendants(WM_KSEND);
			AfxMessageBox("down");
			return TRUE;
			}
	   
	return CRichEditView::PreTranslateMessage(pMsg);
}
//加文件到服务器中
int CServer1View::addfile()
{
	if(((CMainFrame*)::AfxGetMainWnd())->m_wndOnline.m_ListCtrl->GetItemCount()>8){
		AfxMessageBox("同时最多只支持9个文件");
return 0;		
	}

CFileDialog cfiledlg(TRUE,NULL,NULL);
	 cfiledlg.DoModal();
	 
	 fnamepath=cfiledlg.GetPathName();
 
	 CFile myFile;
myFile.Open(fnamepath, CFile::modeRead | CFile::typeBinary|CFile::shareDenyNone); 
zmfile[count].length=myFile.GetLength();
 myFile.Close();
 
 
 for(int j=0;j<((CMainFrame*)::AfxGetMainWnd())->m_wndOnline.m_ListCtrl->GetItemCount();j++)
 {
if(nameph[j]==fnamepath)break;
 }
 if((j+1)<=((CMainFrame*)::AfxGetMainWnd())->m_wndOnline.m_ListCtrl->GetItemCount()){
 AfxMessageBox("文件同名,不可加入!");
return 0;
 }
 if(zmfile[count].length<=0){
	 AfxMessageBox("文件大小为零,不可加入!");
return 0;
 }

 if(m_start){
	listensocket=new mysocket(this);
	 listensocket->Create(8888);
 listensocket->Listen();
 CMainFrame* pWnd=(CMainFrame*)AfxGetMainWnd();
	CServer1App* pApp=(CServer1App*)AfxGetApp();
		DWORD dwthread;
	sockaddr_in local;
	SOCKET m_socket;

	int rc=0;
	//char buf[1];
	local.sin_family=AF_INET;
	local.sin_port=htons(1028);
	local.sin_addr.S_un.S_addr=INADDR_ANY;
	m_socket=socket(AF_INET,SOCK_STREAM,0);

		rc=bind(m_socket,(LPSOCKADDR)&local,sizeof(local));
		if(rc==SOCKET_ERROR){
	CString aaa;
	aaa="Bind错误\n";
    AfxGetMainWnd()->SendMessageToDescendants(WM_AGE1,(LPARAM)aaa.GetBuffer(0),1);
	aaa.ReleaseBuffer();
	//return 0;
//	break;
		}
	::CreateThread(NULL,0,listenthread,(LPVOID)m_socket,0,&dwthread);
	CString aaa;
	aaa="服务器启动!\n";
    //AfxGetMainWnd()->SendMessageToDescendants(WM_AGE1,(LPARAM)aaa.GetBuffer(0),1);
	Message(aaa.GetBuffer(0),RGB(0,0,0));
	aaa.ReleaseBuffer();
	CString strT;
	strT="传圣(测试版)----服务器已经启动,当前IP为"+pApp->m_strIp+" 开放端口号为1028";
	pApp->m_pMainWnd->SetWindowText(strT);
	m_start=FALSE;
 }
	strcpy(zmfile[count].name,cfiledlg.GetFileName());
	 
   		AfxGetMainWnd()->SendMessage(WM_ADDLIST, (LPARAM)&zmfile[count],1);
	
	CString aaa;
	aaa.Format("加入文件  %s",zmfile[count].name);
		aaa+='\n';
	Message(aaa,RGB(0,0,0));
 
strcpy(zmfile[count+1].name,"none");
strcpy(nameph[count],fnamepath);
	count++;
 
	return 1;
}

void CServer1View::OnServerStart() 
{ 
//服务器启动
	if(m_start){
		listensocket=new mysocket(this);
	 listensocket->Create(8888);
 listensocket->Listen();
	
		CMainFrame* pWnd=(CMainFrame*)AfxGetMainWnd();
	CServer1App* pApp=(CServer1App*)AfxGetApp();
		DWORD dwthread;
	sockaddr_in local;
	SOCKET m_socket;

	int rc=0;
 
	local.sin_family=AF_INET;
	local.sin_port=htons(1028);
	local.sin_addr.S_un.S_addr=INADDR_ANY;
	m_socket=socket(AF_INET,SOCK_STREAM,0);

		rc=bind(m_socket,(LPSOCKADDR)&local,sizeof(local));
		if(rc==SOCKET_ERROR){
	CString aaa;
	aaa="Bind错误\n";
    AfxGetMainWnd()->SendMessageToDescendants(WM_AGE1,(LPARAM)aaa.GetBuffer(0),1);
	aaa.ReleaseBuffer();
 
		}
	::CreateThread(NULL,0,listenthread,(LPVOID)m_socket,0,&dwthread);
	CString aaa;
	aaa="服务器启动!\n";
 
	Message(aaa.GetBuffer(0),RGB(0,0,0));
	aaa.ReleaseBuffer();
	CString strT;
	strT="传圣(测试版)----服务器已经启动,当前IP为"+pApp->m_strIp+" 开放端口号为1028";
	pApp->m_pMainWnd->SetWindowText(strT);
	m_start=FALSE;
	}
	else
	{
	CString aaa;
	aaa="服务器已经启动了!\n";
 
	Message(aaa.GetBuffer(0),RGB(0,0,0));
	aaa.ReleaseBuffer();
	}

 

}



/////////////////////////////////////////////////////////////////////////////
// CServer1View message handlers
//接授用户
void CServer1View::accept()
{
chatsocket=new mysocket(this);
listensocket->Accept(*chatsocket);
m_chat=TRUE;
}

void CServer1View::recevied()
{

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -