November 15, 2010

hasnain hasnain
Lab Rat
11 posts

Qtcpsocket connection Problem while connecting with j2me midlet.

 

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

  1. void chatScreen::newConn(){
  2.     char *data = new char();
  3.     qDebug()<<"New Connection";
  4. //    qDebug()<<"threads "<<threads.count()<<" Textbrowsers "<<textBrowsers.count()<<"Sockets "<<connections.count();
  5.     uint i;
  6.     i=1024;
  7. //    QString name;
  8.     QTcpSocket *client =server.nextPendingConnection();
  9.     QByteArray block;
  10.     QDataStream out(&block,QIODevice::WriteOnly);
  11.     QString a=tr("(iq,") + ownUserName + tr(")");
  12. //    out<<a.toAscii();
  13.     client->write(a.toUtf8());
  14.     client->flush();
  15.     client->waitForReadyRead();
  16.     client->readLine(data,i);
  17.     qDebug()<<data;
  18. }

connectToServer() is using to connect to server.

  1. void chatScreen::connectToServer(QString itemName){
  2. //    char *data = new char();
  3.     char *data = new char();
  4.     uint i;
  5.     i=1024;
  6.     uint port;
  7.     port = 1235;
  8.     QString ip;
  9.     for (int k=0;k < contactList.count();k++){
  10.         if(contactList.at(k)->getName()== itemName){
  11.             ip = contactList.at(k)->getIpAddress();
  12.             break;
  13.         }
  14.     }
  15.  
  16.     QTcpSocket *client = new QTcpSocket(this);
  17.     client->connectToHost((QHostAddress)ip,port,QIODevice::ReadWrite);
  18.     client->waitForConnected();
  19.     if(client->state()== QAbstractSocket::ConnectedState){
  20.         client->waitForReadyRead();
  21.         client->readLine(data,i);
  22.         qDebug()<<data;
  23.         QString datain = data;
  24.         if(datain.startsWith(tr("(iq,"))){
  25.             QString a = tr("(hs)(") + QString::number(ownUserId) + tr(")(" )+ ownUserName + tr(")(") + ownPhoneNumber + tr(")(") +
  26.                         ownAlias+ tr(")(") + ownIpAddress + tr(")");
  27.             client->write(a.toUtf8());
  28.             client->flush();
  29. }

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

1 reply

November 15, 2010

Denis Kormalev Denis Kormalev
Lab Rat
1607 posts

Maybe something wrong at j2me part? Have you tried to look for packets via sniffer (like wireshark)?

 
  ‹‹ Qt confused      compiling qml with mobility ››

You must log in to post a reply. Not a member yet? Register here!