• 5330阅读
  • 5回复

可以幫我看一下我SOCKET的溝通有哪邊不對嗎 [复制链接]

上一主题 下一主题
离线osiers
 
只看楼主 正序阅读 楼主  发表于: 2008-11-20
— 本帖被 XChinux 从 General Qt Programming 移动到本区(2011-01-02) —
我的程式碼如下
程式單支溝通都OK
問題我分兩支的時候
一支啟動SERVER Listen
一支Send Data to Server
結果發現只有第一次有收到資料
第二次以後就沒訊息了

請各位高手幫我看一下哪邊有錯
謝謝
QSockTest::QSockTest(QWidget *parent)
    : QWidget(parent)
{
    QLabel *labelServerHost,*labelClientHost;

    QLabel *labelServerPort,*labelClientPort;

    QLabel *labelClientSendText;

    QPushButton *btnSend,*btnListen;

    QGridLayout *glayoutClient=new QGridLayout();
    QGridLayout *glayoutServer=new QGridLayout();
    QGroupBox *gboxClient=new QGroupBox(tr("Client"));
    QGroupBox *gboxServer=new QGroupBox(tr("Server"));
    QHBoxLayout *hlayout=new QHBoxLayout;
    QHostAddress host;
    host=QHostAddress::LocalHost;

    QList<QHostAddress> list=QNetworkInterface::allAddresses ();
    QHostAddress myAddress;
    myAddress=list.at(0);
///////////////////////
    QTextCodec::setCodecForTr(QTextCodec::codecForName("Big5-ETen"));

    labelClientHost=new QLabel(tr("Host"));
    m_edtClientHost=new QLineEdit();
    m_edtClientHost->setText(myAddress.toString());
    labelClientPort=new QLabel(tr("Port"));
    m_edtClientPort=new QLineEdit();
    m_edtClientPort->setText("1234");

    labelClientSendText=new QLabel(tr("SendText"));
    m_edtClientSendText=new QLineEdit();

    btnSend=new QPushButton(tr("Send"));
    connect(btnSend,SIGNAL(clicked()),this,SLOT(StartClientConnect()));

    glayoutClient->addWidget(labelClientHost,0,0);
    glayoutClient->addWidget(m_edtClientHost,0,1);
    glayoutClient->addWidget(labelClientPort,1,0);
    glayoutClient->addWidget(m_edtClientPort,1,1);
    glayoutClient->addWidget(labelClientSendText,2,0);
    glayoutClient->addWidget(m_edtClientSendText,2,1);
    glayoutClient->addWidget(btnSend,3,1);

    gboxClient->setLayout(glayoutClient);

/////////////server
    labelServerHost=new QLabel(tr("Host"));
    m_edtServerHost=new QLineEdit();
    m_edtServerHost->setText(myAddress.toString());

    labelServerPort=new QLabel(tr("Port"));
    m_edtServerPort=new QLineEdit();
    m_edtServerPort->setText("1234");

    btnListen=new QPushButton(tr("Listen"));
    connect(btnListen,SIGNAL(clicked()),this,SLOT(StartServerConnect()));

    glayoutServer->addWidget(labelServerHost,0,0);
    glayoutServer->addWidget(m_edtServerHost,0,1);
    glayoutServer->addWidget(labelServerPort,1,0);
    glayoutServer->addWidget(m_edtServerPort,1,1);
    glayoutServer->addWidget(btnListen,2,1);

    gboxServer->setLayout(glayoutServer);

    hlayout->addWidget(gboxClient);
    hlayout->addWidget(gboxServer);
    this->setLayout(hlayout);

    connect(&m_socketClient, SIGNAL(connected()), this, SLOT(StartSend()));
    connect(&m_socketClient, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));
    connect(&m_socketServer, SIGNAL(newConnection()),this, SLOT(acceptConnection()));
//    ui.setupUi(this);
}

QSockTest::~QSockTest()
{
    m_socketServer.close();
}
void QSockTest::ShowText(QString str)
{
    QMessageBox::about(0, "About", str);
}

void QSockTest::StartClientConnect()
{
    QHostAddress hostClient;
    quint16 iPort;
    QString strHost,strPort;
    QString strSend;
    strSend=m_edtClientSendText->text();
    ShowText("StartClientConnect");
    if(strSend.isEmpty())
        return ;
    strHost=m_edtClientHost->text();
    hostClient.setScopeId(strHost);
    strPort=m_edtClientPort->text();
    iPort=strPort.toInt();
    m_socketClient.connectToHost(hostClient,iPort);
}

void QSockTest::StartSend()
{
    QString strSend;
    strSend=m_edtClientSendText->text();
    m_socketClient.write(strSend.toAscii());
}

void QSockTest::displayError(QAbstractSocket::SocketError socketError)
{
    QMessageBox::information(this, "Network error","The following error occurred: " + m_socketClient.errorString());
    m_socketClient.close();
}

void QSockTest::StartServerConnect()
{
    quint16 iPort;
    QString strPort;
    strPort=m_edtServerPort->text();
    iPort=strPort.toUInt();
    ShowText("StartServerConnect");
    Listen(iPort);
}

void QSockTest::Listen(quint16 port)
{
    QString strHost;
    QHostAddress hostServer;
    strHost=m_edtServerHost->text();
    hostServer.setScopeId(strHost);
    m_socketServer.listen(hostServer, port);

}


void QSockTest::acceptConnection()
{
    m_socketTemp = m_socketServer.nextPendingConnection();
    connect(m_socketTemp, SIGNAL(readyRead()),this, SLOT(GetClientText()));
}

void QSockTest::GetClientText()
{
    QByteArray strReadData=m_socketTemp->read(100);
    ShowText(strReadData);
    m_socketClient.disconnectFromHost();
//    m_socketServer.close();
}
离线osiers
只看该作者 5楼 发表于: 2008-11-24
我的Server沒有關掉
關掉的是Client
我測試結果是  如果Server關掉  Client也會秀說Server已經關掉了
所以我覺得兩個還是有溝通成功的
只是不知道資料怎麼沒送到

謝謝大家的回覆
离线htyoung

只看该作者 4楼 发表于: 2008-11-21
看你的代码分析:
你用StartServerConnect()去启动listen,那么在你disconnect后,你的server已经关了,你在启动client就找不到server了。
如果这时你再StartServerConnect()一次, 你的client就能连上了。

我的分析是否对?

调试一下,看看你第二次socket是没有连上,还是连上了收不到?
***
QT5
***
离线osiers
只看该作者 3楼 发表于: 2008-11-21
我关掉是因为我每次Client要送之前会再连一次
m_socketClient.connectToHost(hostClient,iPort);

这样SERVER才会有newConnection的讯息
connect(&m_socketServer, SIGNAL(newConnection()),this, SLOT(acceptConnection()));

如果不关掉的话好像只做一次而已
不知道我的想法对不对
因为是边看范例边学习的


这程序在单一各程序执行是OK的
问题就在开两个程序  一个当SERVER 一个当CLINET的沟通
只会做一次  第二次就没动静了
离线vrcats
只看该作者 2楼 发表于: 2008-11-21
佩服1楼。我酝酿了好几次都不愿意看这段代码
离线htyoung

只看该作者 1楼 发表于: 2008-11-20
在函数
void QSockTest::GetClientText()
{
    QByteArray strReadData=m_socketTemp->read(100);
    ShowText(strReadData);
    m_socketClient.disconnectFromHost();
//    m_socketServer.close();
}

你收完100个字节后,用 m_socketClient.disconnectFromHost(); 关了socket。
***
QT5
***
快速回复
限100 字节
 
上一个 下一个