TCP 传送问题

weixin_38066613 2019-09-19 03:39: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));}
...全文
3 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

433

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