Results 1 to 2 of 2

Thread: Send a Qstring into different computers connected in a network.

  1. #1
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Send a Qstring into different computers connected in a network.

    i want to send a string into different computers connected in a network.
    i developed a program,where i can enter the IP addresses of the systems, to which the string is to be send.There is a client and a server program. The client is running only at the system which is sending the string and server is running at all other systems. But the client program is only sending the string to the system whose IP address is entered first.Please HELP!!


    Client Code:

    Qt Code:
    1. void resoadv::on_pushButton_3_clicked()
    2. {
    3. QString l,h;
    4. QFile fi("ipreso.txt");
    5. fi.open(QIODevice::ReadOnly | QIODevice::Text);
    6. QTextStream stream(&fi);
    7. while(!fi.atEnd())
    8. {
    9. h=stream.readLine();
    10. sock->connectToHost(h, 40000);
    11. QFile f("resoadv.txt");
    12. f.open(QIODevice::ReadOnly | QIODevice::Text);
    13. QTextStream stream1(&f);
    14. l=stream1.readLine();
    15. f.close();
    16. //ui->label_3->setText(l);
    17. QByteArray block = "";
    18. block.append(l);
    19. const char *str = l.toLatin1();
    20. cout<<str;
    21. sock->write(block);
    22. sock->close();
    23. sock->disconnectFromHost();
    24. }
    25. QFile file("ipreso.txt");
    26. file.open(QIODevice::WriteOnly);
    27. file.close();
    28. }
    To copy to clipboard, switch view to plain text mode 




    server code:

    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; 6th March 2011 at 14:57. Reason: missing [code] tags

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Send a Qstring into different computers connected in a network.

    1) Use signals and slots. This will ensure each stage is complete before starting the next stage.

    2) Please use code tags when pasting code.

    3) UDP may be better - you can then broadcast your string once and all the interested parties can receive it rather than connecting to each host individually.

    4) Don't assume you'll receive the entire string in one go. You should append the string to a buffer until you know you have received all the text (ie, place a marker in the text somewhere or packetise your data so you know the expected length)

  3. The following user says thank you to squidge for this useful post:

    mebingj (6th March 2011)

Similar Threads

  1. Replies: 4
    Last Post: 30th November 2010, 21:09
  2. How to send enum through network?
    By Hostel in forum Newbie
    Replies: 4
    Last Post: 21st October 2010, 07:54
  3. find connected network devices
    By Remco in forum Qt Programming
    Replies: 4
    Last Post: 9th September 2010, 09:03
  4. Getting informed about newly connected network adapters
    By RThaden in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2010, 15:51
  5. Replies: 4
    Last Post: 12th April 2007, 19:52

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.