Results 1 to 1 of 1

Thread: [Please delete it] Proxy between game client and server

  1. #1
    Join Date
    Jan 2009
    Posts
    51
    Thanks
    28
    Qt products
    Qt4
    Platforms
    Unix/X11

    Arrow [Please delete it] Proxy between game client and server

    @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
    Qt Code:
    1. QTcpServer *serv;
    2. QTcpSocket *client;
    3. QTcpSocket *server;
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent), ui(new Ui::MainWindowClass)
    3. {
    4. serv=new QTcpServer;
    5. serv->listen(QHostAddress::Any, 1234);
    6. connect(serv, SIGNAL(newConnection()), this, SLOT(newConn()));
    7. }
    To copy to clipboard, switch view to plain text mode 
    So when game client connect to my proxy, this function should be called:
    Qt Code:
    1. void MainWindow::newConn(){
    2. cout<<"Hello!"<<endl;
    3. client=serv->nextPendingConnection(); //connection with game client
    4. server=new QTcpSocket;
    5. server->connectToHost("11.111.111.11", 1234);
    6. if(!server->isOpen())
    7. cout<<"Connection failed"<<endl;
    8.  
    9. connect(client, SIGNAL(readyRead()), this, SLOT(clientToServer()));
    10. connect(server, SIGNAL(readyRead()), this, SLOT(serverToClient()));
    11. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void MainWindow::clientToServer(){
    2. cout<<"Received "<<client->bytesAvailable()<<"bytes from client"<<endl;
    3. cout<<"Sending to server..."<<endl;
    4. char *data=new char[client->bytesAvailable()];
    5. client->read(data, client->bytesAvailable());
    6. server->write(data);
    7. delete[] data;
    8. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void MainWindow::serverToClient(){
    2. cout<<"Received "<<server->bytesAvailable()<<"bytes from server"<<endl;
    3. cout<<"Sending to client..."<<endl;
    4. char *data=new char[server->bytesAvailable()];
    5. server->read(data, server->bytesAvailable());
    6. client->write(data);
    7. delete[] data;
    8. }
    To copy to clipboard, switch view to plain text mode 
    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!
    Last edited by Macok; 22nd February 2009 at 14:34.

Similar Threads

  1. Very strange socket programming problem
    By montylee in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2008, 13:05
  2. synching client readings to server output
    By OnionRingOfDoom in forum Qt Programming
    Replies: 14
    Last Post: 28th January 2006, 19:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.