Results 1 to 2 of 2

Thread: Qtcpsocket connection Problem while connecting with j2me midlet.

  1. #1
    Join Date
    Sep 2010
    Posts
    12
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Question 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
    Qt Code:
    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. }@
    To copy to clipboard, switch view to plain text mode 


    connectToServer() is using to connect to server.
    Qt Code:
    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. }@
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Qtcpsocket connection Problem while connecting with j2me midlet.

    Qt Code:
    1. char *data = new char();
    To copy to clipboard, switch view to plain text mode 
    Wrong!

    Qt Code:
    1. qDebug()<<"threads "<<threads.count()<<
    To copy to clipboard, switch view to plain text mode 
    This doesn't predict a bright future.

    Qt Code:
    1. QDataStream out(&block,QIODevice::WriteOnly);
    To copy to clipboard, switch view to plain text mode 
    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.

    Qt Code:
    1. QString a=tr("(iq,") + ownUserName + tr(")");
    To copy to clipboard, switch view to plain text mode 
    Wrong, use QString::arg and similar functions.

    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    Wrong! Undefined behaviour.
    You can not cast a QString to a QHostAddress.

Similar Threads

  1. Replies: 1
    Last Post: 23rd October 2009, 18:23
  2. Problem connecting selectionChanged
    By jano_alex_es in forum Newbie
    Replies: 2
    Last Post: 1st June 2009, 15:57
  3. Replies: 1
    Last Post: 1st November 2007, 14:09
  4. QTcpsocket - connection problem
    By vishesh in forum Qt Programming
    Replies: 1
    Last Post: 16th October 2007, 17:03
  5. QTCPSocket not connecting
    By DPinLV in forum Qt Programming
    Replies: 6
    Last Post: 25th August 2006, 07:18

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.