PDA

View Full Version : [Please delete it] Proxy between game client and server



Macok
22nd February 2009, 13:15
@EDIT
SOLVED, please delete it.
The problem was that I didn't tell server->write() how many bytes should it send.

----------------------------------



I'd like to write simple proxy between online game server and client.
Game client should connect to my program, and my program should connect to server.

Client <----> Proxy <-----> Server

Everytime when program receives data from server, it must send it to client, and everytime when it receives data from client it must send it to server.
It doesn't seem to be very hard.
I tried by this way:
Notice: There are 2 very similar variables: serv and server
QTcpServer *serv;
QTcpSocket *client;
QTcpSocket *server;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindowClass)
{
serv=new QTcpServer;
serv->listen(QHostAddress::Any, 1234);
connect(serv, SIGNAL(newConnection()), this, SLOT(newConn()));
}So when game client connect to my proxy, this function should be called:
void MainWindow::newConn(){
cout<<"Hello!"<<endl;
client=serv->nextPendingConnection(); //connection with game client
server=new QTcpSocket;
server->connectToHost("11.111.111.11", 1234);
if(!server->isOpen())
cout<<"Connection failed"<<endl;

connect(client, SIGNAL(readyRead()), this, SLOT(clientToServer()));
connect(server, SIGNAL(readyRead()), this, SLOT(serverToClient()));
}
void MainWindow::clientToServer(){
cout<<"Received "<<client->bytesAvailable()<<"bytes from client"<<endl;
cout<<"Sending to server..."<<endl;
char *data=new char[client->bytesAvailable()];
client->read(data, client->bytesAvailable());
server->write(data);
delete[] data;
}
void MainWindow::serverToClient(){
cout<<"Received "<<server->bytesAvailable()<<"bytes from server"<<endl;
cout<<"Sending to client..."<<endl;
char *data=new char[server->bytesAvailable()];
server->read(data, server->bytesAvailable());
client->write(data);
delete[] data;
}
I think it should work fine, but it doesnt:
Hello!
Received 139 bytes from client
Sending to server...and nothing more happens.
It seems program received some data from client and sent it to the server, but server is not responding.
Whats wrong?
Maybe there's simpler way to write such a proxy?

Thanks a lot for any help!