Results 1 to 20 of 58

Thread: Socket Prog

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Socket Prog

    Dear All,

    I have developed a SERVER_CLIENT Application, Where Server will receives data from client and present it on the Server Application. I have done with the 1-server and 1-client. If i want to increase the Client Nos. I'm not able to do it.. When same Client application in compiled second time as second client. The data at server end is not processed or to be clear. Data was not sent from Secone client only. How this can be addressed,
    1.either we need to create mulitple sockets.. how this can be done having one-Serverapplication.
    2. or Can we create a Server Application where it can accept mutilple "ports" (e.g server->listen(QHostAddress::Any,1234, server->listen(QHostAddress::Any,1235)) connections ? in the server application while checking for Newconnections.

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

    Default Re: Socket Prog

    One approach you could try is every time you get an incoming connection from the server you set a socket and the streams for that user, ( you can make a class user to handle all this ).
    So now when a client connects an instance of the class will be created specifically to that user handling his requests.
    You will still have one Server socket to listen and you create a client-socket for every user.
    That's a general approach. You can find several other ways to solve the issue.

    Good Luck.

  3. #3
    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: Socket Prog

    Quote Originally Posted by Vivek1982 View Post
    1.either we need to create mulitple sockets.. how this can be done having one-Serverapplication.
    This is done automatically. Each incoming connection gets its own QTcpSocket instance.

    Quote Originally Posted by Vivek1982 View Post
    2. or Can we create a Server Application where it can accept mutilple "ports" (e.g server->listen(QHostAddress::Any,1234, server->listen(QHostAddress::Any,1235)) connections ? in the server application while checking for Newconnections.
    No need for multiple listening ports. All connections to a QTcpServer result in new QTcpSocket objects being created, one for each incoming connection/client.

    Cheers,
    _

  4. #4
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Socket Prog

    Thanks for all your replies. I will give an attempt to create a multiple Sockets for adding new clients into my server application. I hope I may example(Qt inbuilt) for this to unsderstand, how coding/ creating multiple socket can be done.

  5. #5
    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: Socket Prog

    Just to be sure you understood this correctly: you don't need to create multiple sockets, they are created for you. This is what the QTcpServer ultimately does.

    Cheers,
    _

  6. #6
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Socket Prog

    Hi. I have tried upto my level how to create a Server to handle many clients, but my non of the debug msgs are working. Im presenting my simple Server client code.plz support me.

    Teamstatus.h
    Qt Code:
    1. class TeamStatus : public QMainWindow {
    2. Q_OBJECT
    3. public:
    4. TeamStatus(QWidget *parent = 0);
    5. ~TeamStatus();
    6. void listen();
    7. void socketdisc();
    8. protected:
    9. void changeEvent(QEvent *e);
    10. void incomingConnection(int socketfd);
    11.  
    12. private:
    13. Ui::TeamStatus *ui1;
    14. QTcpServer *server;
    15. QSet <QTcpSocket*> sockets;
    16. public slots:
    17. void read_socket();
    To copy to clipboard, switch view to plain text mode 

    Teamstatus.cpp
    Qt Code:
    1. TeamStatus::TeamStatus(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui1(new Ui::TeamStatus)
    4. {
    5. ui1->setupUi(this);
    6. server=new QTcpServer(this);
    7. TeamStatus::~TeamStatus()
    8. {
    9.  
    10. delete ui1;
    11. }
    12. void TeamStatus::listen()
    13.  
    14.  
    15. {
    16. QHostAddress hAddr;
    17. hAddr.setAddress("192.168.64.55");
    18.  
    19. server->listen(QHostAddress::Any,1234);
    20. qDebug("Listening to new connection");
    21. qDebug("listening for new socket")
    22. }
    23.  
    24. void TeamStatus::incomingConnection(int socketfd)
    25. {{
    26.  
    27. socket=server->nextPendingConnection();
    28. if((socket->state()==QTcpSocket::ConnectedState))
    29. {
    30. QTcpSocket *socket = new QTcpSocket;
    31. socket->setSocketDescriptor(socketfd);
    32.  
    33. qDebug("new connection1 established");
    34.  
    35. }
    36. {
    37. }
    38. qDebug("new connection1 established");
    39.  
    40. }
    41.  
    42. read_socket();
    43.  
    44.  
    45. }
    46.  
    47.  
    48.  
    49. void TeamStatus::read_socket()
    50. {
    51. QTcpSocket *socket=new QTcpSocket;
    52. socket=(QTcpSocket*) sender();
    53.  
    54. qDebug("after readingy);
    55. QByteArray buffer= socket->readLine();
    56. ... check for the buffer, do something
    57.  
    58. void TeamStatus::write_socket()
    59. {
    60. "How to write the data or send packet to single client or for all clients"
    61. }
    To copy to clipboard, switch view to plain text mode 


    Client.h
    Qt Code:
    1. class QTcpSocket;
    2.  
    3. namespace Ui {
    4. class TCPClient;
    5. }
    6.  
    7. class TCPClient : public QWidget {
    8. Q_OBJECT
    9. public:
    10. TCPClient(QWidget *parent = 0);
    11. ~TCPClient();
    12.  
    13. protected:
    14. void changeEvent(QEvent *e);
    15.  
    16. private:
    17. Ui::TCPClient *ui;
    18. QLineEdit *le,*le1;
    19. QTextEdit *te,*te1;
    20. QPushButton *pb_connect, *pb_disconnect;
    21. QPushButton *pb_send,*pb_send1;
    22. QTcpSocket *socket;
    23. QString CHAT;
    24.  
    25. private slots:
    26. void connecttoserver();
    27. void on_connected();
    28. void write();
    29. void write1();
    30. void DisconnectServer();
    31.  
    32. };
    33.  
    34. #endif // TCPCLIENT_H
    To copy to clipboard, switch view to plain text mode 



    Client.cpp
    Qt Code:
    1. TCPClient::TCPClient(QWidget *parent) :
    2. QWidget(parent),
    3. ui(new Ui::TCPClient)
    4. {
    5. ui->setupUi(this);
    6.  
    7. le= new QLineEdit;
    8. le=ui->lineEdit;
    9. le->setDisabled(true);
    10.  
    11. le1=new QLineEdit;
    12. le1=ui->lineEdit_2;
    13. le1->setDisabled(true);
    14.  
    15. te=new QTextEdit;
    16. te=ui->textEdit;
    17. te->setDisabled(true);
    18.  
    19. te1=new QTextEdit;
    20. te1=ui->textEdit_2;
    21. te1->setDisabled(true);
    22.  
    23. pb_connect=new QPushButton;
    24. pb_connect=ui->pushButton;
    25. pb_connect->setText("CONNECT");
    26. pb_connect->setEnabled(true);
    27.  
    28. pb_send=new QPushButton;
    29. pb_send=ui->pushButton_2;
    30. pb_send->setText("Send");
    31. pb_send->setEnabled(false);
    32.  
    33. pb_send1=new QPushButton;
    34. pb_send1=ui->pushButton_3;
    35. pb_send1->setText("SEND");
    36. pb_send1->setEnabled(false);
    37.  
    38. pb_disconnect=new QPushButton;
    39. pb_disconnect=ui->pushButton_4;
    40. pb_disconnect->setEnabled(false);
    41. pb_disconnect->setText("DISCONNECT");
    42.  
    43.  
    44.  
    45. socket=new QTcpSocket(this);
    46.  
    47. connect(pb_connect,SIGNAL(clicked()),this,SLOT(connecttoserver()));
    48. connect(pb_send,SIGNAL(clicked()),this,SLOT(write()));
    49. connect(pb_send1,SIGNAL(clicked()),this,SLOT(write1()));
    50. connect(pb_disconnect,SIGNAL(clicked()),this,SLOT(DisconnectServer()));
    51. connect(socket,SIGNAL(connected()),this,SLOT(on_connected()));
    52. connect(socket,SIGNAL(readyRead()),this,SLOT(read1()));
    53.  
    54. }
    55.  
    56. TCPClient::~TCPClient()
    57. {
    58. delete ui;
    59. }
    60.  
    61. void TCPClient::changeEvent(QEvent *e)
    62. {
    63. QWidget::changeEvent(e);
    64. switch (e->type()) {
    65. case QEvent::LanguageChange:
    66. ui->retranslateUi(this);
    67. break;
    68. default:
    69. break;
    70. }
    71. }
    72.  
    73. void TCPClient::connecttoserver()
    74. {
    75. QHostAddress hAddr;
    76. hAddr.setAddress("192.168.64.55");
    77. socket->connectToHost(hAddr,1234);
    78.  
    79. }
    80.  
    81. void TCPClient::on_connected()
    82. {
    83. pb_connect->setEnabled(false);
    84. pb_send->setEnabled(true);
    85. pb_send1->setEnabled(true);
    86. qDebug("Connection established");
    87. pb_disconnect->setEnabled(true);
    88. le->setEnabled(true);
    89. le1->setEnabled(true);
    90. te->setEnabled(true);
    91. te1->setEnabled(true);
    92. }
    93.  
    94. void TCPClient::write()
    95. {
    96. QString Data="",Data1=this->le->text();
    97. CHAT="1";
    98. Data.append(CHAT);
    99. Data.append(Data1);
    100. socket->write(Data.toUtf8().constData());
    101. qDebug("Client Sent 1 to Server");
    102.  
    103. }
    104. void TCPClient::write1()
    105. {
    106. QString Data="",Data1=this->le->text();
    107. CHAT="2";
    108. Data.append(CHAT);
    109. Data.append(Data1);
    110. socket->write(Data.toUtf8().constData());
    111. qDebug("Client Sent 2 to server");
    112.  
    113. // socket->flush();
    114.  
    115. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Socket Prog

    Where are you using your listen() method ?
    Check if isListening returns true.
    And check if the newConnection signal is fired when a client connects , to see if you are actually connecting to your server.
    Its not easy to read a code from iphone lol my post might be useless.


    Good Luck.

  8. #8
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Socket Prog

    Im using Listen() in one of the child window called Team Status. While compiling Im getting Debug msgs of Listen function. Im not gettin debug msg of New client or Connection accepted msg on the Application output window. earlier When i created one server and one client it works fine. when initialise for two or more clients in the server by
    Qt Code:
    1. void TeamStatus::incomingConnection(int socketfd) // Public slot in the teamstatus.H file
    2. {{
    3. QTcpSocket *socket = new QTcpSocket;
    4.  
    5. socket=server->nextPendingConnection();
    6. if((socket->state()==QTcpSocket::ConnectedState))
    7. {
    8. socket->setSocketDescriptor(socketfd);
    9.  
    10. qDebug("new connection1 established");
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

    client
    Qt Code:
    1. connect(pb_connect,SIGNAL(clicked()),this,SLOT(connecttoserver()));
    2. connect(socket,SIGNAL(connected()),this,SLOT(on_connected()));
    3. void TCPClient::connecttoserver()
    4. {
    5. QHostAddress hAddr;
    6. hAddr.setAddress("192.168.64.55");
    7. socket->connectToHost(hAddr,1234);
    8.  
    9. void TCPClient::on_connected(int descriptor) //public slot of client.h
    10.  
    11. {
    12. socket=new QTcpSocket(this);
    13. socket->setSocketDescriptor(descriptor);
    14.  
    15.  
    16. connect(pb_send,SIGNAL(clicked()),this,SLOT(write()));
    17. connect(pb_send1,SIGNAL(clicked()),this,SLOT(write1()));
    18. connect(pb_disconnect,SIGNAL(clicked()),this,SLOT(DisconnectServer()));
    19.  
    20. connect(socket,SIGNAL(readyRead()),this,SLOT(read1()));
    21. }
    22.  
    23. void TCPClient::write()
    24. {
    25. QString Data="",Data1=this->le->text();
    26. CHAT="1";
    27. Data.append(CHAT);
    28. Data.append(Data1);
    29. socket->write(Data.toUtf8().constData());
    30. qDebug("Client Sent 1 to Server");
    31.  
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 

    If can't later also u can look at the code then reply..
    Thanks

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

    Default Re: Socket Prog

    Is the newConnection signal getting fired ?
    Connect it to a slot where you accept the connection.
    Last edited by toufic.dbouk; 13th November 2013 at 09:18.

  10. #10
    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: Socket Prog

    Quote Originally Posted by toufic.dbouk View Post
    Connect it to a slot where you accept the connection.
    Exactly.

    You can't just create a QTcpServer object and expect that it magically knows how to invoke the function in which you call nextPendingConnection().

    Programming can look like magic but it isn't

    Cheers,
    _

  11. #11
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Socket Prog

    Btw. you have a memory leak here:
    Qt Code:
    1. QTcpSocket *socket = new QTcpSocket; // this object will never be deleted
    2. socket=server->nextPendingConnection();// because you overwrite the only pointer to it here
    To copy to clipboard, switch view to plain text mode 

  12. #12
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Socket Prog

    ya.. I have given a condition If already socket is accepted/connected. process the debug like new connection established
    Qt Code:
    1. socket=server->nextPendingConnection();
    2. if((socket->state()==QTcpSocket::ConnectedState))
    To copy to clipboard, switch view to plain text mode 
    I feel somewhere I'm going wrong when in creating a socket descriptor while assigning for connecting with many clients.
    or using QSet in teamstatus.h for creating more sockets.

  13. #13
    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: Socket Prog

    There is no need to create a socket descriptor, QTcpServer::nextPendingConnection() creates a fully constructed QTcpSocket object.

    Cheers,
    _

Similar Threads

  1. client/server prog using UDP
    By rajeshakula in forum Newbie
    Replies: 1
    Last Post: 12th July 2011, 11:43
  2. Build issues when qextsrialport.h is included in Linux prog
    By pupqt in forum Installation and Deployment
    Replies: 1
    Last Post: 7th April 2011, 15:48
  3. Replies: 2
    Last Post: 11th March 2010, 09:46
  4. can the image be displayed as it is in chips prog..
    By sh123 in forum Qt Programming
    Replies: 11
    Last Post: 7th February 2009, 08:15
  5. Qt Prog skip the file reading block
    By dgg32 in forum Qt Programming
    Replies: 10
    Last Post: 10th December 2008, 18: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.