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

📄 server1.cpp

📁 一个简单的视频会议VC++MFC工程文件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	char* m_buf;
	m_buf=new char[100];
	SOCKET  sConnect1=(SOCKET)lpparam;
	//调用自定义的函数,从客户端读取100字节的数据。
	int aa=readn(sConnect1,m_buf,100);
	//如果读取数据的时候发生了错误,则
	if(aa<0)
	{
		//关闭此连接用的socket。
		closesocket(sConnect1);
		return -1;
	}

	//强制的数据类型转换,把传来的信息转为定义的文件信息
	fiinfo=(fileinfo*)m_buf;
	CString temp;
	temp.Format("re %d from there",aa);
	CString aaa;
	//用来面的这个switch代码块,来检验客户想说什么
	switch(fiinfo->type)
	{
		//type==0,大概表示:客户端需要从服务器端读取可以下载的文件列表的信息。
		case 0:
			//向客户端发送1080字节(就是10个数组元素)的数据,
			//zmfile这个结构体数组中存放的是:服务器端程序中可供用户端下载的文件的信息
			//(文件名、文件的长度)。
			//每一个zmfile数组元素的大小是108个字节,所以,10个元素就一共是1080个字节。
			//其实,这种直接写上一个数字的编程方法并不好,因为如果以后结构体的设计变了,那么
			//这种硬性写上的数字就需要重新改写,而且,如果在多处都用到这个数字,那么改写起来
			//就非常的麻烦。正确的写法是:用上sizeof,或者,最起码要用上一个宏定义,这样,
			//只需要改写宏的数值就行了。
			aa=sendn(sConnect1,(char*)zmfile,SIZE_OF_zmfile);
			//added for test begin
//			char stemp[35];
//			sprintf(stemp,"SIZE_OF_zmfile=%d \n",SIZE_OF_zmfile);
//			AfxGetMainWnd()->SendMessageToDescendants(WM_AGE1,(LPARAM)stemp,1);
			//经过测试,发现SIZE_OF_zmfile的值的确是1080
			//added for test end
			//如果有错,则
			if(aa<0)
			{	
				closesocket (sConnect1);
				return -1;
			}
			//发消息给主窗体,不过,我发现处理此消息的函数却在视类中,难道是mfc的消息传递机制
			//把消息转送给视类了吗?
			//答:我估计,一定是这么回事。
			aaa="收到LIST命令\n";
			AfxGetMainWnd()->SendMessageToDescendants(WM_AGE1,(LPARAM)aaa.GetBuffer(0),1);
			break;
		//type==2,大概表示:客户端准备好了,所以你(指服务器)可以开始传送文件了。
		case 2:
			//“XX.xxx  文件被请求! 文件的全路径”。一个例子是:TDA7293.pdf  文件被请求!C:\Documents and Settings\YJK\My Documents\TDA7293.pdf
			aaa.Format("%s  文件被请求!%s\n",zmfile[fiinfo->fileno].name,nameph[fiinfo->fileno]);
			//将客户端要下载的文件的信息,显示到RichEdit控件中。
			AfxGetMainWnd()->SendMessageToDescendants(WM_AGE1,(LPARAM)aaa.GetBuffer(0),1);
			//这个函数大概就是传送文件的具体函数吧。
			readfile(sConnect1,fiinfo->seek,fiinfo->len,fiinfo->fileno);
			break;
		//服务器端无法理解客户端的需求,因为当前服务器端只能理解type==0和type==2的这两种请求。
		default:
			aaa="接收协议错误!\n";
			AfxGetMainWnd()->SendMessageToDescendants(WM_AGE1,(LPARAM)aaa.GetBuffer(0),1);
	}

	return 0;
}

/////////////////////////////////////////////////////////////////////////////
// CServer1App initialization

