PDA

View Full Version : server-client communication



sksingh73
18th June 2010, 20:20
- i am new to QT4. Actually i am trying to make a server-client program in which when i send a message from client, it will be displayed in textedit box in server. Once server receives this message, it sends back an acknowledgement sort of message back to client. At client end this message will be displayed in the textedit box.
- now the problem part. i have been able to send message from client to sever & its being displayed in textedit box at server end. my problem is that when server sends back acknowledgement to client its not being displayed in the textedit box at client end. my code is ...

client code:
void client::sendRequest()
{
QString msg = msglineEdit->text();
tcpSocket->write(QString(msg + "\n").toUtf8());
qDebug() << "Sending to server: " << msg;
char buffer[1024] = {0};
tcpSocket->read(buffer, tcpSocket->bytesAvailable());
qDebug() << "Reading from server: " << buffer;
responsetextEdit->append( buffer);
}

server code:
void server::startRead()
{
char buffer[1024] = {0};
client->read(buffer, client->bytesAvailable());
qDebug() << "Reading from client: " << buffer;
recdtextEdit->append( buffer);
QString msg = "Received";
client->write(QString(msg + "\n").toUtf8());
qDebug() << "Writing to client: " << msg;
client->close();
}

- i dont whats wrong in my code. please help.thanx

squidge
18th June 2010, 20:52
1) How do you know when data is available to read? Do you use slot? Is it configured correctly?
2) Theres no guarantee that the amount of bytes read will be the same as the amount of bytes written

cutie.monkey
22nd June 2010, 03:15
Please see this examples:

http://doc.trolltech.com/4.6/network-fortuneclient.html
http://doc.trolltech.com/4.6/network-fortuneserver.html