try

本文介绍了一个简易代理服务器的实现过程,使用 C/C++ 编程语言,并基于 Winsock 库进行网络通信。该代理服务器能够监听本地端口,接收客户端请求,解析请求中的目标地址并转发到互联网上的服务器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#undef UNICODE
#include "stdafx.h"
#include <string>
#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <iphlpapi.h>
#include <stdio.h>
#define DEFAULT_PORT "27015"
//#define INTERNET_PORT "27014"
#define DEFAULT_BUFLEN 10024
#pragma comment(lib, "Ws2_32.lib")
using namespace std;
SOCKET GLOBAL[1000];
//really important!!
int main(void)
{
WSADATA wsaData;
int iResult;
string s_recv_array[DEFAULT_BUFLEN];
SOCKET ListenSocket = INVALID_SOCKET;
SOCKET ClientSocket = INVALID_SOCKET;
struct addrinfo *result = NULL;
struct addrinfo hints;
int iSendResult;
//char recv_internet[DEFAULT_BUFLEN];
//int recvbuflen = DEFAULT_BUFLEN;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
while (1);
return 1;
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
// Resolve the server address and port
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
if (iResult != 0) {
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
while (1);
return 1;
}
// Create a SOCKET for connecting to server
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
while (1);
return 1;
}
// Setup the TCP listening socket
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("bind failed with error: %d\n", WSAGetLastError());
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
while (1);
return 1;
}
freeaddrinfo(result);
iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
printf("listen failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
while (1);
return 1;
}
// Receive until the peer shuts down the connection
int i = 0;
while (1){
ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
printf("accept failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
while (1);
return 1;
}
GLOBAL[i] = ClientSocket;
HANDLE hand = CreateThread(NULL, 0, proxy, &GLOBAL[i], 0, NULL);
i = (i+1) % 1000;
}
// shutdown the connection since we're done
iResult = shutdown(ClientSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
while (1);
return 1;
}
// cleanup
printf("hello world\n");
closesocket(ClientSocket);
WSACleanup();
while (1);
return 0;
}
DWORD WINAPI proxy(LPVOID aa)
{
char recv_internet[DEFAULT_BUFLEN];
struct addrinfo *result = NULL;
struct addrinfo hints;
SOCKET ClientSocket = *(SOCKET*)aa;
char recvbuf[DEFAULT_BUFLEN];
int iResult;
SOCKET ConnectSocket;
do{
recvbuf[iResult] = '\0';
if (iResult > 0) {
printf("received from my computer:==============================================\n");
}
string s_recv;
s_recv = recvbuf;
cout << s_recv << endl;
std::size_t found = s_recv.find(" ");
std::size_t found2 = s_recv.find(" ",found+1);
string second_word = s_recv.substr(found + 1, found2 - found - 1);
string tmp;
string port = "80";
cout << "second: " << second_word << endl;
if (second_word.find("http://") != string::npos) {
found = second_word.find("http://");
std::size_t found2 = second_word.find("/", found + 7);
tmp = second_word.substr(found + 7, found2 - found - 7);


found2 = second_word.find(":", found+6);
if(found2 != string::npos)
port = second_word.substr(found2 + 1);
}
else {
found = second_word.find("/");
tmp = second_word.substr(0, found);


found2 = second_word.find(":");
if(found2 != string::npos)
port = second_word.substr(found2 + 1);
}
found = tmp.find("www.");
if (found != string::npos)
tmp = tmp.substr(found + 4);
cout << "tmp: " << tmp << endl;
cout << "port: " << port << endl;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
if (getaddrinfo(tmp.c_str(), port.c_str(), &hints, &result) != 0) {
printf("getaddrinfo failed with error\n");
WSACleanup();
while (1);
return 1;
}
ConnectSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
cout << "socket failed with error :" << WSAGetLastError() << endl;
WSACleanup();
while (true);
return 1;
}
if (connect(ConnectSocket, result->ai_addr, result->ai_addrlen) == SOCKET_ERROR) {
printf("socket failed with error\n");
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
while (1);
return 1;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
while (1);
return 1;
}
iResult = send(ConnectSocket, recvbuf, iResult, 0);
if (iResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
while (1);
return 1;
}
printf("Sent to internet:==============================================\n%s", recvbuf);
iResult = recv(ConnectSocket, recv_internet, DEFAULT_BUFLEN, 0);
recv_internet[iResult] = '\0';
if (iResult > 0)
printf("receive from internet:==============================================\n%s", recv_internet);
int iSendResult = send(ClientSocket, recv_internet, iResult, 0);
if (iSendResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
while (1);
return 1;
}
printf("Sent to my_computer: ==============================================\n%s", recv_internet);
int iResult = recv(ClientSocket, recvbuf, DEFAULT_BUFLEN, 0);
}while(iResult > 0);
iResult = shutdown(ClientSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
while (1);
return 1;
}
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
while (1);
return 1;
}
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值