Results 1 to 5 of 5

Thread: Socket program not working

  1. #1
    Join Date
    Jan 2011
    Posts
    16
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Socket program not working

    I created a client and server program in Qt. It works when i test it from windows to linux,connected through TCP socket and also it works when i run client and server in the same system.But it is not working when i run it in two different linux OS connected in network..

  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: Socket program not working

    That's by far not enough information.

    What did you already try to solve it?
    How does your code look like?
    Do these two different linux systems work as expected? In other words, can you setup another client/server connection between them? (ftp for example?)
    Etc...

  3. #3
    Join Date
    Jan 2011
    Posts
    16
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Socket program not working

    One system fedora was installed and in the other system ubuntu was installed.

  4. #4
    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: Socket program not working

    That's like:
    I have two cookies, one with sprinkles, one without. Why doesn't my car start?

    Lets try to eliminate the variables. Please post a minimal compilable example demonstrating the problem.
    This way, the variable of your code being correct or wrong can be eliminated.

  5. #5
    Join Date
    Jan 2011
    Posts
    16
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Socket program not working

    CLIENT
    Qt Code:
    1. #include "dialog1.h"
    2. #include "ui_dialog1.h"
    3. #include "timeall.h"
    4. #include<QByteArray>
    5. #include <QFile>
    6. #include <QTextStream>
    7.  
    8.  
    9.  
    10. QString q,s,t,o,d;
    11. Dialog1::Dialog1(QWidget *parent) :
    12. QDialog(parent),
    13. ui(new Ui::Dialog1)
    14. {
    15. ui->setupUi(this);
    16. sock = new QTcpSocket(this);
    17. sock->connectToHost("localhost", 40000);
    18. connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(on_pushButton_2_clicked()));
    19. }
    20.  
    21. Dialog1::~Dialog1()
    22. {
    23. delete ui;
    24. sock->close();
    25.  
    26. }
    27.  
    28. void Dialog1::changeEvent(QEvent *e)
    29. {
    30. QDialog::changeEvent(e);
    31. switch (e->type()) {
    32. case QEvent::LanguageChange:
    33. ui->retranslateUi(this);
    34. break;
    35. default:
    36. break;
    37. }
    38. }
    39.  
    40. void Dialog1::on_pushButton_clicked()
    41. {
    42. //Time Advanced
    43. timeall *t1=new timeall;
    44. t1->setModal(true);
    45. t1->show();
    46.  
    47. QFile file("dd.txt");
    48. file.open(QIODevice::WriteOnly);
    49. QTextStream out(&file);
    50. q=ui->lineEdit_3->text();
    51. s=ui->lineEdit_2->text();
    52. if(s==NULL)
    53. {
    54. q.clear();
    55. o.clear();
    56. d="date +%Y%m%d -s ";
    57. t=ui->lineEdit_4->text();
    58. o+=t;
    59. t=ui->lineEdit_3->text();
    60. o+=t;
    61. t=ui->lineEdit->text();
    62. o+=t;
    63. d+=o;
    64.  
    65. out<<d;
    66.  
    67.  
    68.  
    69. /*sock->write(block);
    70.   sock->close();*/
    71.  
    72. // system(m);
    73. //d.clear();
    74. // o.clear();
    75. //t.clear();
    76. }
    77.  
    78. else if(q==NULL)
    79. {
    80. d="date +%T -s ";
    81. q=ui->lineEdit_2->text();
    82. o+=q;
    83. q=ui->lineEdit_5->text();
    84. o+=q;
    85. d+=o;
    86. //const char *m = d.toLatin1();//Qstring to const *char
    87. QByteArray block = "";
    88. block.append(d);
    89. //sock->write(block);
    90. //sock->close();
    91.  
    92. }
    93. else
    94. {
    95. d="date ";
    96. o+=q;
    97. q=ui->lineEdit->text();
    98. o+=q;
    99. q=ui->lineEdit_2->text();
    100. o+=q;
    101. q=ui->lineEdit_5->text();
    102. o+=q;
    103. q=ui->lineEdit_4->text();
    104. o+=q;
    105.  
    106. d+=o;
    107. QByteArray block = "";
    108. block.append(d);
    109. // sock->write(block);
    110. // sock->close();
    111.  
    112. }
    113.  
    114. }
    115.  
    116. void Dialog1::on_pushButton_2_clicked()
    117. {
    118.  
    119. q=ui->lineEdit_3->text();
    120. s=ui->lineEdit_2->text();
    121. if(s==NULL)
    122. {
    123. q.clear();
    124. o.clear();
    125. QString d="date +%Y%m%d -s ";
    126. t=ui->lineEdit_4->text();
    127. o+=t;
    128. t=ui->lineEdit_3->text();
    129. o+=t;
    130. t=ui->lineEdit->text();
    131. o+=t;
    132. d+=o;
    133.  
    134. const char *m = d.toLatin1();//Qstring to const *char
    135. QByteArray block = "";
    136. block.append(d);
    137. sock->write(block);
    138. sock->close();
    139.  
    140. // system(m);
    141. d.clear();
    142. o.clear();
    143. t.clear();
    144. }
    145.  
    146. else if(q==NULL)
    147. {
    148. QString d="date +%T -s ";
    149. q=ui->lineEdit_2->text();
    150. o+=q;
    151. q=ui->lineEdit_5->text();
    152. o+=q;
    153. d+=o;
    154. const char *m = d.toLatin1();//Qstring to const *char
    155. QByteArray block = "";
    156. block.append(d);
    157. sock->write(block);
    158. sock->close();
    159. //system(m);
    160. d.clear();
    161. o.clear();
    162. q.clear();
    163. }
    164. else
    165. {
    166. QString d="date ";
    167. o+=q;
    168. q=ui->lineEdit->text();
    169. o+=q;
    170. q=ui->lineEdit_2->text();
    171. o+=q;
    172. q=ui->lineEdit_5->text();
    173. o+=q;
    174. q=ui->lineEdit_4->text();
    175. o+=q;
    176.  
    177. d+=o;
    178. const char *m = d.toLatin1();//Qstring to const *char
    179. QByteArray block = "";
    180. block.append(d);
    181. sock->write(block);
    182. sock->close();
    183. // system(m);
    184. d.clear();
    185. o.clear();
    186. q.clear();
    187. }
    188.  
    189.  
    190.  
    191.  
    192. }
    To copy to clipboard, switch view to plain text mode 
    //SERVER
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10. // Set up connection
    11. server = new QTcpServer(this);
    12.  
    13. // If server does not listen
    14. if( !server->listen(QHostAddress::Any,40000))
    15. {
    16.  
    17. server->close();
    18. return; // Leave constructor, e.g. if port is currently used
    19. }
    20. connect(server, SIGNAL(newConnection()), this, SLOT(on_new_connection()));
    21. }
    22.  
    23. MainWindow::~MainWindow()
    24. {
    25. //delete ui;
    26. server->close();
    27. }
    28.  
    29. void MainWindow::changeEvent(QEvent *e)
    30. {
    31. QMainWindow::changeEvent(e);
    32. switch (e->type()) {
    33. case QEvent::LanguageChange:
    34. //ui->retranslateUi(this);
    35. break;
    36. default:
    37. break;
    38. }
    39. }
    40. void MainWindow::on_new_connection()
    41. {
    42. sock = server->nextPendingConnection();
    43. connect(sock, SIGNAL(readyRead()), this, SLOT(on_ready_read()));
    44. }
    45.  
    46. void MainWindow::on_ready_read()
    47. {
    48. QByteArray msg=sock->readAll();
    49. sock->close();
    50. QString message(msg);
    51. //ui->label->setText(message);
    52. const char *m = message.toLatin1();
    53. system(m);
    54. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 30th January 2011 at 11:22.

Similar Threads

  1. Beginner level program not working
    By Nishant in forum Newbie
    Replies: 9
    Last Post: 11th June 2010, 14:46
  2. I need example for Windows Mobile working program
    By Mr.QT in forum Qt Programming
    Replies: 3
    Last Post: 22nd June 2009, 09:19
  3. why this program is not working
    By ramamurthy.kv in forum Qt Programming
    Replies: 3
    Last Post: 8th May 2008, 13:26
  4. Replies: 2
    Last Post: 28th March 2008, 17:03
  5. Reading from TCP Socket crashes program
    By OnionRingOfDoom in forum Qt Programming
    Replies: 26
    Last Post: 27th January 2006, 19:32

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.