Programming with Microsoft Visual C++.NET 6ed [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Programming with Microsoft Visual C++.NET 6ed [Electronic resources] - نسخه متنی

George Shepherd, David Kruglinski

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
توضیحات
افزودن یادداشت جدید


Building a Web Server Using <i class="emphasis">CHttpBlockingSocket</i>






Building a Web Server Using CHttpBlockingSocket


If you need a Web server, your best bet is to buy one or to use Microsoft Internet Information Services (IIS), which comes bundled with Windows NT/2000 Server. You can install it on Windows 2000 Professional and on Windows XP as well. Of course, you'll learn more if you build your own server and you'll also have a useful diagnostic tool. And what if you need features that IIS can't deliver? Suppose you want to add Web server capability to an existing Windows application, or suppose you have a custom ActiveX control that sets up its own non-HTTP TCP connection with the server. Take a good look at the server code in Ex28a. It might work as a foundation for your next custom server application.


Ex28a Server Limitations


The server part of the Ex28a program honors GET requests for files, and it has logic for processing POST requests. These are the two most common HTTP request types. Ex28a will not, however, launch Common Gateway Interface (CGI) scripts or load Internet Server Application Programming Interface (ISAPI) DLLs. Ex28a makes no provision for security, and it doesn't have FTP capabilities. Other than that, it's a great server! If you want the missing features, just write the code for them yourself.



Ex28a Server Architecture


You'll soon see that Ex28a combines an HTTP server, a Winsock HTTP client, and two WinInet HTTP clients. All three clients can talk to the built-in server or to any other server on the Internet. Any client program, including the Telnet utility and standard browsers such as Microsoft Internet Explorer, can communicate with the Ex28a server. You'll examine the client sections a little later in this chapter.

Ex28a is a standard MFC SDI document–view application with a view class derived from CEditView. The main menu includes Start Server and Stop Server menu choices as well as a Configuration command that brings up a tabbed dialog box for setting the home directory, the default file for blind GETs, and the listening port number (usually 80).

The Start Server command handler starts a global socket listening and then launches a thread, as in the simplified HTTP server described previously. Look at the ServerThreadProc function included in the

ServerThread.cpp file of the Ex28a project on the companion CD. Each time a server thread processes a request, it logs the request by sending a message to the CEditView window. It also sends messages for exceptions, such as bind errors.

The primary job of the server is to deliver files. It first opens a file, storing a CFile pointer in pFile, and then it reads 5 KB (SERVERMAXBUF) blocks and writes them to the socket sConnect, as shown in the code below:

char* buffer = new char[SERVERMAXBUF];
DWORD dwLength = pFile->GetLength();
nBytesSent = 0;
DWORD dwBytesRead = 0;
UINT uBytesToRead;
while(dwBytesRead < dwLength) {
uBytesToRead = min(SERVERMAXBUF, dwLength - dwBytesRead);
VERIFY(pFile->Read(buffer, uBytesToRead) == uBytesToRead);
nBytesSent += sConnect.Write(buffer, uBytesToRead, 10);
dwBytesRead += uBytesToRead;
}

The server is programmed to respond to a GET request for a phony file named Custom. It generates some HTML code that displays the client's IP address, port number, and a sequential connection number. This is one possibility for server customization.

The server normally listens on a socket bound to address INADDR_ANY. This is the server's default IP address determined by the Ethernet board or assigned during your connection to your ISP. If your server computer has several IP addresses, you can force the server to listen to one of them by filling in the Server IP Address box on the Advanced tab of the Configuration dialog box. You can also change the server's listening port number on the Server tab. If you choose port 90, for example, browser users would connect to http://localhost:90.

The leftmost status bar indicator pane displays "Listening" when the server is running.



Using the Win32 TransmitFile Function


With Windows NT/2000/XP, you can make your server more efficient by using the Win32 TransmitFile function in place of the CFile::Read loop in the code excerpt shown above. TransmitFile sends bytes from an open file directly to a socket and is highly optimized. The Ex28a ServerThreadProc function contains the following line:

if (::TransmitFile(sConnect, (HANDLE) pFile >m_hFile, dwLength, 0, 
NULL, NULL, TF_DISCONNECT))

If you have Windows NT/2000/XP, uncomment the line

#define USE_TRANSMITFILE

at the top of

ServerThread.cpp to activate the TransmitFile logic.



Building and Testing Ex28a


Open the Ex28a project in Visual C++ .NET, and then build the project. A directory under Ex28a, called Website, contains some HTML files and is set up as the Ex28a server's home directory, which appears to clients as the server's root directory.





Note

If you have another HTTP server running on your computer, stop it now. If you have installed IIS along with Windows NT/2000 Server, it is probably running now, so you must stop it. Ex28a reports a bind error (10048) if another server is already listening on port 80.


Run the program from the debugger, and then choose Start Server from the Internet menu. Now go to your Web browser and type localhost. You should see the Welcome To The Inside Visual C++ .NET Home Page complete with all graphics. The Ex28a window should look like this.


Look at the Visual C++ .NET debug window for a listing of the client's request headers.

If you click the browser's Refresh button, you might notice Ex28a error messages like this:

WINSOCK ERROR—SERVER: Send error #10054 — 10/05/99 04:34:10 GMT

This tells you that the browser read the file's modified date from the server's response header and figured out that it didn't need the data because it already had the file in its cache. The browser then closed the socket, and the server detected an error. If the Ex28a server were smarter, it would have checked the client's If-Modified-Since request header before sending the file.

Of course, you can test the server on your intranet. Start the server on one computer, and then run the browser from another, typing in the server's host name as it appears in the HOSTS file.



/ 319