Results 1 to 4 of 4

Thread: Qtcpserver problem

  1. #1
    Join Date
    Aug 2006
    Location
    Germany
    Posts
    33
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Qtcpserver problem

    Hello Everyone!

    I have a basic question.

    In fortune server client example, its the client request the data from the server.

    So the server receives newconnection() slot and then sends the data.

    How to send data to client from server, without any signals from client.

    Hope I made my question clearer?

    looking forward to your reply

    thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qtcpserver problem

    If you have a connection with a client, you just write to the socket and client should get the data. If you don't have a connection, you have to turn your client into a server and the server into a client.

  3. #3
    Join Date
    Aug 2006
    Location
    Germany
    Posts
    33
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qtcpserver problem

    thanks jacek for your reply.

    Actually, my application runs in threads. Client as well as Server. First of all, sorry for posting freakingly long. I want to complete it by myself but i am stumped.


    Client side inside thread
    Qt Code:
    1. void CEthernet::DoWork() // run()
    2. {
    3. Lock();
    4.  
    5. if(m_pSocket->waitForReadyRead()== true)
    6. {
    7. QByteArray m_aServerPaket, m_aServerData ;
    8. m_aServerPaket = m_pSocket->readAll();
    To copy to clipboard, switch view to plain text mode 

    Server side- Dialog class, server class and Thread class

    In server class overwritting the incoming connection and sending the socketDescriptor to Dialog class

    Qt Code:
    1. void EthernetServer::incomingConnection(int socketDescriptor)
    2. {
    3. m_socketDescriptor = socketDescriptor;
    4. SerialProtocolTestWidget m_Widget;
    5. m_Widget.onNewEthernetConnection(socketDescriptor);
    6. }
    To copy to clipboard, switch view to plain text mode 

    In Dialog class

    only initalizing the new thread instance. communication should be done only after button press in the dialog.
    Qt Code:
    1. void SerialProtocolTestWidget::onNewEthernetConnection(int socketDescriptor)
    2. {
    3. m_EthernetThread1 = new EthernetThread(socketDescriptor,this);
    4. }
    To copy to clipboard, switch view to plain text mode 

    Data transfer should only be done after button press in the dialog.

    Qt Code:
    1. void SerialProtocolTestWidget::on_butProgrammwahlEthernet_clicked()
    2. {
    3. m_EthernetThread1->SetData(eProgramChoice, totalSize, id, pData, len,true);
    4.  
    5. m_EthernetThread1->start();
    6. }
    To copy to clipboard, switch view to plain text mode 


    In Thread class

    Qt Code:
    1. EthernetThread::EthernetThread(int socketDescriptor, QObject *parent)
    2. : QThread(parent), socketDescriptor(socketDescriptor), m_bRun1(false)
    3. {
    4. if (!tcpSocket.setSocketDescriptor(socketDescriptor,QAbstractSocket::ConnectedState, QIODevice::WriteOnly))
    5. {
    6. emit error(tcpSocket.error());
    7. return;
    8. }
    9. }
    10.  
    11. EthernetThread::~EthernetThread(){}
    12.  
    13. void EthernetThread::run()
    14. {
    15. int WrittenData = tcpSocket.write((char*)pBuff,len);
    16. }
    17. void EthernetThread::SetData(ECommand command, UINT32 payloadSize, UINT32 id, void *pData, UINT32 &packageLength, bool m_run)
    18. {
    19. //m_bRun1 = m_run;
    20. pBuff = MakeDataPackage(command,payloadSize,id,pData,packageLength);
    21. len = packageLength;
    22. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for looking upto this.
    Your suggestion will be highly appreciated.

    P.S: (reduced info)
    Dialog starts the server and it has many buttons which sends respective data on click to the connected client. Both server and client runs in different threads.
    Last edited by kingslee; 2nd September 2008 at 11:53.

  4. #4
    Join Date
    Aug 2006
    Location
    Germany
    Posts
    33
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qtcpserver problem

    Hello jacek and Everyone,
    I found the solution.
    I removed the threads and used signals/slots and its working .
    thanks again

Similar Threads

  1. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  2. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47
  3. QTcpServer problem in Python Qt
    By mejustme in forum Qt Programming
    Replies: 2
    Last Post: 26th June 2008, 14:49
  4. Replies: 4
    Last Post: 23rd May 2008, 09:42
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.