Hello,
I am having a trouble with the proxy I am making with C++.
I am connecting a client to a server trough my program.
client talks to my program, program talks to server
server talks to my program, program talks to client
Like that. However, when I receive something from the client and send it to the server, it all goes fine. But when I receive from the server - which goes fine - and try to send to the client something goes terribly wrong.
Which happens in the cltosv function.
#include <cstdio>
#include <winsock2.h>
#include <iostream>
#include <string>
using namespace std;
struct incSockets
{
SOCKET* sv;
SOCKET* cl;
sockaddr_in* clSockAddr;
sockaddr_in* svSockAddr;
};
void filter( char* packet, int nPacketLen )
{
while (nPacketLen > 0)
{
cout << "(" << int(*packet) << ")";
//cout << "" << (*packet) << "";
packet++;
nPacketLen--;
}
cout << endl;
}
//
// Error String
//
char *ErrText( int err )
{
// Displays a nicer error message.
}
DWORD WINAPI svtocl(void* param)
{
cout << "Arived in thread SVTOCL" << endl;
struct incSockets* incs = static_cast<struct incSockets*>(param);
SOCKET cl = *incs->cl;
SOCKET sv = *incs->sv;
sockaddr_in clSockAddr = *incs->clSockAddr;
sockaddr_in svSockAddr = *incs->svSockAddr;
char buffer[20480];
size_t bytesReceived;
int fromlen = sizeof(svSockAddr);
int bytes_sent;
while(true){
bytesReceived = recvfrom(sv, buffer, 20480, 0, (struct sockaddr *)&svSockAddr, &fromlen);
if(bytesReceived == 4294967295) {
cout << "Servers: " << ErrText(WSAGetLastError()) << endl;
}
else if(bytesReceived > 0)
{
cout << "Server: ";
filter(buffer, bytesReceived);
bytes_sent = sendto(cl, buffer, bytesReceived, 0, (struct sockaddr*)&clSockAddr, sizeof(struct sockaddr_in));
//cout << "Bytes Sent: " << bytes_sent << endl;
if(bytes_sent < 0)
{
cout << ErrText(WSAGetLastError()) << endl;
}
}
else
{
cout << "Server bytes: " << bytesReceived << endl;
}
}
}
DWORD WINAPI cltosv(void* param)
{
cout << "Arived in thread CLTOSV" << endl;
struct incSockets* incs = static_cast<struct incSockets*>(param);
SOCKET cl = *incs->cl;
SOCKET sv = *incs->sv;
sockaddr_in clSockAddr = *incs->clSockAddr;
sockaddr_in svSockAddr = *incs->svSockAddr;
char buffer[20480];
size_t bytesReceived;
int fromlen = sizeof(clSockAddr);
int bytes_sent;
while(true){
//bytesReceived = recvfrom(cl, buffer, sizeof(buffer), 0, &client, &client_length);
bytesReceived = recvfrom(cl, buffer, 20480, 0, (struct sockaddr *)&clSockAddr, &fromlen);
if(bytesReceived == 4294967295) {
//cout << "Client: " << ErrText(WSAGetLastError()) << endl;
}
else if(bytesReceived > 0)
{
cout << "Client: ";
filter(buffer, bytesReceived);
bytes_sent = sendto(sv, buffer, bytesReceived, 0, (struct sockaddr*)&svSockAddr, sizeof(struct sockaddr_in));
cout << "Bytes Sent: " << bytes_sent << endl;
if(bytes_sent < 0)
{
cout << ErrText(WSAGetLastError()) << endl;
}
}
}
}
int main(int argc, char** argv) {
struct incSockets incs;
const int iReqWinsockVer = 2;
WSADATA wsaData;
if (WSAStartup(MAKEWORD(iReqWinsockVer,0), &wsaData)==0)
{
if (LOBYTE(wsaData.wVersion) >= iReqWinsockVer)
{
SOCKET cl = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
SOCKET sv = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (cl==INVALID_SOCKET)
{
cout << "1" << ErrText(WSAGetLastError()) << endl;
}
if(sv == INVALID_SOCKET)
{
cout << "2" << ErrText(WSAGetLastError()) << endl;
}
sockaddr_in clSockAddr;
memset(&clSockAddr, 0, sizeof(clSockAddr));
clSockAddr.sin_family = AF_INET;
clSockAddr.sin_port = htons(1339);
clSockAddr.sin_addr.S_un.S_addr = INADDR_ANY;
if (bind(cl, (sockaddr*)(&clSockAddr), sizeof(clSockAddr))!=0)
{
cout << "3" << ErrText(WSAGetLastError()) << endl;
}
sockaddr_in svSockAddr;
memset(&svSockAddr, 0, sizeof(svSockAddr));
svSockAddr.sin_family = AF_INET;
svSockAddr.sin_port = htons(7777);
svSockAddr.sin_addr.S_un.S_addr = inet_addr("94.75.229.241");
if(connect(sv, (sockaddr*)(&svSockAddr), sizeof(svSockAddr))!=0)
{
cout << "4" << ErrText(WSAGetLastError()) << endl;
}
incs.clSockAddr = &clSockAddr;
incs.svSockAddr = &svSockAddr;
incs.cl = &cl;
incs.sv = &sv;
void* pIncs = &incs;
DWORD threadId1;
CreateThread(0, 0, cltosv, pIncs, 0, &threadId1);
DWORD threadId2;
CreateThread(0, 0, svtocl, pIncs, 0, &threadId2);
while(true) {}
closesocket(cl);
closesocket(sv);
}
else
{
// Required version not available
}
// Cleanup winsock
if (WSACleanup()!=0)
{
// cleanup failed
}
}
else
{
// startup failed
}
return 0;
}