PDA

View Full Version : Qtcpsocket connection Problem while connecting with j2me midlet.



hasnain
15th November 2010, 11:00
Hi,
i am using QTcpServer and QTcpSocket for listening and establishing connection. QTcpServer accept connection and open a socket and, J2me server accept connection from my Qt app. but after that when i tried to transfer data. any incoming data is not detected on both sides any body has an idea how i can do this. both Qt and j2me app are reading/writing in UTF8 format.

newConn() is connected to the QTcpserver::newConnection signal


@void chatScreen::newConn(){
char *data = new char();
qDebug()<<"New Connection";
// qDebug()<<"threads "<<threads.count()<<" Textbrowsers "<<textBrowsers.count()<<"Sockets "<<connections.count();
uint i;
i=1024;
// QString name;
QTcpSocket *client =server.nextPendingConnection();
QByteArray block;
QDataStream out(&block,QIODevice::WriteOnly);
QString a=tr("(iq,") + ownUserName + tr(")");
// out<<a.toAscii();
client->write(a.toUtf8());
client->flush();
client->waitForReadyRead();
client->readLine(data,i);
qDebug()<<data;
}@


connectToServer() is using to connect to server.

@void chatScreen::connectToServer(QString itemName){
// char *data = new char();
char *data = new char();
uint i;
i=1024;
uint port;
port = 1235;
QString ip;
for (int k=0;k < contactList.count();k++){
if(contactList.at(k)->getName()== itemName){
ip = contactList.at(k)->getIpAddress();
break;
}
}

QTcpSocket *client = new QTcpSocket(this);
client->connectToHost((QHostAddress)ip,port,QIODevice::Rea dWrite);
client->waitForConnected();
if(client->state()== QAbstractSocket::ConnectedState){
client->waitForReadyRead();
client->readLine(data,i);
qDebug()<<data;
QString datain = data;
if(datain.startsWith(tr("(iq,"))){
QString a = tr("(hs)(") + QString::number(ownUserId) + tr(")(" )+ ownUserName + tr(")(") + ownPhoneNumber + tr(")(") +
ownAlias+ tr(")(") + ownIpAddress + tr(")");
client->write(a.toUtf8());
client->flush();
}@

the above code is working fine when used to communicate between two Qt apps.
--

tbscope
15th November 2010, 11:36
char *data = new char();
Wrong!


qDebug()<<"threads "<<threads.count()<<
This doesn't predict a bright future.


QDataStream out(&block,QIODevice::WriteOnly);
When connecting to a non Qt based server or a Qt server that doesn't use QDataStream, this does not work. A DataStream is a binary stream only readable by a program that can actually decode it.

But you don't use it.


QString a=tr("(iq,") + ownUserName + tr(")");
Wrong, use QString::arg and similar functions.


(QHostAddress)ip
Wrong! Undefined behaviour.
You can not cast a QString to a QHostAddress.