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

📄 httpmt.c

📁 http服务器
💻 C
📖 第 1 页 / 共 2 页
字号:
//
// HTTPMT.C		HTTP Server Functions
//
//				Multi-Threaded Model
//

#include "httpMT.h"
#include <stdio.h>
//
// Internal linkage
//
static SOCKET listenSocket;
static char szWebRoot[_MAX_PATH];
static HWND	ghwnd;
static UINT guAppMsg;

HANDLE ghExit;
DWORD  gdwListenThread;

////////////////////////////////////////////////////////////

BOOL StartHTTP(LPHTTPSERVINFO lpInfo)
{
	SOCKADDR_IN		saServer;		
	LPSERVENT		lpServEnt;		
	unsigned		ThreadAddr;
	char			szBuf[256];		
	char			szAddress[80];
	DWORD			dwAddrStrLen;
    int				nRet;			

	SOCKADDR_IN	SockAddr;
	SOCKET		socketClient;
	DWORD		dwClientThread;
	int         nLen;
	//
	// Save the Window handle and message
	// ID for further use
	//
	ghwnd    = lpInfo->hwnd;
	guAppMsg = lpInfo->uMsgApp;
	if (lpInfo->lpRootDir != NULL)
		strcpy(szWebRoot, lpInfo->lpRootDir);
	else
		strcpy(szWebRoot, "/WebPages");

	//
	// Create the exit signal event object
	//
	ghExit = CreateEvent(NULL,		// Security
						 TRUE,		// Manual reset
						 FALSE,		// Initial State
						 NULL);		// Name
	if (ghExit == NULL)
		return FALSE;

 	//
	// Create a TCP/IP stream socket
	//
	listenSocket = socket(AF_INET, 
						  SOCK_STREAM, 
						  IPPROTO_TCP);
	if (listenSocket == INVALID_SOCKET)
	{
		LogWinSockError(ghwnd, 
						"Could not create listen socket",
						WSAGetLastError());
		return FALSE;
	}

	//
	// If a specific port number was specified
	// then use it
	//
	if (lpInfo->nPort != 0)
		saServer.sin_port = htons(lpInfo->nPort);
	else
	{
		//
		// Find a port number
		//
		lpServEnt = getservbyname("http", "tcp");
		if (lpServEnt != NULL)
			saServer.sin_port = lpServEnt->s_port;
		else
			saServer.sin_port = htons(HTTPPORT);
	}

	//
	// Fill in the rest of the address structure
	//
	saServer.sin_family = AF_INET;
	saServer.sin_addr.s_addr = INADDR_ANY;

	//
	// bind our name to the socket
	//
	nRet = bind(listenSocket, 
				(LPSOCKADDR)&saServer, 
				sizeof(struct sockaddr));
	if (nRet == SOCKET_ERROR)
	{
		LogWinSockError(ghwnd,
						 "bind() error",
						 WSAGetLastError());
		closesocket(listenSocket);
		return FALSE;
	}

	//
	// Set the socket to listen
	//
	nRet = listen(listenSocket, SOMAXCONN);
	if (nRet == SOCKET_ERROR)
	{
		LogWinSockError(ghwnd,
						 "listen() error",
						 WSAGetLastError());
		closesocket(listenSocket);
		return FALSE;
	}
//////////////////////////////////////////////////////

	//
	// Create the listening thread
	//
	gdwListenThread = _beginthreadex(
							NULL,		 // Security
							0,			 // Stack size
							ListenThread,// Function address
							&ghExit,	 // Argument
							0,			 // Init flag
							&ThreadAddr);// Thread address
	if (!gdwListenThread)
	{
		LogEvent(ghwnd, 
				 "Could not create listening thread: %d",
				 GetLastError());
		closesocket(listenSocket);
		return FALSE;
	}

//////////////////////////add mythread//////////////////////////
/*
	gdwListenThread =CreateThread(NULL,0,listenThread,NULL,NULL,NULL);
	if (!gdwListenThread)
	{
		closesocket(listenSocket);
		return FALSE;
	}
*/	
/////////////////////////////////////////////////////////////////
	//
	// Display the host name and address
	//
	gethostname(szBuf, sizeof(szBuf));
	dwAddrStrLen = sizeof(szAddress);
	GetLocalAddress(szAddress, &dwAddrStrLen);
	LogEvent(ghwnd, 
			 "HTTP Server Started: %s [%s] on port %d",
			 szBuf,
			 szAddress,
			 htons(saServer.sin_port));

	return TRUE;
}
unsigned __stdcall listenThread()
{
	int nLen;
	SOCKET		socketClient;
	unsigned	ThreadAddr;
	DWORD		dwClientThread;
	SOCKADDR_IN	SockAddr;


	while(1)
	{
		//
		// Block on accept()
		//
		nLen = sizeof(SOCKADDR_IN);
		socketClient = accept(listenSocket,
							  (LPSOCKADDR)&SockAddr,
							  &nLen);    
		//////////////////////////////////there is a mistake!!!
		if (socketClient == INVALID_SOCKET)
		{
			//
			// StopHTTP() closes the listening socket
			// when it wants this thread to stop.
			break;
		}

		//
		// We have a connection
		// 
		LogEvent(ghwnd, 
				 "Connection accepted on socket:%d from:%s",
				 socketClient,
				 inet_ntoa(SockAddr.sin_addr));

		//
		// Allocate parms for client and fill in defaults
		// 

		//
		// Start a client thread to handle this request
		//
		dwClientThread =CreateThread(NULL,0,myThread,&socketClient,NULL,NULL);
        CloseHandle((HANDLE)dwClientThread);

	}
	return 0;
}
unsigned __stdcall myThread(SOCKET mysocket)
{
	BYTE buf[1024];

	char * xmlfile = "<root>\n    <child/>\n    <child/>\n    <child/>\n</root>";

	if (!recv(mysocket, buf, sizeof(buf),0))
	{
		closesocket(mysocket);
		MessageBox(NULL,"recv error!!!","error!!",MB_OK);
		return 0;
	}
	if(buf!=NULL)MessageBox(NULL,buf,"buf:",MB_OK);
	send(mysocket,xmlfile,strlen(xmlfile),0);
	closesocket(mysocket);
	return 0;


}
////////////////////////////////////////////////////////////

