我的程式碼如下
程式單支溝通都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();
}