BOOL CServer1App::InitInstance()
{
	//在下面的这个函数中,调用了WSAStartup函数初始化了WinSock。至于对应的WSACleanup函数,
	//也许不必调用了吧。因为由AppWizard自动生成的支持WinSock的程序框架中,根本就没有
	//对WSACleanup函数的调用。
	if (!AfxSocketInit())
	{
		AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
		return FALSE;
	}

	// Initialize OLE libraries
	if (!AfxOleInit())
	{
		AfxMessageBox(IDP_OLE_INIT_FAILED);
		return FALSE;
	}

	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	// Change the registry key under which our settings are stored.
	// TODO: You should modify this string to be something appropriate
	// such as the name of your company or organization.
	SetRegistryKey(_T("Local AppWizard-Generated Applications"));

	LoadStdProfileSettings();  // Load standard INI file options (including MRU)

	// Register the application's document templates.  Document templates
	//  serve as the connection between documents, frame windows and views.

	CSingleDocTemplate* pDocTemplate;
	pDocTemplate = new CSingleDocTemplate(
		IDR_MAINFRAME,
		RUNTIME_CLASS(CServer1Doc),
		RUNTIME_CLASS(CMainFrame),       // main SDI frame window
		RUNTIME_CLASS(CServer1View));
	pDocTemplate->SetContainerInfo(IDR_CNTR_INPLACE);
	AddDocTemplate(pDocTemplate);

	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);

	// Dispatch commands specified on the command line
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;

	// The one and only window has been initialized, so show and update it.

	//added by author begin
	//Centers a window relative to its parent.If the pop-up window is not owned, it is centered relative to the screen.
	m_pMainWnd->MoveWindow(0,0,800,600,true);
	//我试过了,下面这一句的确管用。
	m_pMainWnd->CenterWindow();
	m_pMainWnd->SetWindowText("月影传书");
	
	m_pMainWnd->ShowWindow(SW_SHOW);	//这句代码不是作者添加了,是AppWizard自动生成的。
	m_pMainWnd->UpdateWindow();			//这句代码不是作者添加了,是AppWizard自动生成的。
    
//	WORD wVersionRequested;//commented by me!
//  WSADATA wsaData;//commented by me!
    char name[255];
    PHOSTENT hostinfo;
	//WORD  16-bit unsigned integer. 
	//The MAKEWORD macro creates a WORD value by concatenating the specified values. 
	//用两个8位的BYTE类型的数,合成一个16位的数。表示要使用 WinSock 2.0 版。
//  wVersionRequested = MAKEWORD( 2, 0 );//commented by me!
	//The WSAStartup function must be the first Windows Sockets function called by an application or DLL. It allows an application or DLL to specify the version of Windows Sockets required and retrieve details of the specific Windows Sockets implementation. The application or DLL can only issue further Windows Sockets functions after successfully calling WSAStartup.
	//使用API版本的WinSock的时候,需要先调用此函数。
	//Return Values  The WSAStartup function returns zero if successful. Otherwise, it returns one of the error codes listed in the following. 
	//如果初始化WinSock成功,则
	//经过思考,我认为下面的这一句代码根本就是多余,因为在本函数中的开始处已经调用了if (!AfxSocketInit())
	//这一句代码,在AfxSocketInit函数内部已经调用了一次WSAStartup函数了。
//  if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )//commented by me!
	{
		//The gethostname function returns the standard host name for the local machine.
        if( gethostname ( name, sizeof(name)) == 0)
		{
			//The gethostbyname function retrieves host information corresponding to a host name from a host database.
			//其参数是:[in] Pointer to the null-terminated name of the host to resolve. 
			//If retrieveing host information successfully, then
            if((hostinfo = gethostbyname(name)) != NULL)
            {
				//The inet_ntoa function converts an (Ipv4) Internet network address into a string in Internet standard dotted format.
				/*
				char FAR * inet_ntoa(
				  struct   in_addr in  
				);
				  */
				//h_addr_list  Null-terminated list of addresses for the host. Addresses are returned in network byte order. The macro h_addr is defined to be h_addr_list[0] for compatibility with older software. 
				//The in_addr structure represents a host by its Internet address.
				/*
				struct in_addr {
				  union {
						  struct { u_char s_b1,s_b2,s_b3,s_b4; }   S_un_b;
						  struct { u_short s_w1,s_w2; }            S_un_w;
						  u_long                                   S_addr;
				  } S_un;
				};
				*/
				//因为inet_ntoa函数的参数是in_addr类型的,所以,下面就用了这么复杂的数据类型转换。
				m_strIp = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
			}
		}
		//The WSACleanup function terminates use of the Ws2_32.dll.An application or DLL is required to perform a successful WSAStartup call before it can use Windows Sockets services. When it has completed the use of Windows Sockets, the application or DLL must call WSACleanup to deregister itself from a Windows Sockets implementation and allow the implementation to free any resources allocated on behalf of the application or DLL. 
		//奇怪了,我觉得不应该在这里就调用WSACleanup函数呀,太早了呀,怎么回事呢?
		//我觉得此函数应该放到App类的析构函数中调用才对。
		//答:经过最新的分析,我认为下面的这一句根本就是多余。
//		WSACleanup();//commented by me!
     } 
	//added by author begin

	return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();
	CLinkCtrl	m_mail;
	CLinkCtrl	m_http;
// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual BOOL OnInitDialog();
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
//	afx_msg void OnButton1();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_MAIL, m_mail);
	DDX_Control(pDX, IDC_HTTP, m_http);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
//	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// App command to run the dialog
void CServer1App::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CServer1App message handlers

BOOL CAboutDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	m_mail.SetLinkString("http://www.sarahclub.com/");
	m_http.SetLinkString("http://211.152.147.97/bbs/");
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

⌨️ 快捷键说明

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