|
—
本帖被 XChinux 从 General Qt Programming 移动到本区(2011-01-02)
—
我有TCP的问题。 UDP就没事。 每次 run "sender.cpp" 的时候就会 crash. sender.cpp file: - #include <QtGui>
- #include <QtNetwork>
- #include <stdlib.h>
- #include "sender.h"
- Sender::Sender(QWidget *parent)
- : QDialog(parent)
- {
- statusLabel = new QLabel;
- quitButton = new QPushButton(tr("Quit"));
- quitButton->setAutoDefault(false);
- teMessage = new QTextEdit(this);
- sendButton = new QPushButton(tr("Send"));
- port = 5824;
- server = new QTcpServer(this);
- serverSocket = new QTcpSocket(this);
- server->listen(QHostAddress::Any, port);
- connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
- connect(sendButton, SIGNAL(clicked()), this, SLOT(sendMessage()));
- // connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune()));
- QHBoxLayout *buttonLayout = new QHBoxLayout;
- buttonLayout->addStretch(1);
- buttonLayout->addWidget(sendButton);
- buttonLayout->addWidget(quitButton);
- QVBoxLayout *mainLayout = new QVBoxLayout;
- mainLayout->addWidget(teMessage);
- mainLayout->addWidget(statusLabel);
- mainLayout->addLayout(buttonLayout);
- setLayout(mainLayout);
- setWindowTitle(tr("Server"));
- }
- void Sender::sendMessage()
- {
- serverSocket = server->nextPendingConnection();
- QByteArray datagram;
- QDataStream out(&datagram, QIODevice::WriteOnly);
- out.setVersion(QDataStream::Qt_4_1);
- QString content = teMessage->toPlainText();
- out << content;
- out.setDevice(serverSocket);
- //tcpSocket->writeDatagram(datagram, QHostAddress::LocalHost, 5824);
- connect(serverSocket, SIGNAL(disconnected()), serverSocket, SLOT(deleteLater()));
- serverSocket->write(datagram);
- serverSocket->disconnectFromHost();
- teMessage->clear();
- }
receiver.cpp file: - #include <QtGui>
- #include <QtNetwork>
- #include "receiver.h"
- Receiver::Receiver(QWidget *parent)
- : QDialog(parent)
- {
- statusLabel = new QLabel(tr("Listening for broadcasted messages"));
- quitButton = new QPushButton(tr("&Quit"));
- teShowMessage = new QTextEdit(this);
- teShowMessage->setReadOnly(true);
- tcpSocket = new QTcpSocket(this);
- QString host = "localhost";
- port = 5824;
- tcpSocket->connectToHost(host, port);
- blockSize = 0;
- connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));
- connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
- QHBoxLayout *buttonLayout = new QHBoxLayout;
- buttonLayout->addStretch(1);
- buttonLayout->addWidget(quitButton);
- QVBoxLayout *mainLayout = new QVBoxLayout;
- mainLayout->addWidget(teShowMessage);
- mainLayout->addWidget(statusLabel);
- mainLayout->addLayout(buttonLayout);
- setLayout(mainLayout);
- setWindowTitle(tr("Broadcast Receiver"));
- }
- void Receiver::processPendingDatagrams()
- {
- QDataStream in(tcpSocket);
- in.setVersion(QDataStream::Qt_4_1);
- if (blockSize == 0) {
- if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
- return;
- in >> blockSize;
- }
- if (tcpSocket->bytesAvailable() < blockSize)
- return;
- QString content;
- in >> content;
- teShowMessage->append(tr("Received datagram: \"%1\"").arg(content));
- }
|