PDA

View Full Version : Client/server application



sali555
21st April 2011, 18:35
hi everyone,

i am writing a network application and have one problem. I have server and two clients. The first client send message to server, when this message comes to server, the server send other message to the second client but I dont know how can I do that.

I have this function, which send data to client

void MainWindow::writeData(QTcpSocket* socket, QByteArray data)
{
socket->write(data);
socket->flush();
}

The first client send message START, the second send "FINISH"
When message FINISH comes, I want to send message HELLO to the client, which sended message START.

sorry about my english

squidge
21st April 2011, 18:49
So you know how to send data.

Do you know how to receive data? If so, what is the problem? Post your code and point to the part which isn't working.

sali555
21st April 2011, 20:47
yes I know how send/recieve data, but I dont know how find right socket.

server.cpp


void MainWindow::startServer()
{
server = new QTcpServer(this);
QObject::connect(server, SIGNAL(newConnection()),this, SLOT(acceptConnection()));
server->listen();
}

void MainWindow::acceptConnection()
{
QTcpSocket* socket = server->nextPendingConnection();
connect(socket, SIGNAL(readyRead()), SLOT(startRead()));
}

// IN THIS FUNCTION I HAVE PROBLEM!!!!
void MainWindow::startRead()
{
QTcpSocket* socket = qobject_cast<QTcpSocket*>(this->sender());
QByteArray rawdata = socket->readAll();

if(rawdata == "FINISHREADY")
{
// PROBLEM
// the first parameter in this function must be a socket from which comes message
// STARTREADY
// and I dont know how find right socket

zapsat(start, "READY");
}
}

//write data to socket
void MainWindow::writeData(QTcpSocket* socket, QByteArray data)
{
socket->write(data);
socket->flush();
}

squidge
21st April 2011, 23:42
So you need to store the socket pointer when you accept the connection. Where you store it is up to you.

sali555
22nd April 2011, 08:23
yes this is it, but i dont khow how to do. How to store socket pointer? I didnt find any method.

squidge
22nd April 2011, 09:55
Your already storing it in local scope (QTcpSocket* socket), what you need to do is store it in a more global scope, such as in your class. Just assign, eg. clientSock = socket;