我正在创建自己的代理服务器。但是,我不知道如何收听浏览器。而且,我有一些问题:
我认为随着每个代理服务器的工作,我的程序将从客户端(浏览器)获取请求,将其转发到Web服务器,然后从服务器接收内容,最后转发到客户端。
#pragma comment(lib, "Ws2_32.lib")
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <sys/types.h>
#include <iostream>
#include <fcntl.h>
#include <WS2tcpip.h>
#include <sys/stat.h>
#include <io.h>
#include <process.h> /* for getpid() and the exec..() family */
#include <direct.h>
using namespace std;
bool checkSubString(char *str, char*substr)
{
if (strstr(str, substr) != NULL)
{
return true;
}
return false;
}
int main()
{
WSADATA wsData;
WORD ver = MAKEWORD(2, 2);
int wsOK = WSAStartup(ver, &wsData);
if (wsOK != 0)
{
cerr << "cant init winsock" << endl; return 0;
}
char message[1024];
int sockcheck = 0;
sockaddr_in server_input_addr;
memset(&server_input_addr, '0', sizeof(server_input_addr));
memset(&message, '0', sizeof(message));
sockcheck = socket(AF_INET, SOCK_STREAM, 0);
if (sockcheck < 0)
{
cerr << "Error while creating socket!!!\n";
return 0;
}
server_input_addr.sin_family = AF_INET;
server_input_addr.sin_addr.S_un.S_addr = INADDR_ANY;
server_input_addr.sin_port = htons(8888);
bind(sockcheck, (sockaddr*)&server_input_addr, sizeof(server_input_addr));
listen(sockcheck, 5);
int connFd = 0;
int n = 0;
int client_length = sizeof(server_input_addr);
while (1) {
connFd = accept(sockcheck, (sockaddr*)&server_input_addr, &client_length);
if (connFd < 0)
{
cerr << "\nError in accepting message from browser"; return 0;
}
n = _read(connFd, message, 1023);
if (n > 0)
{
cerr << "ERROR reading from socket\n";
}
_write(connFd, "Message received", 15);
_close(connFd);
}
cout << "\nSuccess!!!\n";
}