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

📄 ch18.htm

📁 VC使用大全。里面集合了VC使用的各种使用技巧。非常有用。
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<p>Handles the Windows message generated when a socket has urgent, out-of-band data ready to read.</p>
<tr>
<td>
<pre><font color="#008000">OnReceive</font></pre>
<td>
<p>Handles the Windows 
message generated when a socket has data that could be read with <font color="#008000">Receive()</font>.Often overridden by derived classes.</p>
<tr>
<td>
<pre><font color="#008000">OnSend</font></pre>
<td>
<p>Handles the Windows message generated when a 
socket is ready to accept data that could be sent with <font color="#008000">Send()</font>. Often overridden by derived classes.</p>
<tr>
<td>
<pre><font color="#008000">Receive</font></pre>
<td>
<p>Reads data from the remote socket to which this socket is 
connected.</p>
<tr>
<td>
<pre><font color="#008000">ReceiveFrom</font></pre>
<td>
<p>Reads a datagram from a connectionless remote socket.</p>
<tr>
<td>
<pre><font color="#008000">Send</font></pre>
<td>
<p>Sends data to the remote socket to which this 
socket is connected.</p>
<tr>
<td>
<pre><font color="#008000">SendTo</font></pre>
<td>
<p>Sends a datagram without a connection.</p>
<tr>
<td>
<pre><font color="#008000">SetSockOpt</font></pre>
<td>
<p>Sets socket options.</p>
<tr>
<td>
<p>ShutDown</p>

<td>
<p>Keeps the socket open but prevents any further Send() or Receive() calls.</p></table>
<p>If you use the <font color="#008000">CAsyncSocket</font> class, you will have to fill the socket address structures yourself, and many developers would rather 
delegate a lot of this work. In that case, <font color="#008000">CSocket</font> is a better socket class.</p>
<p><b><i>CSocket</i></b></p>
<p><font color="#008000">CSocket</font> inherits from <font color="#008000">CAsyncSocket</font> and so has all the 
functions listed for <font color="#008000">CAsyncSocket</font>. Table 13.2 describes the new methods added and the virtual methods that are overridden in the derived CSocket class.</p>
<p><i>Table 18.2&#151;CSocket Methods</i></p>
<table border>
<tr>
<td>

