Client/server application
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
Code:
{
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
Re: Client/server application
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.
Re: Client/server application
yes I know how send/recieve data, but I dont know how find right socket.
server.cpp
Code:
void MainWindow::startServer()
{
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
());
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
{
socket->write(data);
socket->flush();
}
Re: Client/server application
So you need to store the socket pointer when you accept the connection. Where you store it is up to you.
Re: Client/server application
yes this is it, but i dont khow how to do. How to store socket pointer? I didnt find any method.
Re: Client/server application
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;