Results 1 to 3 of 3

Thread: Help in Qt Socket Programing in Windows 7?

  1. #1
    Join Date
    Dec 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question Help in Qt Socket Programing in Windows 7?

    hi
    im unable to communication data in my application server and client, i am use windows 7,when i use windows XP is work well, can anyone help me please

    my code like this
    in application Server :
    main:
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include "server.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication a(argc, argv);
    7. MyServer server;
    8. server.listen(QHostAddress::Any, 4711);
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 


    server.cpp
    Qt Code:
    1. #include <QtCore>
    2. #include <QtNetwork>
    3. #include "server.h"
    4. #include "MyServerHandlerMethod.h"
    5.  
    6.  
    7. MyServer::MyServer(QObject *parent):QTcpServer (parent)
    8. {
    9. connect(this,SIGNAL(newConnection()),this,SLOT(MyServerHandler()));
    10. }
    11.  
    12.  
    13. void MyServer::MyServerHandler()
    14. {
    15. QTcpSocket *socket = nextPendingConnection();
    16. if(!socket) return;
    17. new MyServerHandlerMethod(socket);
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 


    MyServerHandlerMethod.h
    Qt Code:
    1. #ifndef MYSERVERHANDLERMETHOD_H
    2. #define MYSERVERHANDLERMETHOD_H
    3. #include <QtCore/QString>
    4. #include <QtNetwork>
    5. #include <iostream>
    6.  
    7. using namespace std;
    8. class MyServerHandlerMethod : public QObject
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13.  
    14. MyServerHandlerMethod(QTcpSocket *socket, QObject *parent = 0) : QObject(parent)
    15. {
    16. connect(socket,SIGNAL(readyRead()),this,SLOT(HandlerConnection()));
    17. this->socket = socket;
    18. }
    19.  
    20. }
    21.  
    22.  
    23. signals:
    24.  
    25. private:
    26. QTcpSocket *socket;
    27. QString input;
    28.  
    29.  
    30. void ConnectClose()
    31. {
    32. socket->close();
    33. delete socket;
    34. deleteLater();
    35. }
    36.  
    37. private slots:
    38.  
    39. void HandlerConnection()
    40. {
    41. QByteArray data = socket->readAll();
    42. this->input = data; //QString input
    43.  
    44. cout<< qPrintable(this->input)<<endl;
    45. InputHandler();
    46.  
    47. ConnectClose();
    48.  
    49. }
    50.  
    51.  
    52. };
    53.  
    54. #endif // MYSERVERHANDLERMETHOD_H
    To copy to clipboard, switch view to plain text mode 


    in client:
    main
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include "client.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication a(argc, argv);
    7. client tryClient;
    8.  
    9. tryClient.send();
    10.  
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 


    client.h
    Qt Code:
    1. #ifndef CLIENT_H
    2. #define CLIENT_H
    3.  
    4. #include <QtNetwork/QTcpSocket>
    5.  
    6. class client : public QObject
    7. {
    8. Q_OBJECT
    9. public:
    10. void send()
    11. {
    12. const int Timeout = 5 * 1000;
    13. clientSocket = new QTcpSocket(this);
    14. clientSocket->connectToHost("localhost",4711);
    15. if (!clientSocket->waitForConnected(Timeout)) {
    16. return;
    17. }
    18.  
    19. clientSocket->write("ACK");
    20.  
    21. }
    22.  
    23. private:
    24. QTcpSocket *clientSocket;
    25. signals:
    26. private slots:
    27.  
    28.  
    29. };
    30. #endif // CLIENT_H
    To copy to clipboard, switch view to plain text mode 

    please help me
    Last edited by dedy2000; 5th December 2009 at 06:13.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Help in Qt Socket Programing in Windows 7?

    Have you checked that the Windows 7 firewall is letting connections in?

  3. #3
    Join Date
    Dec 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help in Qt Socket Programing in Windows 7?

    thank, it's work, i open the port in window 7 in the firewall menu, im add new rule in inbound menu, and its work, thanks for your repply

Similar Threads

  1. Replies: 1
    Last Post: 16th November 2009, 07:10
  2. Events lost in IPC through local socket in Windows
    By yogesh in forum Qt Programming
    Replies: 1
    Last Post: 24th May 2009, 11:38
  3. Qt Jambi, deploying app on Mac & Windows fails
    By ChrisColon in forum Installation and Deployment
    Replies: 2
    Last Post: 16th February 2009, 22:05
  4. Replies: 5
    Last Post: 15th January 2009, 09:03
  5. Qt and windows vista
    By munna in forum General Discussion
    Replies: 8
    Last Post: 11th January 2006, 22:33

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.