Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 43

Thread: incommingConnection not called by QTcpServer

  1. #21
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: incommingConnection not called by QTcpServer

    Quote Originally Posted by toufic.dbouk View Post
    Qt Code:
    1. void myClient::readyRead()
    2. {
    3. QString fPath("C:\\Users\\Ali\\Desktop\\music");
    4. QByteArray block;
    5.  
    6. block=socket->readAll();
    7. socket->flush();
    8. qDebug()<< "block: " << block;
    9. socket->waitForReadyRead();
    10.  
    11. QString fName= QString(block);
    12. QFile sentFile(fPath +QDir::separator() + fName);
    13. block.clear();
    14.  
    15. if(!sentFile.exists())
    16. {
    17. sentFile.open(QFile::WriteOnly);
    18. while(socket->waitForReadyRead())
    19. {
    20. QByteArray block= socket->readAll();
    21. qDebug()<< "read: " << block.size();
    22. sentFile.write(block);
    23. block.clear();
    24. }
    25. sentFile.close();
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 

    thats how iam saving the data.
    this readyRead slot is connected to the sockets readyRead Signal
    You didn't get my question. I asked you specifically -- how many times is readyRead() called for your file? Remove all content of the method and only put a debug statement that prints something and a call to socket->readAll(). Then run the program and see how many times the method is called for your file. Then sit down and think why it happens and how to change your code.

    i replied to anda_skoa but he havnt replied back , i need him to reply to my question first to understand whats going on.
    for now i connected the newConnection to another slot that will handle the work.
    I can tell you what is going on. There is a class A that has a virtual method x() and class B that doesn't have that method and you are implementing B::x() hoping that whatever was calling A::x() will call B::x() now. This is not the case in C++. You need to subclass A and reimplement x() for it to be called.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  2. #22
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: incommingConnection not called by QTcpServer

    Quote Originally Posted by wysota View Post
    You didn't get my question. I asked you specifically -- how many times is readyRead() called for your file? Remove all content of the method and only put a debug statement that prints something and a call to socket->readAll(). Then run the program and see how many times the method is called for your file. Then sit down and think why it happens and how to change your code.

    hello Wysota,
    sorry for the late reply i got busy last day
    anyway i tried what u told me , the readyRead is emiited lots of time depending on the file size for sure
    i was able to get rid of that waitforReadyReady loop
    data is fully written on server
    BUT STILL THE WHOLE GUI FREEZES!!
    it freezes as long as the file is being written
    so actually this didnt solve my problem...
    heres the code:
    Qt Code:
    1. void myClient::readyRead()
    2. {
    3. QString fPath("path");
    4. QByteArray block;
    5. QString fName= "test1";
    6. QFile sentFile(fPath +QDir::separator() + fName);
    7. sentFile.open(QFile::WriteOnly | QFile::Append);
    8. block=socket->readAll();
    9. socket->flush();
    10. qDebug()<< "block: " << block.size();
    11. sentFile.write(block);
    12. }
    To copy to clipboard, switch view to plain text mode 

    so do i use threads for this to not freeze ? or dd i do a mistake or missed something ?
    please let me know ASAP
    your help is really appreciated
    thanks.

  3. #23
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: incommingConnection not called by QTcpServer

    1. open the file once instead of reopening every time the method is called.
    2. don't flush the socket.
    3. create a QTime object at the beginning of the method, call its start() method and prints the result of its elapsed() method at the end of the readyRead() function. See how much time (in miliseconds) it takes to execute the method. It shouldn't take more than 100ms unless you are using a really low-end machine or you have problems elsewhere in your design which causes unnecessary lags and buildup in pending socket data.
    4. also see how large blocks of data you receive, if you operate on the boundary of available memory, you might be getting into slowdowns caused by swapping, try to eliminate those (e.g. by not reading all data at once but instead reading it in smaller blocks that are guaranteed to fit in ram).

    See if it helps.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #24
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: incommingConnection not called by QTcpServer

    Quote Originally Posted by wysota View Post
    1. open the file once instead of reopening every time the method is called.
    2. don't flush the socket.
    do you mean that i should open the file outside the readyRead slot ?
    may i ask why flushing the socket would result in such a slow or freeze of GUI ?

    ill try all of what you said and let you know the result

  5. #25
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: incommingConnection not called by QTcpServer

    Quote Originally Posted by toufic.dbouk View Post
    and thats exactly what i want , i want the QTcpServer::incommingConnection to be called when a connection is established after which ill handle it the way i want.
    As I said you need to create a subclass of QTcpServer for that and then reimplement/override the virtual method incomingConnection()

    Quote Originally Posted by toufic.dbouk View Post
    well server in my class is an instance of QTcpServer class
    The class "server" is a QMainWindow, not a QTcpServer

    Quote Originally Posted by toufic.dbouk View Post
    for sure it will signal the QTcpServer::incommingConnection when a connection is accepted even if i subclassed from QMainWindow, i still have that QTcpServer object with its incommingConnection
    Yes, QTcpServer works totally independent of where you put it, whether a main window or somewhere else.

    Quote Originally Posted by toufic.dbouk View Post
    so i still cant see the problem , why isnt it being invoked ?
    QTcpServer::incomingConnection() is invoked.

    If you want your method to be invoked instead, you have to derive from QTcpServer and reimplement/override it.

    Your original code looks like you were assuming that the method of a totally different object (server::incomingConnection) would somehow magically be invoked.
    C++ might look like magic from time to time but I am afraid it is not

    Cheers,
    _

  6. #26
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: incommingConnection not called by QTcpServer

    Quote Originally Posted by anda_skoa View Post
    The class "server" is a QMainWindow, not a QTcpServer
    what i meant here is that i have a QTcpServer instance(mServer) in my server class
    so i was expecting the incommingConnection to be invoked from it

    Your original code looks like you were assuming that the method of a totally different object (server::incomingConnection) would somehow magically be invoked.
    C++ might look like magic from time to time but I am afraid it is not
    its nice to have some sense of comedy from time to time, it removes the feeling of student-instructor at such forms:P
    thanks ill give what u said a try and reply back later.

  7. #27
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: incommingConnection not called by QTcpServer

    Quote Originally Posted by toufic.dbouk View Post
    do you mean that i should open the file outside the readyRead slot ?
    Yes, that's correct. Opening and closing the file takes some time, why waste it?
    may i ask why flushing the socket would result in such a slow or freeze of GUI ?
    I didn't say it does (maybe it does, maybe not). What flush does is that it forces all data pending to be written to be forced into the socket (AFAIR it sets the P(U)SH flag in TCP). Since you're not writing anything to the socket there is no point in flushing it, it's a pure waste of time. Even if you were writing something to the socket, flushing it in the part of code related to reading from the socket wouldn't make much sense too.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #28
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: incommingConnection not called by QTcpServer

    Quote Originally Posted by wysota View Post
    I didn't say it does (maybe it does, maybe not). What flush does is that it forces all data pending to be written to be forced into the socket (AFAIR it sets the P(U)SH flag in TCP). Since you're not writing anything to the socket there is no point in flushing it, it's a pure waste of time. Even if you were writing something to the socket, flushing it in the part of code related to reading from the socket wouldn't make much sense too.
    sure this makes sense thanks.
    but how can i open the file before the readyRead slot is invoked even if the file isnt there ?
    in other other words , im getting the name of the file from the readyRead slot and creating that file too when the readyRead Slot is executed ?
    so how can i open the file before executing the readyRead slot ?

  9. #29
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: incommingConnection not called by QTcpServer

    Quote Originally Posted by toufic.dbouk View Post
    sure this makes sense thanks.
    but how can i open the file before the readyRead slot is invoked even if the file isnt there ?
    in other other words , im getting the name of the file from the readyRead slot and creating that file too when the readyRead Slot is executed ?
    so how can i open the file before executing the readyRead slot ?
    Open it when you receive the file name from the socket and then keep it open (you can create QFile on the heap or make it a member variable of your class). Note that you may receive the file name in parts, you have to take that into account when implementing the slot that handles incoming data.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #30
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: incommingConnection not called by QTcpServer

    alright things are getting better i guess
    but i need you to tell me something
    if i have a block of code in a method (Readyread Slot ) which is entered everytime the readyRead Signal is emitted
    in this method i want part of the code to not be executed but for the first time it enters
    is there any easy way to lock such part of code so that it is not executed on the 2nd , third , fourth etc executions of the method?
    i havnt slept for a while so honestly i dont know how to do it
    may you help in this please?

  11. #31
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: incommingConnection not called by QTcpServer

    Quote Originally Posted by toufic.dbouk View Post
    if i have a block of code in a method (Readyread Slot ) which is entered everytime the readyRead Signal is emitted
    in this method i want part of the code to not be executed but for the first time it enters
    is there any easy way to lock such part of code so that it is not executed on the 2nd , third , fourth etc executions of the method?
    i havnt slept for a while so honestly i dont know how to do it
    may you help in this please?
    Have a bool member variable, initialize it to something and then check its value in the method and flip it to the other value. Or simply have a QFile member variable and each time you enter the method, check if the file is open or not.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #32
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: incommingConnection not called by QTcpServer

    i guess i cant do that cuz :

    void myFucntion()
    {

    bool lock=false;

    while(lock==false)
    {
    /* code that i want to block after first execution*/

    bool = true;

    }

    // rest of the code




    }

    this will result in re-initialization off variable bool to false everytime the method is executed which will result in the
    execution of my code

    if u suggest to define in it header tell me how cuz i tried several times and each resulted in a different error.
    thanks

  13. #33
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: incommingConnection not called by QTcpServer

    I said a (class) member variable, not a local variable.

    Qt Code:
    1. class myClient : ... {
    2. ...
    3. myClient(...) : ... {
    4. ...
    5. isInitialized = false;
    6. }
    7. private:
    8. bool isInitialized;
    9. };
    10.  
    11. void myClient::readyRead() {
    12. if(!isInitialized) {
    13. isInitialized = true;
    14. // executed once
    15. }
    16. // executed every time
    17. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. The following user says thank you to wysota for this useful post:

    toufic.dbouk (8th January 2013)

  15. #34
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: incommingConnection not called by QTcpServer

    error: ISO C++ forbids initialization of member 'lock'
    error: making 'lock' static
    error: ISO C++ forbids in-class initialization of non-const static member 'lock'

    these are the errors when i declare lock in my header as : bool lock=false;

    if i make it static and const i wont be able to change it

    if i make it just static : error: ISO C++ forbids in-class initialization of non-const static member 'lock'

    thats what i meant by i tried several times each with an error

  16. #35
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: incommingConnection not called by QTcpServer

    Look at the code I posted and compare it with yours.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. #36
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: incommingConnection not called by QTcpServer

    Quote Originally Posted by wysota View Post
    Look at the code I posted and compare it with yours.
    yea sry i wrote the reply before the code appear

    it is working , the readyRead SLot is almost good
    its not blocking that much i guess the slight freeze might be from my laptop even though its a corei7 toshiba with 8GB of rams
    but i havnt turned it off for a quite long time :P

    ok so lets continue
    do u have any easy idea to do authentication ?
    what i mean is that when my client connects he will have to enter a user name and pass and email
    i want the server to do authentication on this after he signs up
    so the next time he logs in the server can authenticate
    if the user name and pass are correct he will have access else he wont
    can it be done without saving the data to a file on the server ?
    should i encrypt the password ?

  18. #37
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: incommingConnection not called by QTcpServer

    This is really out of scope of this forum. Although I have some knowledge regarding the topics you are asking about, I suggest you read some articles on designing protocols and services. I can only suggest that you follow standards instead of reinventing the wheel. E.g. think about using HTTP as your application protocol, maybe it suits your needs.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. #38
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: incommingConnection not called by QTcpServer

    ahhh...

    may you give me your email so that you help me on this cuz its out of scop of this form? if your are willing to help me for sure

    i thought you would know how to use the QAuthenticator and whether it can suit my need or not
    i couldnt fine much tutorials about it but for the qt documentations

    one more thing
    i forgot to ask when should i close the file ?
    i cant close it at that same readyRead slot , cuz then i would have done nothin
    so whats the solution ?
    i really appreciate your help.
    thanks.

  20. #39
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: incommingConnection not called by QTcpServer

    I will not design a protocol for you. The problem is not about Qt, it is about your understanding of how networking works. I cannot teach you that via email. This is not a task for 10 minutes, you have to do some reading and thinking. Choosing the right classes for the job is the last step that you need to do. First you need to go through a bunch of others.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  21. #40
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: incommingConnection not called by QTcpServer

    i ddnt ask to design a protocol for me
    i simply asked for help via email thats all
    i know networking, i took networking courses and do understand what you say about tcp and http and all that networking stuff
    dont go to NAT cuz i havnt took that yet :P
    im obliged to use what im using here and i have to use a authentication way and encrypting password
    anyway if thats out of scope its fine.
    but u ddnt answer me about the QAuthenticator ?

Similar Threads

  1. QTcpServer
    By DmitryNik in forum Newbie
    Replies: 0
    Last Post: 25th October 2011, 12:36
  2. QTcpServer
    By DmitryNik in forum Newbie
    Replies: 2
    Last Post: 1st October 2011, 08:07
  3. QTcpServer Problem
    By ionutdanila in forum Newbie
    Replies: 2
    Last Post: 27th December 2010, 12:18
  4. QTcpServer and GDB
    By baray98 in forum Qt Programming
    Replies: 2
    Last Post: 21st January 2009, 08:02
  5. Replies: 1
    Last Post: 18th June 2006, 10:12

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.