PDA

View Full Version : Sending string from QT client to Java server



seanmu13
28th June 2007, 15:24
I am trying to send a string from my QT client to a Java server. I am able to get them connected, verified both with the QT and Java programs. However, I am not able to send a string over the socket, nothing gets passed to the server, can anybody help?

wysota
28th June 2007, 21:31
You probably face encoding problems, but providing more details would help pinpoint the problem.

seanmu13
29th June 2007, 14:39
Using a QTcpSocket I am able to successfully connect to localhost indicated by the socket state, I initialize a QTextSteam with the QIODevice being the socket, then I simply have:
out << "someTextToSendToServer"; And when the Java server indicates that something was sent to it, it is just null. It's not a problem with the server because someone else was able to successfully send a string to the server with another program, but I am unable to contact them.

seanmu13
29th June 2007, 17:45
Do I have to use the write method in QIODevice?

wysota
1st July 2007, 21:32
Was "another program" written in Java as well?

seanmu13
2nd July 2007, 13:44
No it was written in C# I believe, so it didn't help much.

wysota
2nd July 2007, 22:26
Can we see some code of the Qt based client?

seanmu13
2nd July 2007, 22:29
int port = 50000;
QTcpSocket socket(this);
connect(&socket,SIGNAL(connected()),this,SLOT(isConnected() ));
socket.connectToHost(QHostAddress::LocalHost,port, QIODevice::ReadWrite);
socket.waitForConnected(1000);
//socket.write((const char *)"sdfsdf",5);
QTextStream out(&socket);
out << "sample text" << endl;
socket.disconnectFromHost();

jacek
2nd July 2007, 22:35
QTextStream out(&socket);
out << "sample text" << endl;
socket.disconnectFromHost();
How about calling waitForBytesWritten() before disconnecting and waitForDisconnected() after? If socket goes out of scope after a call to disconnectFromHost() it won't have any chance to send that data.

seanmu13
3rd July 2007, 13:29
I'll try that and see how it goes. Thank you.

seanmu13
3rd July 2007, 13:52
Thank you so much Jacek! I've been working on that stuff for about a week now with no luck. Thanks again.