首页 > 代码库 > HTTP转发小Demo

HTTP转发小Demo

工作中一个方案可行性预研写的小Demo,一晚上搞定的小程序.

主要功能是一个Http透明转发:

1.监听一个端口, 接收浏览器的连接请求.

2. 接收浏览器发出的请求数据, 将这些转发给一个指定的服务器.

3. 接收服务器的应答,将应答发送给浏览器.

代码都在一个文件中:

// httptranschannel.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "httptranschannel.h"
#include <iostream>
#include <string>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

const int PORT_LOCAL = 8084;
//const char* SERVER_IP = "192.168.84.133";
//const int SERVER_PORT = 80;
const int PACKAGE_BUFFER_SIZE = 100*1024;	// 分包大小.

SOCKET g_listenSocket = INVALID_SOCKET;
SOCKET g_acceptSocket = INVALID_SOCKET;

SOCKET g_serverSocket = INVALID_SOCKET;	// 同服务器之间的连接Socket.

// 唯一的应用程序对象

CWinApp theApp;

using namespace std;

int InitSocket()
{
	// Initialize Winsock
	WSADATA wsaData;
	int iResult = 0;
	iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
	if (iResult != NO_ERROR) {
		wprintf(L"WSAStartup() failed with error: %d\n", iResult);
		return 1;
	}
	return 0;
}

int ListenSocket()
{
	if ( g_listenSocket == INVALID_SOCKET )
	{
		int iResult = 0;

		sockaddr_in service;

		//----------------------
		// Create a SOCKET for listening for incoming connection requests.
		g_listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
		if (g_listenSocket == INVALID_SOCKET) {
			wprintf(L"socket function failed with error: %ld\n", WSAGetLastError());
			WSACleanup();
			return 1;
		}
		//----------------------
		// The sockaddr_in structure specifies the address family,
		// IP address, and port for the socket that is being bound.
		service.sin_family = AF_INET;
		service.sin_addr.s_addr = ADDR_ANY; // inet_addr("127.0.0.1");
		service.sin_port = htons(PORT_LOCAL);

		iResult = bind(g_listenSocket, (SOCKADDR *) & service, sizeof (service));
		if (iResult == SOCKET_ERROR) {
			wprintf(L"bind function failed with error %d\n", WSAGetLastError());
			iResult = closesocket(g_listenSocket);
			if (iResult == SOCKET_ERROR)
				wprintf(L"closesocket function failed with error %d\n", WSAGetLastError());
			WSACleanup();
			return 1;
		}
		//----------------------
		// Listen for incoming connection requests 
		// on the created socket
		if (listen(g_listenSocket, SOMAXCONN) == SOCKET_ERROR)
			wprintf(L"listen function failed with error: %d\n", WSAGetLastError());

		wprintf(L"Listening on socket...\n");

		//----------------------
		// Create a SOCKET for accepting incoming requests.
		wprintf(L"Waiting for client to connect...\n");
	}
	
	//----------------------
	// Accept the connection.
	g_acceptSocket = accept(g_listenSocket, NULL, NULL);
	if (g_acceptSocket == INVALID_SOCKET) {
		wprintf(L"accept failed with error: %ld\n", WSAGetLastError());
		closesocket(g_listenSocket);
		WSACleanup();
		return 1;
	} else
		wprintf(L"Client connected.\n");

	//g_acceptSocket = accept(g_listenSocket, NULL, NULL);
	//if (g_acceptSocket == INVALID_SOCKET) {
	//	wprintf(L"accept failed with error: %ld\n", WSAGetLastError());
	//	closesocket(g_listenSocket);
	//	WSACleanup();
	//	return 1;
	//} else
	//	wprintf(L"Client connected.\n");

	return 0;
}

int DestroySocket()
{
	if ( INVALID_SOCKET != g_listenSocket )
	{
		int iResult = closesocket(g_listenSocket);
		if (iResult == SOCKET_ERROR) {
			wprintf(L"closesocket function failed with error %d\n", WSAGetLastError());
			WSACleanup();
			return 1;
		}
	}
	
	WSACleanup();
	return 0;
}

int ReceivFromSocket( SOCKET hSocket, char* pBuf, const int bufLen, int& dataLen, bool& disconnected )
{
	disconnected = false;

	fd_set fdSocket;        // 所有可用套节字集合
	FD_ZERO(&fdSocket);
	FD_SET( hSocket, &fdSocket);
	timeval tv;
	tv.tv_sec   =   0; 
	tv.tv_usec   =   0; 
	int nHasData = http://www.mamicode.com/select( 1,&fdSocket, NULL, NULL, &tv) ; >