61.htm
来自「网络编程原理文摘 [文件] 精华区目录结构 [目录] 网络编程的」· HTM 代码 · 共 944 行 · 第 1/2 页
HTM
944 行
<HTML><HEAD><TITLE>CTerm非常精华下载</TITLE>
<META content="text/html; charset=gb2312" http-equiv=Content-Type></HEAD>
<BODY bgColor=#ffffff>
<TABLE border=0 cellPadding=0 cellSpacing=0 width=100% background=0044.jpg>
<BODY>
<TR>
<TD height=150 rowSpan=2 width="308" ><IMG alt=DDl_back.jpg height=129 width=300 src="DDl_back.jpg" ></TD>
<TD background=DDl_back2.jpg height=50 width="581" ><BIG><BIG><FONT face=黑体>
<P align=center >重拳出击 一网打尽</FONT></BIG></BIG></P></TD></TR>
<TR>
<TD bgColor=#0099ff borderColor=#0099ff height=5 width="581"></TD></TR>
<TR>
<TD align=middle colSpan=2 height=100 vAlign=top width="891" ><BR>
<p align=center>[<a href="清华bbs网络资源.htm">回到开始</a>][<a href="8.htm">上一层</a>][<a href="62.htm">下一篇</a>]
<hr><p align="left"><small>发信人: goodmind (花为媒), 信区: NetPRG <br>
标 题: 简单的聊天程序段 <br>
发信站: BBS 水木清华站 (Sun Dec 3 18:12:46 2000) <br>
<br>
UniTalk2 Chat Program <br>
<br>
------------------------------------------------------------------------ <br>
-------- <br>
<br>
This article was contributed by Ivan Cerrato. Max Vigilante. Bartolo <br>
Sorrentino. <br>
<br>
Environment: NT4 SP3; Developed with VC 60 SP3; <br>
<br>
<br>
Unitalk2 is a simple talk with some strange detail: <br>
<br>
It's an application built on ATL technology <br>
It utilizes Connection Points <br>
The UniTalk application (UniTalk2.exe) acts as both the server and <br>
client depending on the start method* <br>
<br>
*if you start the application from Explorer by double-clicking it, it <br>
acts as a client. The "other" instance to which it connects through DCOM <br>
is then the server. <br>
<br>
UniTalk2 is a simple chat. Hence, you can talk via a network <br>
connection with another user. The application presents you with a <br>
standard (horizontal) split window format to distinguish the text you're <br>
writing from that of the remote computer. <br>
<br>
In the combo box control, you can choose the friend to "call". The <br>
list displayed in the combo box is maintained via a simple text file <br>
named IpAddress.txt. When you choose to initiate a conversation with a <br>
remote machine, the user on that machine will see a dialog that <br>
informs him of an incoming chat request. They need only to simply <br>
click the Yes button to acknowledge the request and begin chatting <br>
when the application starts up. <br>
<br>
Important Notes <br>
The UniTalk application (UniTalk2.exe) must be installed on both <br>
machines that want to participate in the chat. <br>
Both users must be on the same domain <br>
UniTalk2 is an interactive, COM-based application, that exchanges data <br>
across a network connection via DCOM. Therefore, you must configure <br>
dcomcnfg.exe for the launch of .exe <br>
Description of the UniTalk2 Code <br>
The _tWinMain function (UniTalk2.cpp) function decides (based on how the <br>
application was invoked) if it is to act as a server or client. <br>
extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, <br>
HINSTANCE /*hPrevInstance*/, LPTSTR lpCmdLine, int nShowCmd) <br>
{ <br>
if( nShowCmd == 1) <br>
{ <br>
MainBool = true; <br>
}; <br>
<br>
... <br>
<br>
if( MainBool ) <br>
{ <br>
_ASSERTE(CCall2::mpDialo1 == NULL); <br>
CCall2::mpDialo1 = new CDialogo(true); <br>
CCall2::mpDialo1->DoModal(); <br>
} <br>
else <br>
{ <br>
{ <br>
// if MainBool is false, the appplication started in <br>
// remote-mode and this branch of 'if' run <br>
_ASSERTE(CCall2::mpDialo2 == NULL); <br>
CCall2::mpDialo2 = new CDialogo(false); <br>
CCall2::mpDialo2->DoModal(); <br>
<br>
//CCall2::mpDialo2->Create(NULL); <br>
//CCall2::mpDialo2->ShowWindow(SW_SHOWNORMAL); <br>
} <br>
} <br>
<br>
When CCall2::mpDialo1 is equal to "new CDialog(TRUE)", the application <br>
is launched as a chat client. Otherwise, it starts up as the server. <br>
<br>
The central part of the application is the CDialog. This is <br>
responsible for the dialogs, creation of interfaces and creation of <br>
connection points. In the RunConnection() function, you can see the <br>
creation of the remote interface via the call to the standard COM <br>
function ::CoCreateInstanceEx() as well as the creation of <br>
CComObject<CRx2> (mpRx2 = new CComObject<CRx2>) that represents the <br>
connection-point. <br>
<br>
<br>
void CDialogo::RunConnection() <br>
{ <br>
USES_CONVERSION; <br>
i = res = 0; <br>
COSERVERINFO info; <br>
MULTI_QI qi; <br>
int result = 0; <br>
info.dwReserved1 = NULL; <br>
info.dwReserved2 = NULL; <br>
info.pAuthInfo = NULL; <br>
//info.pwszName = NULL; <br>
info.pwszName = A2W(SelectedIp); <br>
//info.pwszName = L"150.197.55.101";//max <br>
<br>
qi.pIID = &IID_IUnknown; <br>
qi.hr = 0; <br>
qi.pItf = NULL; <br>
<br>
int mConnCLCTX; <br>
<br>
if (SelectedIp[0] == '§') <br>
{ <br>
{ <br>
mLocalTalk = true; <br>
<br>
info.pwszName = NULL; <br>
mConnCLCTX = CLSCTX_LOCAL_SERVER; <br>
}else <br>
{ <br>
mConnCLCTX = CLSCTX_REMOTE_SERVER; <br>
} <br>
<br>
hr = CoCreateInstanceEx(CLSID_Call2, <br>
NULL, <br>
mConnCLCTX, <br>
&info, <br>
1, <br>
&qi ); <br>
<br>
//ATLASSERT(hr==NOERROR); <br>
if (!SUCCEEDED(hr)) <br>
{ <br>
::MessageBox(m_hWnd, <br>
_T("Could not create ExeChat object." <br>
" Make sure the server is registered."), <br>
_T("Object Instantiation Error."), <br>
MB_OK | MB_ICONINFORMATION); <br>
<br>
// return 0L; <br>
} <br>
_ASSERT(qi.pItf != NULL); <br>
<br>
hr = qi.pItf->QueryInterface(IID_ICall2,(void**)&pCall2); <br>
_ASSERT(pCall2 != NULL); <br>
<br>
hr = pCall2->QueryInterface(IID_IConnectionPointContainer, <br>
(void **)&pConnPtContainer); <br>
<br>
_ASSERT(SUCCEEDED(hr) && pConnPtContainer != NULL); <br>
<br>
//BSTR per il chiamante <br>
// wchar_t nome[] = L"ivan"; <br>
//BSTR mTempBstr = SysAllocString(nome); <br>
<br>
hr = pCall2->Run(&res); <br>
_ASSERT(SUCCEEDED(hr)); <br>
<br>
<br>
mpRx2 = new CComObject<CRx2>; <br>
_ASSERT(mpRx2 != NULL); <br>
((IRx2*)mpRx2)->AddRef(); <br>
<br>
hr = pConnPtContainer->FindConnectionPoint(IID_IRx2, <br>
&m_pIRx2ConnectionPoint); <br>
<br>
_ASSERT(SUCCEEDED(hr) && mpRx2 != NULL); <br>
<br>
qi.pItf->Release(); <br>
pConnPtContainer->Release(); <br>
<br>
////////////////////////// Advise //////////////////////// <br>
_ASSERT(mpRx2 != NULL); <br>
hr = m_pIRx2ConnectionPoint->Advise((IUnknown*)mpRx2, <br>
&m_dwCookie); <br>
<br>
_ASSERT(SUCCEEDED(hr) && m_dwCookie != 0); <br>
/////////////////////////////////////////////////////////// <br>
} <br>
<br>
If the CDialogo::RunConnection() function returns without error, you are <br>
linked with your friend and are ready to chat. <br>
<br>
Installation <br>
On each machine where you want to run UniTalk, you must register the <br>
UniTalk2 application by typing the following into the Windows "start <br>
box" (obviously, you'll need to qualify the name of the application with <br>
the name of the directory where the application resides): <br>
<path>Unitalk2.exe -Regserver <br>
You must register UniTalk2ps.dll (this is the proxy/stub) on the <br>
machines of each user as follows: <br>
regsvr32 <path>UniTalk2ps.dll; <br>
Place the IpAddress.txt user database in the same directory with <br>
Unitalk2.exe <br>
You must edit the IpAddress.txt text file and add the names and IP <br>
addresses of those with which you wish to chat. <br>
You must configure DCOM (using the DCOMCNFG.EXE application) as follows: <br>
<br>
Default Properties tab <br>
Check the Enable Distributed COM on this computer check box <br>
In the Default Authentication Level combo box, select the None option <br>
In the Default Impersonation Level combo box, select the Delegate option <br>
<br>
<br>
Default Security tab <br>
On all Access, Launch and Configuration security, give "full control" to <br>
INTERACTIVE USER <br>
Application tab <br>
Double click UniTalk2 or IVirtInterface <br>
On the General tab, set the Authentication level to None <br>
On the Location tab, check the "Run application on this computer..." <br>
check box <br>
On the Security tab, set "full control" for the INTERACTIVE USER on <br>
Launch, Access and Configuration security <br>
On the Identity tab, check the "The Interactive user" check box <br>
Y <br>
<br>
<br>
ou must so configure Dcomcnfg.exe on the client and server machines <br>
<br>
-- <br>
<br>
音乐+美眉+啤酒=???? <br>
<br>
<br>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?