// server1View.cpp : implementation of the CServer1View class
//
#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;
}
// send message
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);
}
// initial update
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()
{
// limit the number of files on server
if(((CMainFrame*)::AfxGetMainWnd())->m_wndOnline.m_ListCtrl->GetItemCount() > 8){
AfxMessageBox("同时最多只支持9个文件");
return 0;
}
// the dialog to show the file would be opened
CFileDialog cfiledlg(TRUE, NULL, NULL);
cfiledlg.DoModal();
// the path of the file in the computer
fnamepath = cfiledlg.GetPathName();
CFile myFile;
// open the file along the path and use the mode
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) // if have the same path break?
break;
}
// check wether the file already in the server
if((j+1) <= ((CMainFrame*)::AfxGetMainWnd())->m_wndOnline.m_ListCtrl->GetItemCount()){
AfxMessageBox("文件同名,不可加入!");
return 0;
}
// check the length
if(zmfile[count].length <= 0){
AfxMessageBox("文件大小为零,不可加入!");
return 0;
}
if(m_start){
listensocket = new mysocket(this); // new a the mysocket object
listensocket->Create(8888); // create the socket port
listensocket->Listen(); // listen the socket
// ??? what about their function?
CMainFrame* pWnd = (CMainFrame*)AfxGetMainWnd();
CServer1App* pApp = (CServer1App*)AfxGetApp();
DWORD dwthread;
sockaddr_in local; // socket struct
SOCKET m_socket; // socket object
int rc = 0;
//char buf[1]; initiate the socket struct
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); // creat the socket interface
rc = bind(m_socket, (LPSOCKADDR)&local, sizeof(local)); // bind the socket interface
if(rc == SOCKET_ERROR){
CString aaa;
aaa = "Bind错误\n";
AfxGetMainWnd()->SendMessageToDescendants(WM_AGE1, (LPARAM)aaa.GetBuffer(0), 1);
aaa.ReleaseBuffer();
//return 0;
// break;
}
// create thread in the socket to make the communication come true
::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();
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()
{
//服务器启动
//*
WORD wVersionRequested = MAKEWORD(2, 2);
WSADATA wsaData;
WSAStartup(wVersionRequested, &wsaData);
//*/
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)&loca
实例53-多线程文件下载服务器.rar_文件下载_文件服务器_服务器_服务器 接收_涓嬭浇鏂囦欢
版权申诉
101 浏览量
2022-09-24
12:27:09
上传
评论
收藏 67KB RAR 举报
小贝德罗
- 粉丝: 68
- 资源: 1万+
最新资源
- 使用 C 语言实现的计算非负整数的阶乘
- 2011-2021最新版本北京大学数字普惠金融指数(PKU-DFIIC).xlsx
- 县域数字乡村指数2018-2020(1).xlsx
- Docker容器配置进阶
- tensorflow-gpu-2.7.4-cp37-cp37m-manylinux2010-x86-64.whl
- 多段线、 圆、弧转多段线(仅我可见)
- tensorflow-2.7.2-cp38-cp38-manylinux2010-x86-64.whl
- 李慧琴C语言基础部分.zip
- yeyue-p8Yi4-ve4a83792.apk
- tensorflow-gpu-2.7.3-cp38-cp38-manylinux2010-x86-64.whl
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