<p><b>Method Name</b></p>
<td>
<p><b>Description</b></p>
<tr>
<td>
<pre><font color="#008000">Attach</font></pre>
<td>
<p>Attaches a socket handle to a <font color="#008000">CAsyncSocket</font> instance, so that it can form a connection to another 
machine.</p>
<tr>
<td>
<pre><font color="#008000">Create</font></pre>
<td>
<p>Completes the initialization after the constructor constructs a blank socket.</p>
<tr>
<td>
<pre><font color="#008000">FromHandle</font></pre>
<td>
<p>Returns a pointer to the 
<font color="#008000">CSocket</font> attached to the handle it was passed.</p>
<tr>
<td>
<pre><font color="#008000">IsBlocking</font> </p>
<td>
<p>Returns <font color="#008000">TRUE</font> if the socket is blocking at the moment, waiting for something to 
happen.</p>
<tr>
<td>
<pre><font color="#008000">CancelBlockingCall</font></pre>
<td>
<p>Cancels whatever request had left the socket blocking.</p>
<tr>
<td>
<pre><font color="#008000">OnMessagePending</font></pre>
<td>
<p>Handles the Windows messages 
generated for other parts of your application while the socket is blocking. Often overridden by derived classes.</p></table>
<p>In many cases, socket programming is no longer necessary because the WinInet classes, ISAPI programming, and ActiveX controls 
for Web pages are bringing more and more power to Internet programmers. If you would like to explore a sample socket program, try Chatter and ChatSrvr, provided with Visual C++. Search on either name in the online help, or open the files, located on the CD 
in the \DevStudio\VC\Samples\MFC\Advanced\Chatter and \DevStudio\VC\Samples\MFC\Advanced\Chatsrvr folders.</p>
<p>Each session of Chatter emulates a user server. The ChatSrvr program is the server, acting as traffic manager among several clients. Each 
Chatter can send messages to the ChatSrvr&#151;by typing in some text&#151;and the ChatSrvr sends the message to everyone logged into the session. Several channels of traffic are managed at once.</p>
<p>If you&#146;ve worked with sockets before, this short 
overview may be all you need to get started. If not, you may not need to learn them. If you plan to write a client/server application that runs over the Internet and does not use the existing standard applications like mail or the Web, then learning 
sockets is probably in your future. But, if you want to use e-mail, the Web, ftp, and other popular Internet information sources, you don&#146;t have to do it by writing socket programs at all. You may be able to use MAPI, the WinInet classes, or ISAPI to 
achieve the results you are looking for.</p>
<h3><b>Messaging API (MAPI)</b></h3>
<p>The most popular networking feature in most offices is electronic mail. You could add code to your application to generate the right commands over a socket to transmit a 
mail message, but it's simpler to build on the work of others.</p>
<p><b>What Is MAPI?</b></p>
<p>MAPI is a way of pulling together applications that need to send and receive messages (<i>messaging applications</i>) with applications that know how to send 
and receive messages (<i>messaging services</i> and <i>service </i><i>providers</i>,) in order to lower the work load of all the developers involved. Figure 18.1 shows the scope of MAPI. Note that the word <i>messaging</i> actually covers far more than 
just electronic mail: a MAPI service could send a fax or voice-mail message rather than an electronic mail message. If your application uses MAPI, the messaging services such as e-mail clients that the user has installed will carry out the work of sending 
the messages that your application generates.</p>
<b><a href="tfigs01.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch18/tfigs01.gif">FIG. 18.1</a></b>
<p><i>The Messaging API covers applications that need messaging and those </i><i>that provide it.</i></p>
<p>The extent to which an application uses 
messaging varies widely:</p>
<ul>
<li> Some applications can send a message, but sending messages is not really what the application is about. For example, a word processor is fundamentally about entering and formatting text and then printing or saving 
that text. If the word processor can also send the text in a message, fine, but that's incidental. Applications like this are said to be <i>messaging aware</i> and typically use just the tip of the MAPI functionality.</p>
<li> Some applications are useful 
without being able to send messages, but they are far more useful in an environment where messages can be sent. For example, a personal scheduler program can manage one person's To Do list whether messaging is enabled or not, but if it is enabled, a number 
of workgroup and client-contact features&#151;such as sending e-mail to confirm an appointment&#151;become available. Applications like this are said to be <i>messaging enabled</i> and use some, but not all, of the MAPI features.</p>
<li> Finally, some 
applications are all about messaging. Without messaging, these applications are useless. They are said to be <i>messaging based,</i> and they use all of MAPI's functionality.</p>
</ul>
<p><b>Win95 Logo Requirements</b></p>
<p>The number-one reason for a 
developer to make an application messaging aware is to meet the requirements of the Windows 95 Logo program. To qualify for the logo, an application must have a <u>S</u>end item on the <u>F</u>ile menu that uses MAPI to send the document. (Exceptions are 
granted to applications without documents.)</p>
<p>To add this feature to your applications, it's best to think of it before you create the empty shell with AppWizard. If you are planning ahead, here is a list of all the work you have to do to meet this 
part of the logo requirement:</p>
<ol>
<li><p> In Step 4 of AppWizard, select the MAP<u>I</u> (Messaging API) check box.</p>
</ol>
<p>That's it! The menu item is added, and message maps and functions are generated to catch the menu item and call functions 
that use your <font color="#008000">Serialize()</font> function to send the document through MAPI. Figure 18.2 shows an application called MAPIDemo, included on the book's CD-ROM, that is just an AppWizard empty shell.</p>
<a 
href="Tfigs02.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch18/Tfigs02.gif"><b>FIG. 18.2</b></a>
<p><i>AppWizard adds the </i><i><u>S</u></i><i>end item to the </i><i><u>F</u></i><i>ile menu, as well as the code </i><i>that handles the item.</i></p>
<p>No additional code was added, beyond the code 
generated by the AppWizard, to this application, and the <u>S</u>end item is on the <u>F</u>ile menu, as you can see. If you choose this menu item, your MAPI mail client is launched to send the message. Figures 18.2 and 18.3 were captured on a machine with 
Microsoft Exchange installed as an Internet mail client, and so it is Microsoft Exchange that is launched, as shown in Figure 18.3. The message contains the current document, and it is up to you to fill in the recipient, the subject, and any text you wish 
to send with the document.</p>
<a href="Tfigs03.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch18/Tfigs03.gif"><b>FIG. 18.3</b></a>
<p><i>Microsoft Mail is launched so the user can fill in the rest of the </i><i>e-mail message around the document that is being sent.</i></p>
<blockquote><p><img 
src="tip.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/tip.gif">
<p>If the <u>S</u>end item does not appear on your menu, make sure that you have a MAPI client installed. Microsoft Exchange is an easy-to-get MAPI client. The <font color="#008000">OnUpdateFileSendMail()</font> function removes the menu 
item <u>S</u>end from the menu if no MAPI client is registered on your computer.</p>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<p>If you didn&#146;t request MAPI support from AppWizard when you built your application, here are the steps to follow which will 
manually add the <u>S</u>end item:</p>
<ol>
<li><p> Add the <u>S</u>end item to the <u>F</u>ile menu. Use a resource ID of <font color="#008000">ID_FILE_SEND_MAIL</font>. The prompt will be supplied for you.</p>
<li><p> Add these two lines to the 
document's message map, outside the //AFX comments:</p>
<pre><font color="#008000">ON_COMMAND(ID_FILE_SEND_MAIL, OnFileSendMail)</font></pre>
<pre><font color="#008000">ON_UPDATE_COMMAND_UI(ID_FILE_SEND_MAIL, OnUpdateFileSendMail)</font></pre>
</ol>