void StopHTTP()
{
	int nRet;

	//
	// Signal the exit event
	//
	SetEvent(ghExit);

	//
	// Close the listening socket
	//
	nRet = closesocket(listenSocket);

	//
	// And wait for the listen thread to stop
	//
	nRet = WaitForSingleObject((HANDLE)gdwListenThread, 10000);
	if (nRet == WAIT_TIMEOUT)
		LogEvent(ghwnd, "TIMEOUT waiting for ListenThread");

	CloseHandle((HANDLE)gdwListenThread);
	CloseHandle(ghExit);
	LogEvent(ghwnd, "Server Stopped");
}

////////////////////////////////////////////////////////////

unsigned __stdcall ListenThread(void *pVoid)
{
	SOCKET		socketClient;
	unsigned	ThreadAddr;
	DWORD		dwClientThread;
	SOCKADDR_IN	SockAddr;
	LPREQUEST	lpReq;
	int			nLen;
	DWORD		dwRet;
	HANDLE		hNoClients;
	LPHANDLE	pHandle = (LPHANDLE)pVoid;

	//
	// Initialize client thread count to 0
	//
//	hNoClients = InitClientCount();

	//
	// Loop forever accepting connections
	//
	while(1)
	{
		//
		// Block on accept()
		//
		nLen = sizeof(SOCKADDR_IN);
		socketClient = accept(listenSocket,
							  (LPSOCKADDR)&SockAddr,
							  &nLen);
		if (socketClient == INVALID_SOCKET)
		{
			//
			// StopHTTP() closes the listening socket
			// when it wants this thread to stop.
			break;
		}

		//
		// We have a connection
		// 
		LogEvent(ghwnd, 
				 "Connection accepted on socket:%d from:%s",
				 socketClient,
				 inet_ntoa(SockAddr.sin_addr));

		//
		// Allocate parms for client and fill in defaults
		// 
		lpReq = malloc(sizeof(REQUEST));
		if (lpReq == NULL)
		{
			LogEvent(ghwnd,
					 "No memory for client request");
			continue;
		}
		lpReq->hExit  = *pHandle;
		lpReq->Socket = socketClient;
		lpReq->dwConnectTime = GetTickCount();
		lpReq->hFile = INVALID_HANDLE_VALUE;
		lpReq->dwRecv = 0;;
		lpReq->dwSend = 0;


		//
		// Start a client thread to handle this request
		//
		dwClientThread = _beginthreadex(
							NULL,		  // Security
							0,			  // Stack size
							ClientThread, // Thread function
							lpReq,	      // Argument
						    0,			  // Init flag
						    &ThreadAddr); // Thread address
		if (!dwClientThread)
		{
			LogEvent(ghwnd, "Couldn't start client thread");
		}
		//
        // We won't be using client thread handles,
		// so we close them when they are created.
		// The thread will continue to execute.
        //
        CloseHandle((HANDLE)dwClientThread);

	}

	//
	// Wait for exit event
	//
/*
	WaitForSingleObject((HANDLE)*pHandle, INFINITE);

	//
	// Wait for ALL clients to exit
	//
	dwRet = WaitForSingleObject(hNoClients, 5000);
	if (dwRet == WAIT_TIMEOUT)
	{
		LogEvent(ghwnd,
			"One or more client threads did not exit");
	}
	DeleteClientCount();
*/
	return 0;
}

