Hello everyone,
I'm writing a program to get user's browser packet on (127.0.0.1:8000) and send it from my program to the destination, and receive data from the destination and send it back to the user's browser.
When I execute my program the error "WSAECONNABORTED" is occured.
My code:
#include "stdafx.h"
#pragma comment(lib, "ws2_32")
using namespace HttpSockets;
using namespace std;
using namespace Screen;
using namespace Exceptions;
struct ClientSockets {
Socket S; Socket C;
};
string Receiving(Socket);
DWORD WINAPI ReceiveFromClient(LPVOID lpParam);
DWORD WINAPI ReceiveFromServer(LPVOID lpParam);
HANDLE thread;
int main(int argc, char* argv[])
{
PrintScreen Printer;
try {
Socket SocketServer;
EndPoint EP;
EP.Create("0.0.0.0", 8000);
SocketServer.Bind(EP);
SocketServer.Listen(10);
int x = 0;
while(true) {
ClientSockets Client;
Socket S = SocketServer.Accept();
Socket C;
C.Connect("198.252.206.140", 80);
Client.S = S;
Client.C = C;
CreateThread(NULL, 0, ReceiveFromClient, &Client, 0, NULL);
CreateThread(NULL, 0, ReceiveFromServer, &Client, 0, NULL);
}
}
catch(Exception &ex) {
Printer.Print("\n");
Printer.Print(ex.what(), Red | Intensity);
Printer.Print("\n");
}
system("pause");
return 0;
}
DWORD WINAPI ReceiveFromServer(LPVOID lpParam)
{
ClientSockets *Client = (ClientSockets*)lpParam;
Socket S = Client->S;
Socket C = Client->C;
PrintScreen Printer;
try {
while(true) {
string Buffer = Receiving(C);
if(Buffer != "") {
S.Send((char*)Buffer.c_str());
Printer.Print(Buffer);
}
}
}
catch(Exception &ex) {
Printer.Print("\n");
Printer.Print("ReceiveFromServer: [");
Printer.Print(ex.what(), Red | Intensity);
Printer.Print("]");
Printer.Print("\n");
}
return 0;
}
DWORD WINAPI ReceiveFromClient( LPVOID lpParam )
{
ClientSockets *Client = (ClientSockets*)lpParam;
Socket S = Client->S;
Socket C = Client->C;
PrintScreen Printer;
try {
while(true) {
char buffer[65535];
S.Receive(buffer);
string data;
data.append(buffer);
while(true) {
size_t f = data.find("127.0.0.1:8000");
if(f != string::npos) data.replace(f, string("127.0.0.1:8000").length(), "stackoverflow.com");
else break;
}
C.Send((char*)data.c_str());
}
}
catch(Exception &ex) {
Printer.Print("ReceiveFromClient: [");
Printer.Print(ex.what(), Red | Intensity);
Printer.Print("]");
Printer.Print("\n");
}
return 0;
}
string Receiving(Socket client)
{
string data;
char* Buffer = (char*)malloc(1024);
int Result = client.Receive(Buffer, 1024);
Buffer[Result] = '\0';
if(Result > 0)
{
data.append(Buffer);
}
return data;
}
So, what's the wrong of my code???