<p>Adding the mail support to your application manually is not much harder than asking AppWizard to do it.</p>
<p><b>Advanced Use of MAPI</b></p>
<p>If you want more from MAPI than just meeting the logo requirements, things do get harder. There are 
actually four kinds of MAPI client interfaces:</p>
<ul>
<li> Simple MAPI, an older API not recommended for use in new applications</p>
<li> Common Messaging Calls (CMC), a simple API for messaging-aware and messaging-enabled applications</p>
<li> Extended 
MAPI, a full-featured API for messaging-based applications</p>
<li> OLE Messaging, an API with somewhat fewer features than Extended MAPI but ideal for use with Visual C++</p>
</ul>
<p><b>Common Messaging Calls</b></p>
<p>There are only ten functions in 
the CMC API. That makes it easy to learn, yet packs enough punch to get the job done. They are the following:</p>
<ul>
<li> <font color="#008000">cmc_logon()</font> connects to a mail server and identifies the user.</p>
<li> <font 
color="#008000">cmc_logoff()</font> disconnects from a mail server.</p>
<li> <font color="#008000">cmc_send()</font> sends a message.</p>
<li> <font color="#008000">cmc_send_documents()</font> sends one or more files.</p>
<li> <font 
color="#008000">cmc_list()</font> lists the messages in the user's mailbox.</p>
<li> <font color="#008000">cmc_read()</font> reads a message from the user's mailbox.</p>
<li> <font color="#008000">cmc_act_on()</font> saves or deletes a message.</p>
<li> 
<font color="#008000">cmc_look_up()</font> resolves names and addresses.</p>
<li> <font color="#008000">cmc_query_configuration()</font> reports what mail server is being used.</p>
<li> <font color="#008000">cmc_free()</font> frees any memory allocated by 

⌨️ 快捷键说明

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