////////////////////////////////////////////////////////////

unsigned __stdcall ClientThread(void *pVoid)
{
	int nRet;
	BYTE buf[1024];
	LPREQUEST lpReq = (LPREQUEST)pVoid;

	char *html_head  = "<body><h1>file coudn't found</h1></body>";
	char * xmlfile = "<root>\n    <child/>\n    <child/>\n    <child/>\n</root>";
	const char szMsg[256];
	//
	// Count this client
	//
//	IncrementClientCount();

	//
	// Recv the request data
	//
/*
	if (!RecvRequest(lpReq, buf, sizeof(buf)))
	{
		CloseConnection(lpReq);
		free(lpReq);
		DecrementClientCount();
		return 0;
	}

*/
	if (!recv(lpReq->Socket, buf, sizeof(buf),0))
	{
		closesocket(lpReq->Socket);
//		CloseConnection(lpReq);
		free(lpReq);
//		DecrementClientCount();
		MessageBox(NULL,"recv error!!!","error!!",MB_OK);
		return 0;
	}

///////////////add txt file to show the content of buf and lpReq///////////////////////////
/*
	FILE *fp;
  if((fp=fopen("bufAndLpReq.txt","ab"))==NULL)
  {
     printf("cannot open/crate a file!");
     exit(0);
  }
  fwrite(buf,strlen(buf),1,fp);//将从客户端发来的消息备份在本地的目录下;
  fwrite("lpReq:",6,1,fp);
  fwrite(lpReq->hExit,strlen(lpReq->hExit),1,fp);
  fwrite(lpReq->Socket,strlen(lpReq->Socket),1,fp);
  fwrite(lpReq->nMethod,strlen(lpReq->nMethod),1,fp);
  fwrite(lpReq->dwConnectTime,strlen(lpReq->dwConnectTime),1,fp);
  fwrite(lpReq->dwRecv,strlen(lpReq->dwRecv),1,fp);
  fwrite(lpReq->dwSend,strlen(lpReq->dwSend),1,fp);
  fwrite(lpReq->hFile,strlen(lpReq->hFile),1,fp);
  fwrite(lpReq->szFileName,strlen(lpReq->szFileName),1,fp);
  fclose(fp);
  */
	if(buf!=NULL)MessageBox(NULL,buf,"buf:",MB_OK);
//  MessageBox(NULL,"lpReq",lpReq->hExit,MB_OK);
 /////////////////////////////////////////////////////////////////////////////////////////
	//
	// Parse the request info
	//


//	nRet = ParseRequest(lpReq, buf);


/*
	if (nRet)
	{
		SendError(lpReq, nRet);
		CloseConnection(lpReq);
		free(lpReq);
		DecrementClientCount();
		return 0;
	}

	//
	// Send the file to the client
	//
	SendFile(lpReq);
*/
///////////////////////////////////add code to send xml file
//	wsprintf(szMsg, "<body><h1>file coudn't found</h1></body>");
	send(lpReq->Socket,xmlfile,strlen(xmlfile),0);
//	send(lpReq->Socket,xmlfile,strlen(xmlfile),0);
///////////////////////////////////////////////////////////////////////////////
	//
	// Clean up
//	CloseConnection(lpReq);
	closesocket(lpReq->Socket);
	free(pVoid);

	//
	// Subtract this client
	//
//	DecrementClientCount();

⌨️ 快捷键说明

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