Ok, so I have a program which acts as a server, and can write data to a socket. Now when I use the client program to connect to the server, the client connects just fine, but it crashes whenever it goes to read the data. I'm not entirely sure how the whole QIODevice thing plays into it, but what I wrote makes sense, at least to me :P. Anyway, here's the code:

Qt Code:
  1. // Server/Client-related connections
  2. connect(&tcpServer, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
  3. connect(&tcpServer, SIGNAL(connectionAccepted()), this, SLOT(sendSpeed()));
  4. connect(&tcpClient, SIGNAL(connected()), this, SLOT(readSpeed()));
  5.  
  6. connect(&tcpClient, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
  7. connect(&tcpClient, SIGNAL(state(QAbstractSocket::SocketState)), this, SLOT(displayStatus(QAbstractSocket::SocketState)));
  8.  
  9. // Set up layout
  10. layout->addWidget(connectButton, 0, 0, 1, 2);
  11. layout->addWidget(slider, 1, 0, 1, 2);
  12. layout->addWidget(label, 3, 0);
  13. //layout->addWidget(label2, 3, 0);
  14. layout->addWidget(ipLabel, 5, 0);
  15. win->setLayout(layout);
  16.  
  17. } // MainWindow()
  18.  
  19. ////////////////////////////////////////////////////////////////////////
  20. // SERVER FUNCTIONS //
  21.  
  22. // startServer(QString ip, quint16 port) - Starts the server
  23. ////////////////////////////////////////////////////////////////////////
  24. void MainWindow::startServer(QString ip, quint16 port)
  25. {
  26. statusBar->showMessage("Server created", 0);
  27. if (!tcpServer.listen(QHostAddress(ip), port))
  28. {
  29. statusBar->showMessage(tr("Unable to start the server: %1.", 0).arg(tcpServer.errorString()));
  30. return;
  31. }
  32. else
  33. {
  34. statusBar->showMessage(tr("Listening on port %1...", 0).arg(tcpServer.serverPort()));
  35. }
  36. }
  37.  
  38. // acceptConnection() - Completes the connection to the client
  39. ////////////////////////////////////////////////////////////////////////
  40. void MainWindow::acceptConnection()
  41. {
  42. slider->setAmpValue(10);
  43. slider->setFreqValue(10);
  44. clientConnection = tcpServer.nextPendingConnection();
  45. statusBar->showMessage("Connection established.", 0);
  46. emit connectionAccepted();
  47. }
  48.  
  49. // sendSpeed() - Sends the slider speed to the client
  50. ////////////////////////////////////////////////////////////////////////
  51. void MainWindow::sendSpeed()
  52. {
  53. QByteArray block;
  54. QDataStream out(&block, QIODevice::WriteOnly);
  55. value = slider->ampValue();
  56.  
  57. // Write the slider value to the output buffer
  58. out.setVersion(QDataStream::Qt_4_0);
  59. out << value;
  60.  
  61. // Send the linear slider value to the client
  62. clientConnection->write(block);
  63. }
  64.  
  65. // SERVER FUNCTIONS
  66.  
  67. ////////////////////////////////////////////////////////////////////////
  68. // CLIENT FUNCTIONS //
  69.  
  70. // getServer() - Establishes a connection to the server
  71. ////////////////////////////////////////////////////////////////////////
  72. void MainWindow::getServer(QString ip, quint16 port)
  73. {
  74. blockSize = 0;
  75. tcpClient.connectToHost(QHostAddress(ip), port);
  76. }
  77.  
  78. // readSpeed() - Reads the slider speed value into a label
  79. ////////////////////////////////////////////////////////////////////////
  80. void MainWindow::readSpeed()
  81. {
  82. QDataStream in(&tcpClient);
  83. in.setVersion(QDataStream::Qt_4_0);
  84.  
  85. quint8 sliderValue;
  86. in >> sliderValue;
  87. label->setNum(sliderValue);
  88. }
To copy to clipboard, switch view to plain text mode