?? ch18.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function popUp(pPage) {
var fullURL = document.location;
var textURL = fullURL.toString();
var URLlen = textURL.length;
var lenMinusPage = textURL.lastIndexOf("/");
lenMinusPage += 1;
var fullPath = textURL.substring(0,lenMinusPage);
popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394');
figDoc= popUpWin.document;
zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>';
zhtm += '<link rel="stylesheet" href="/includes/stylesheets/ebooks.css"></head>';
zhtm += '<BODY bgcolor="#FFFFFF">';
zhtm += '<IMG SRC="' + fullPath + pPage + '">';
zhtm += '<P><B>' + pPage + '</B>';
zhtm += '</BODY></HTML>';
window.popUpWin.document.write(zhtm);
window.popUpWin.document.close();
// Johnny Jackson 4/28/98
}
//-->
</SCRIPT>
<link rel="stylesheet" href="/includes/stylesheets/ebooks.css">
<TITLE>Special Edition Using Visual C++ 6 -- Ch 18 -- Sockets, MAPI, and the Internet</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<CENTER>
<H1><IMG SRC="../button/que.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"><BR>
Special Edition Using Visual C++ 6</H1>
</CENTER>
<CENTER>
<P><A HREF="../ch17/ch17.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch19/ch19.htm"><IMG
SRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A>
<HR>
</CENTER>
<CENTER>
<H1>- 18 -</H1>
</CENTER>
<CENTER>
<H1>Sockets, MAPI, and the Internet</H1>
</CENTER>
<UL>
<LI><A HREF="#Heading1">Using Windows Sockets</A>
<UL>
<LI><A HREF="#Heading2">Winsock in MFC</A>
</UL>
<LI><A HREF="#Heading3">Using the Messaging API (MAPI)</A>
<UL>
<LI><A HREF="#Heading4">What Is MAPI?</A>
<LI><A HREF="#Heading5">Win95 Logo Requirements</A>
<LI><A HREF="#Heading6">Advanced Use of MAPI</A>
</UL>
<LI><A HREF="#Heading7">Using the WinInet Classes</A>
<LI><A HREF="#Heading8">Using Internet Server API (ISAPI) Classes</A>
</UL>
<P>
<HR SIZE="4">
<CENTER>
<H1></H1>
</CENTER>
<H2><A NAME="Heading1"></A>Using Windows Sockets</H2>
<P>There are a number of ways your applications can communicate with other applications
through a network like the Internet. This chapter introduces you to the concepts
involved with these programming techniques. Subsequent chapters cover some of these
concepts in more detail.</P>
<P>Before the Windows operating system even existed, the Internet existed. As it
grew, it became the largest TCP/IP network in the world. The early sites were UNIX
machines, and a set of conventions called Berkeley sockets became the standard for
TCP/IP communication between UNIX machines on the Internet. Other operating systems
implemented TCP/IP communications, too, which contributed immensely to the Internet's
growth. On those operating systems, things were becoming messy, with a wide variety
of proprietary implementations of TCP/IP. Then a group of more than 20 vendors banded
together to create the Winsock specification.</P>
<P>The Winsock specification defines the interface to a DLL, typically called WINSOCK.DLL
or WSOCK32.DLL. Vendors write the code for the functions themselves. Applications
can call the functions, confident that each function's name, parameter meaning, and
final behavior are the same no matter which DLL is installed on the machine. For
example, the DLLs included with Windows 95 and Windows NT are not the same at all,
but a 32-bit Winsock application can run unchanged on a Windows 95 or Windows NT
machine, calling the Winsock functions in the appropriate DLL.</P>
<BLOCKQUOTE>
<P>
<HR>
<strong>NOTE:</strong> Winsock isn't confined to TCP/IP communication. IPX/SPX support is
the second protocol supported, and there will be others. For more information, check
the Winsock specification itself. The Stardust Labs Winsock Resource Page at <A target="_new" HREF="http://www.stardust.com/wsresource/"><B>http://www.stardust.com/wsresource/</B></A>
is a great starting point. n
<HR>
</BLOCKQUOTE>
<P>An important concept in sockets programming is a socket's port. Every Internet
site has a numeric address called an <I>IP address,</I> typically written as four
numbers separated by dots: <B>198.53.145.3</B>, for example. Programs running on
that machine are all willing to talk, by using sockets, to other machines. If a request
arrives at <B>198.53.145.3</B>, which program should handle it?</P>
<P>Requests arrive at the machine, carrying a <I>port number--</I>a number from 1,024
and up that indicates which program the request is intended for. Some port numbers
are reserved for standard use; for example, Web servers traditionally use port 80
to listen for Web document requests from client programs like Netscape Navigator.</P>
<P>Most socket work is <I>connection-based</I>: Two programs form a connection with
a socket at each end and then send and receive data along the connection. Some applications
prefer to send the data without a connection, but there is no guarantee that this
data will arrive. The classic example is a time server that regularly sends out the
current time to every machine near it without waiting until it is asked. The delay
in establishing a connection might make the time sent through the connection outdated,
so it makes sense in this case to use a connectionless approach.</P>
<P>
<H3><A NAME="Heading2"></A>Winsock in MFC</H3>
<P>At first, sockets programming in Visual C++ meant making API calls into the DLL.
Many developers built socket classes to encapsulate these calls. Visual C++ 2.1 introduced
two new classes: CAsyncSocket and CSocket (which inherits from CAsyncSocket). These
classes handle the API calls for you, including the startup and cleanup calls that
would otherwise be easy to forget.</P>
<P>Windows programming is <I>asynchronous</I>: lots of different things happen at
the same time. In older versions of Windows, if one part of an application was stuck
in a loop or otherwise hung up, the entire application--and sometimes the entire
operating system--would stick or hang with it. This is obviously something to avoid
at all costs. Yet a socket call, perhaps a call to read some information through
a TCP/IP connection to another site on the Internet, might take a long time to complete.
(A function that is waiting to send or receive information on a socket is said to
be <I>blocking</I>.) There are three ways around this problem:</P>
<UL>
<LI>Put the function that might block in a thread of its own. The thread will block,
but the rest of the application will carry on.
<P>
<LI>Have the function return immediately after making the request, and have another
function check regularly (<I>poll</I> the socket) to see whether the request has
completed.
<P>
<LI>Have the function return immediately, and send a Windows message when the request
has completed.
</UL>
<P>The first option was not available until recently, and the second is inefficient
under Windows. Most Winsock programming adopts the third option. The class CAsyncSocket
implements this approach. For example, to send a string across a connected socket
to another Internet site, you call that socket's Send() function. Send() doesn't
necessarily send any data at all; it tries to, but if the socket isn't ready and
waiting, Send() just returns. When the socket is ready, a message is sent to the
socket window, which catches it and sends the data across. This is called <I>asynchronous
Winsock programming.</I></P>
<BLOCKQUOTE>
<P>
<HR>
<strong>NOTE:</strong> Winsock programming isn't a simple topic; entire books have been
written on it. If you decide that this low-level sockets programming is the way to
go, building standard programs is a good way to learn the process. n
<HR>
</BLOCKQUOTE>
<P><B><I>CAsyncSocket</I>  </B>The CAsyncSocket class is a wrapper class
for the asynchronous Winsock calls. It has a number of useful functions that facilitate
using the Winsock API. Table 18.1 lists the CAsyncSocket member functions and responsibilities.</P>
<P>
<H4>Table 18.1  CAsyncSocket Member Functions</H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"><B>Method Name</B></TD>
<TD ALIGN="LEFT"><B>Description</B></TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">Accept</TD>
<TD ALIGN="LEFT">Handles an incoming connection on a listening socket, filling a new socket with the
address information.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">AsyncSelect</TD>
<TD ALIGN="LEFT">Requests that a Windows message be sent when a socket is ready.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">Attach</TD>
<TD ALIGN="LEFT">Attaches a socket handle to a CAsyncSocket instance so that it can form a connection
to another machine.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">Bind</TD>
<TD ALIGN="LEFT">Associates an address with a socket.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">Close</TD>
<TD ALIGN="LEFT">Closes the socket.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">Connect</TD>
<TD ALIGN="LEFT">Connects the socket to a remote address and port.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">Create</TD>
<TD ALIGN="LEFT">Completes the initialization process begun by the constructor.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">Detach</TD>
<TD ALIGN="LEFT">Detaches a previously attached socket handle.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">FromHandle</TD>
<TD ALIGN="LEFT">Returns a pointer to the CAsyncSocket attached to the handle it was passed.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">GetLastErro</TD>
<TD ALIGN="LEFT">Returns the error code of the socket. After an operation fails, call GetLastError
to find out why.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">GetPeerName</TD>
<TD ALIGN="LEFT">Finds the IP address and port number of the remote socket that the calling object
socket is connected to, or fills a socket address structure with that information.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">GetSockName</TD>
<TD ALIGN="LEFT">Returns the IP address and port number of this socket, or fills a socket address
structure with that information.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">GetSockOpt</TD>
<TD ALIGN="LEFT">Returns the currently set socket options.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">IOCtl</TD>
<TD ALIGN="LEFT">Sets the socket mode most commonly to blocking or non-blocking.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">Listen</TD>
<TD ALIGN="LEFT">Instructs a socket to watch for incoming connections.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">OnAccept</TD>
<TD ALIGN="LEFT">Handles the Windows message generated when a socket has an incoming connection to
accept (often overridden by derived classes).</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">OnClose</TD>
<TD ALIGN="LEFT">Handles the Windows message generated when a socket closes (often overridden by derived
classes).</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">OnConnect</TD>
<TD ALIGN="LEFT">Handles the Windows message generated when a socket becomes connected or a connection
attempt ends in failure (often overridden by derived classes).</TD>
</TR>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -