QT中的网络编程实例
Qclient.h
/**************************************************************************** ** $Id: /sample/10/qclient.h 2.3.2 edited 2004-10-12 $ **
** Copyright (C) 2004-2005 OURSELEC AS. All rights reserved. ** Http://www.ourselec.com
** This file is part of an example program for Qt. This example
** program may be used, distributed and modified without limitation. **
*****************************************************************************/ #ifndef QCLIENT_H #define QCLIENT_H #include
#include
Q_OBJECT
public:
QClient(QWidget *parent = 0, const char *name = 0); private slots:
void closeConnection(); void sendToServer(); void connectToServer(); void socketReadyRead(); void socketConnected();
void socketConnectionClosed(); void socketClosed(); void socketError(int); private:
QSocket *socket; QTextView *infoText;
QLineEdit *addrText; QLineEdit *portText; QLineEdit *inputText; };
#endif //QCLIENT_H
Qclient.cpp
/**************************************************************************** ** $Id: /sample/10/qclient.h 2.3.2 edited 2004-10-12 $ **
** Copyright (C) 2004-2005 OURSELEC AS. All rights reserved. ** Http://www.ourselec.com
** This file is part of an example program for Qt. This example
** program may be used, distributed and modified without limitation. **
*****************************************************************************/ #include \#include
#include
QClient::QClient(QWidget *parent, const char *name) : QWidget(parent, name) {
infoText = new QTextView(this); QHBox *hb = new QHBox(this); inputText = new QLineEdit(hb);
QHBox *addrBox = new QHBox(this);
QLabel *ip = new QLabel(\ip->setAlignment(1);
addrText = new QLineEdit(addrBox);
QLabel *port = new QLabel(\
port->setAlignment(1);
portText = new QLineEdit(addrBox); QHBox *buttonBox = new QHBox(this);
// QPushButton *Connect = new QPushButton(tr(\QPushButton *send = new QPushButton(tr(\
QPushButton *close = new QPushButton(tr(\ buttonBox);
QPushButton *quit = new QPushButton(tr(\
QPushButton *Connect = new QPushButton(tr(\connect(send, SIGNAL(clicked()), SLOT(sendToServer()) );
connect(close, SIGNAL(clicked()), SLOT(closeConnection()) ); connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()) );
connect(Connect, SIGNAL(clicked()), SLOT(connectToServer()) );
//create the socket and connect various of its signals socket = new QSocket(this);
connect(socket, SIGNAL(connected()), SLOT(socketConnected()) );
connect(socket, SIGNAL(connectionClosed()), SLOT(socketConnectionClosed()) ); connect(socket, SIGNAL(readyRead()), SLOT(socketReadyRead()) ); connect(socket, SIGNAL(error(int)), SLOT(socketError(int)) );
QVBoxLayout *l = new QVBoxLayout(this); l->addWidget(infoText, 10); l->addWidget(hb, 1);
l->addWidget(addrBox, 1); l->addWidget(buttonBox, 1); // l->addWidget(Connect, 1); // l->addWidget(close, 1); // l->addWidget(quit, 1); //connect to the server
infoText->append(tr(\// socket->connectToHost(host, port); }
void QClient::closeConnection() {
socket->close();
if (QSocket::Closing == socket->state()) { // We have a delayed close
connect(socket, SIGNAL(delayedCloseFinished()), SLOT(socketClosed())); } else {
// The socket is closed socketClosed(); } }
void QClient::sendToServer() {
// write to the server
if (QSocket::Connected == socket->state()) { QTextStream os(socket);
os << inputText->text() << \ inputText->setText(\} else {
// The socket is unconnected
infoText->append(tr(\} }
void QClient::connectToServer() {
// connect to the server
socket->connectToHost(addrText->text(), (portText->text()).toInt()); }
void QClient::socketReadyRead() {
// read from the server
while (socket->canReadLine()) {
infoText->append(socket->readLine()); } }
void QClient::socketConnected() {
相关推荐: