static int initWebs(int demo)
{
struct hostent *hp;
struct in_addr intaddr;
char host[128], dir[128], webdir[128];
char *cp;
char_t wbuf[128];
/*
* Initialize the socket subsystem
*/
socketOpen();
#ifdef USER_MANAGEMENT_SUPPORT
/*
* Initialize the User Management database
*/
umOpen();
umRestore(T("umconfig.txt"));
#endif
/*
* Define the local Ip address, host name, default home page and the
* root web directory.
*/
if (gethostname(host, sizeof(host)) < 0) {
error(E_L, E_LOG, T("Can't get hostname"));
return -1;
}
if ((hp = gethostbyname(host)) == NULL) {
error(E_L, E_LOG, T("Can't get host address"));
return -1;
}
memcpy((char *) &intaddr, (char *) hp->h_addr_list[0],
(size_t) hp->h_length);
/*
* Set ../web as the root web. Modify this to suit your needs
* A "-demo" option to the command line will set a webdemo root
*/
getcwd(dir, sizeof(dir));
if ((cp = strrchr(dir, '/'))) {
*cp = '\0';
}
if (demo) {
sprintf(webdir, "%s/%s", dir, demoWeb);
} else {
sprintf(webdir, "%s/%s", dir, rootWeb);
}
/*
* Configure the web server options before opening the web server
*/
websSetDefaultDir(webdir);
cp = inet_ntoa(intaddr);
ascToUni(wbuf, cp, min(strlen(cp) + 1, sizeof(wbuf)));
websSetIpaddr(wbuf);
ascToUni(wbuf, host, min(strlen(host) + 1, sizeof(wbuf)));
websSetHost(wbuf);
/*
* Configure the web server options before opening the web server
*/
websSetDefaultPage(T("default.asp"));
websSetPassword(password);
/*
* Open the web server on the given port. If that port is taken, try
* the next sequential port for up to "retries" attempts.
*/
websOpenServer(port, retries);
/*
* First create the URL handlers. Note: handlers are called in sorted order
* with the longest path handler examined first. Here we define the security
* handler, forms handler and the default web page handler.
*/
websUrlHandlerDefine(T(""), NULL, 0, websSecurityHandler,WEBS_HANDLER_FIRST);
websUrlHandlerDefine(T("/goform"), NULL, 0, websFormHandler, 0);
websUrlHandlerDefine(T("/cgi-bin"), NULL, 0, websCgiHandler, 0);
websUrlHandlerDefine(T(""), NULL, 0, websDefaultHandler,WEBS_HANDLER_LAST);
/*
* Now define two test procedures. Replace these with your application
* relevant ASP script procedures and form functions.
*/
websAspDefine(T("aspTest"), aspTest);
websFormDefine(T("formTest"), formTest);
websFormDefine(T("formTest1"), formTest1);
/*
* Create the Form handlers for the User Management pages
*/
#ifdef USER_MANAGEMENT_SUPPORT
formDefineUserMgmt();
#endif
/*
* Create a handler for the default home page
*/
websUrlHandlerDefine(T("/"), NULL, 0, websHomePageHandler, 0);
return 0;
}
1、int socketOpen()
int socketOpen()
int socketOpen()
{
#if (defined (CE) || defined (WIN))
WSADATA wsaData;
#endif
if (++socketOpenCount > 1) {
return 0;
}
#if (defined (CE) || defined (WIN))
if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0) {
return -1;
}
if (wsaData.wVersion != MAKEWORD(1,1)) {
WSACleanup();
return -1;
}
#endif
socketList = NULL;
socketMax = 0;
socketHighestFd = -1;
return 0;
}
註釋:
#if (defined (CE) || defined (WIN)) 表示只要定義了宏 CE 或者WIN則會執行"WSADATA wsaData;"
static int socketOpenCount = 0; /* Number of task using sockets */ 使用SOCKET的數量,初始化的時候應該爲1
socketList socket 隊列;
socketMax soket最大個數;
socketHighestFd Highest socket fd opened 最大的socket
2、gethostname
簡述:返回本地主機的標準主機名。
#include <Winsock2.h>
int PASCAL FAR gethostname(char FAR *name, int namelen);
name: 一個指向將要存放主機名的緩衝區指針。
namelen:緩衝區的長度。
編輯本段註釋:
該函數把本地主機名存放入由name參數指定的緩衝區中。返回的主機名是一個以NULL結束的字符串。主機名的形式取決於Windows Sockets實現-它可能是一個簡單的主機名,或者是一個域名。然而,返回的名字必定可以在gethostbyname()和WSAAsyncGetHostByName()中使用。
返回值:
如果沒有錯誤發生,gethostname()返回0。否則它返回SOCKET_ERROR。應用程序可以通過WSAGetLastError()來得到一個特定的錯誤代碼。
錯誤代碼:
WSAEFAULT 名字長度參數太小。
WSANOTINTIALISED 在應用這個API前,必須成功地調用WSAStartup()。
WSAENTDOWN Windows Sockets實現檢測到了網絡子系統的錯誤。
WSAEINPROGRESS 一個阻塞的Windows Sockets操作正在進行。