• 3992阅读
  • 0回复

TCP 传送问题 [复制链接]

上一主题 下一主题
离线cooler123
 
只看楼主 倒序阅读 楼主  发表于: 2007-09-19
— 本帖被 XChinux 从 General Qt Programming 移动到本区(2011-01-02) —
我有TCP的问题。 UDP就没事。 每次 run "sender.cpp" 的时候就会 crash.


sender.cpp file:

  1. #include <QtGui>
  2. #include <QtNetwork>
  3. #include <stdlib.h>
  4. #include "sender.h"
  5. Sender::Sender(QWidget *parent)
  6.     : QDialog(parent)
  7. {
  8.     statusLabel = new QLabel;
  9.     quitButton = new QPushButton(tr("Quit"));
  10.     quitButton->setAutoDefault(false);
  11.     teMessage = new QTextEdit(this);
  12.     sendButton = new QPushButton(tr("Send"));
  13.     port = 5824;
  14.     server = new QTcpServer(this);
  15.     serverSocket = new QTcpSocket(this);
  16.     server->listen(QHostAddress::Any, port);
  17.     connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
  18.     connect(sendButton, SIGNAL(clicked()), this, SLOT(sendMessage()));
  19.   // connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune()));
  20.     QHBoxLayout *buttonLayout = new QHBoxLayout;
  21.     buttonLayout->addStretch(1);
  22.     buttonLayout->addWidget(sendButton);
  23.     buttonLayout->addWidget(quitButton);
  24.     QVBoxLayout *mainLayout = new QVBoxLayout;
  25.     mainLayout->addWidget(teMessage);
  26.     mainLayout->addWidget(statusLabel);
  27.     mainLayout->addLayout(buttonLayout);
  28.     setLayout(mainLayout);
  29.     setWindowTitle(tr("Server"));
  30. }
  31. void Sender::sendMessage()
  32. {       
  33.     serverSocket = server->nextPendingConnection();
  34.     QByteArray datagram;
  35.     QDataStream out(&datagram, QIODevice::WriteOnly);
  36.     out.setVersion(QDataStream::Qt_4_1);
  37.     QString content = teMessage->toPlainText();
  38.     out << content;
  39.     out.setDevice(serverSocket);
  40.     //tcpSocket->writeDatagram(datagram, QHostAddress::LocalHost, 5824);
  41. connect(serverSocket, SIGNAL(disconnected()), serverSocket, SLOT(deleteLater()));
  42. serverSocket->write(datagram);
  43. serverSocket->disconnectFromHost();
  44.     teMessage->clear();
  45. }


receiver.cpp file:
  1. #include <QtGui>
  2. #include <QtNetwork>
  3. #include "receiver.h"
  4. Receiver::Receiver(QWidget *parent)
  5.     : QDialog(parent)
  6. {
  7.     statusLabel = new QLabel(tr("Listening for broadcasted messages"));
  8.     quitButton = new QPushButton(tr("&Quit"));
  9.     teShowMessage = new QTextEdit(this);
  10.     teShowMessage->setReadOnly(true);
  11.     tcpSocket = new QTcpSocket(this);
  12.     QString host = "localhost";
  13.     port = 5824;
  14.     tcpSocket->connectToHost(host, port);
  15.     blockSize = 0;
  16.     connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));
  17.     connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
  18.     QHBoxLayout *buttonLayout = new QHBoxLayout;
  19.     buttonLayout->addStretch(1);
  20.     buttonLayout->addWidget(quitButton);
  21.     QVBoxLayout *mainLayout = new QVBoxLayout;
  22.     mainLayout->addWidget(teShowMessage);
  23.     mainLayout->addWidget(statusLabel);
  24.     mainLayout->addLayout(buttonLayout);
  25.     setLayout(mainLayout);
  26.     setWindowTitle(tr("Broadcast Receiver"));
  27. }
  28. void Receiver::processPendingDatagrams()
  29. {
  30.     QDataStream in(tcpSocket);
  31.     in.setVersion(QDataStream::Qt_4_1);
  32.        if (blockSize == 0) {
  33.         if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
  34.             return;
  35.         in >> blockSize;
  36.     }
  37.     if (tcpSocket->bytesAvailable() < blockSize)
  38.         return;
  39.     QString content;
  40.     in >> content;
  41.     teShowMessage->append(tr("Received datagram: \"%1\"").arg(content));
  42. }
快速回复
限100 字节
 
上一个 下一个