#define VXMAINWINDOW_H
#include <QtNetwork/QLocalServer>
#include <QtNetwork/QLocalSocket>
class QTextEdit;
{
Q_OBJECT
CVxMainWindow(QWidget *parent=NULL);
~CVxMainWindow();
protected:
void resizeEvent(QResizeEvent *);
private slots:
void Btn_ListenClickedSlot();
void Btn_StopListenClickedSlot();
void newConnectionSlot();
void dataReceived();
private:
QLocalServer *m_pServer;
QLocalSocket *m_pSocket;
QPushButton *m_pBtn_StopListen;
QTextEdit *m_pEdt_Info;
};
: QWidget(parent)
{
m_pBtn_Listen = new QPushButton(QObject::tr("开始监听"), this);
m_pBtn_StopListen = new QPushButton(QObject::tr("停止监听"), this);
m_pEdt_Info = new QTextEdit(this);
m_pServer = new QLocalServer(this);
connect(m_pBtn_StopListen, SIGNAL(clicked()), this, SLOT(Btn_StopListenClickedSlot()));
connect(m_pServer, SIGNAL(newConnection()), this, SLOT(newConnectionSlot()));
}
{
{
m_pBtn_Listen->setGeometry(10, 5, 80, 20);
m_pBtn_StopListen->setGeometry(100, 5, 80, 20);
m_pEdt_Info->setGeometry(0, 30, width(), height() - 30);
}
{
if (!m_pServer->isListening())
{
if (m_pServer->listen(QObject::tr("AAA")))
{
m_pEdt_Info->append(QObject::tr("打开监听端口成功!"));
}
else
{
m_pEdt_Info->append(QObject::tr("打开监听端口失败!"));
}
}
else
{
m_pEdt_Info->append(QObject::tr("正在监听中...!"));
}
}
{
if (m_pServer->isListening())
{
m_pServer->close();
m_pEdt_Info->append(QObject::tr("停止监听!"));
}
}
{
m_pEdt_Info->append(QObject::tr("有新客户端连接到服务器"));
m_pSocket = m_pServer->nextPendingConnection();
connect(m_pSocket, SIGNAL(disconnected()), m_pSocket, SLOT(deleteLater()));
connect(m_pSocket, SIGNAL(readyRead()),this, SLOT(dataReceived()));
QString vMsgStr = QObject::tr("Welcome");
if((length=m_pSocket->write(vMsgStr.toLatin1(),vMsgStr.length()))!=vMsgStr.length())
{
}
{
while(m_pSocket->bytesAvailable())
{
QString vTemp;
vTemp = m_pSocket->readLine();
m_pEdt_Info->append(vTemp);
QString vMsgStr = QObject::tr("回复:") + vTemp;
if((length=m_pSocket->write(vMsgStr.toLatin1(),vMsgStr.length()))!=vMsgStr.length())
{
}
}
#define VXMAINWINDOW_H
#include <QtNetwork/QLocalSocket>
class QTextEdit;
class QLineEdit;
{
Q_OBJECT
CVxMainWindow(QWidget *parent=NULL);
~CVxMainWindow();
protected:
void resizeEvent(QResizeEvent *);
private slots:
void Btn_ConnectClickedSlot();
void Btn_DisConnectClickedSlot();
void Btn_SendClickedSlot();
void connectedSlot();
void disconnectedSlot();
void dataReceived();
void displayError(QAbstractSocket::SocketError);
private:
QLocalSocket *m_pSocket;
QPushButton *m_pBtn_DisConnect;
QTextEdit *m_pEdt_Info;
QLineEdit *m_pEdt_Send;
QPushButton *m_pBtn_Send;
};
: QWidget(parent)
{
m_pBtn_Connect = new QPushButton(QObject::tr("连接服务器"), this);
m_pBtn_DisConnect = new QPushButton(QObject::tr("断开连接"), this);
m_pEdt_Send = new QLineEdit(this);
m_pBtn_Send = new QPushButton(QObject::tr("发送"), this);
m_pEdt_Info = new QTextEdit(this);
m_pSocket = new QLocalSocket(this);
connect(m_pBtn_DisConnect, SIGNAL(clicked()), this, SLOT(Btn_DisConnectClickedSlot()));
connect(m_pBtn_Send, SIGNAL(clicked()), this, SLOT(Btn_SendClickedSlot()));
connect(m_pSocket, SIGNAL(connected()), this, SLOT(connectedSlot()));
connect(m_pSocket, SIGNAL(disconnected()), this, SLOT(disconnectedSlot()));
connect(m_pSocket, SIGNAL(readyRead()),this, SLOT(dataReceived()));
connect(m_pSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
}
{
{
m_pBtn_Connect->setGeometry(10, 5, 80, 20);
m_pBtn_DisConnect->setGeometry(100, 5, 80, 20);
m_pEdt_Send->setGeometry(10, 30, 150, 20);
m_pBtn_Send->setGeometry(170, 30, 80, 20);
m_pEdt_Info->setGeometry(0, 60, width(), height() - 60);
}
{
m_pSocket->connectToServer(QObject::tr("AAA"));
}
{
m_pSocket->disconnectFromServer();
}
{
int length = 0;
QString vMsgStr = m_pEdt_Send->text();
if((length=m_pSocket->write(vMsgStr.toLatin1(),vMsgStr.length()))!=vMsgStr.length())
{
m_pEdt_Info->append(QObject::tr("发送信息失败:") + vMsgStr);
}
}
{
m_pEdt_Info->append(QObject::tr("成功连接到服务器!"));
}
{
m_pEdt_Info->append(QObject::tr("断开与服务器的连接!"));
}
{
while(m_pSocket->bytesAvailable())
{
QString vTemp;
vTemp = m_pSocket->readLine();
m_pEdt_Info->append(vTemp);
}
}
{