Commit 0f751e3a by PotatoGim

이전 작업을 히스토리 없이 가져오기

parent 03193bb6
#-------------------------------------------------
#
# Project created by QtCreator 2011-06-12T16:58:21
#
#-------------------------------------------------
QT += core gui network
TARGET = Client
TEMPLATE = app
SOURCES += main.cpp\
client.cpp \
clientwindow.cpp
HEADERS += \
client.h \
clientwindow.h
FORMS +=
#include "client.h"
#include <QtGui>
Client::Client(QWidget *parent)
{
window = parent;
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(recvData()));
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(displayError(QAbstractSocket::SocketError)));
}
void Client::sendData (QString *data)
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16) 0;
out << *data;
out.device()->seek(0);
out << (quint16) (block.size() - sizeof(quint16));
connect (clientConnection, SIGNAL(disconnected()),
clientConnection, SLOT(deleteLater()));
clientConnection->write (block);
clientConnection->disconnectFromHost();
}
void Client::recvData()
{
QDataStream in(tcpSocket);
in.setVersion(QDataStream::Qt_4_0);
if (blockSize == 0) {
if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
return;
in >> blockSize;
}
if (tcpSocket->bytesAvailable() < blockSize)
return;
in >> strData;
/*
if (nextFortune == currentFortune) {
QTimer::singleShot(0, this, SLOT(requestNewFortune()));
return;
*/
}
void Client::displayError (QAbstractSocket::SocketError socketError)
{
switch (socketError) {
case QAbstractSocket::RemoteHostClosedError:
break;
case QAbstractSocket::HostNotFoundError:
QMessageBox::information(this, tr("Client"),
tr("The host was not found. Please check the "
"host name and port settings."));
break;
case QAbstractSocket::ConnectionRefusedError:
QMessageBox::information(this, tr("Client"),
tr("The connection was refused by the peer. "
"Make sure the fortune server is running, "
"and check that the host name and port "
"settings are correct."));
break;
default:
QMessageBox::information(this, tr("Client"),
tr("The following error occurred: %1.")
.arg(tcpSocket->errorString()));
}
}
#ifndef CLIENT_H
#define CLIENT_H
#include <QtCore>
#include <QtNetwork>
//#include "data.h"
class Client
{
public: // Constructor & Destructor
Client(QWidget *parent = 0);
~Client();
public: // Methods
private: // Attrs
QWidget *window;
QTcpSocket *tcpSocket;
QString strData;
quint16 blockSize;
private slots:
void sendData();
void recvData();
void displayError (QAbstractSocket::SocketError socketError);
};
#endif // CLIENT_H
#include "clientwindow.h"
ClientWindow::ClientWindow(QWidget *parent) :
QWidget(parent)
{
client = new Client (this);
setupUI();
connect (btnLogin, SIGNAL(clicked()), client, SLOT(Client::sendData()));
connect (btnQuit, SIGNAL(clicked()), this, SLOT(quit()));
}
ClientWindow::~ClientWindow()
{
delete lbID;
delete lbPW;
delete edtID;
delete edtPW;
delete btnLogin;
delete btnQuit;
}
void ClientWindow::setupUI()
{
lbID = new QLabel (QString::fromLocal8Bit("아이디 : "), this);
lbID->setGeometry(10, 20, 60, 20);
lbPW = new QLabel (QString::fromLocal8Bit("비밀번호 : "), this);
lbPW->setGeometry(10, 50, 60, 20);
edtID = new QLineEdit (this);
edtID->setGeometry(80, 20, 100, 20);
edtPW = new QLineEdit (this);
edtPW->setGeometry(80, 50, 100, 20);
btnLogin = new QPushButton (QString::fromLocal8Bit("로그인"), this);
btnLogin->setGeometry(190, 20, 50, 20);
btnQuit = new QPushButton (QString::fromLocal8Bit("종료"), this);
btnQuit->setGeometry(190, 50, 50, 20);
}
void ClientWindow::login()
{
/*
Sending login message
*/
}
void ClientWindow::quit()
{
this->close();
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <QtGui>
#include "client.h"
class ClientWindow : public QWidget
{
Q_OBJECT
public: // Constructor & Destructor
explicit ClientWindow (QWidget *parent = 0);
~ClientWindow ();
public: // Attr
/* Layout */
QLabel *lbID;
QLabel *lbPW;
QLineEdit *edtID;
QLineEdit *edtPW;
QPushButton *btnLogin;
QPushButton *btnQuit;
Client *client;
private: // Method
void setupUI();
private slots:
void login();
void quit();
};
#endif // MAINWINDOW_H
#include <QtGui/QApplication>
#include "clientwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ClientWindow w;
w.show();
return a.exec();
}
#-------------------------------------------------
#
# Project created by QtCreator 2011-06-12T19:35:47
#
#-------------------------------------------------
QT += core gui network
TARGET = Server
TEMPLATE = app
SOURCES += main.cpp\
server.cpp \
serverwindow.cpp \
msgdata.cpp
HEADERS += \
server.h \
serverwindow.h \
msgdata.h
FORMS +=
#include <QApplication>
#include <QtCore>
#include <stdlib.h>
#include "serverwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ServerWindow mw;
mw.show();
return a.exec();
}
#include "msgdata.h"
MsgData::MsgData ()
{
/*
ToDo : Default Constructor
*/
}
MsgData::MsgData (DataFlag, QString &srcID)
{
m_flag = 0;
switch (m_flag) {
case 0: // Normal Trans
break;
case 1: // Login Trans
break;
default:
break;
}
}
MsgData::MsgData (DataFlag, QString &srcID, QString &dstID)
{
/*
ToDo : Overrided Constructor
*/
}
MsgData::MsgData (DataFlag, QString &srcID, QString &dstID, QByteArray &data)
{
/*
ToDo : Overrided Constructor
*/
}
#ifndef MSGDATA_H
#define MSGDATA_H
#include <QtCore>
/*
0 : Data
1~127 : Client
128~255 : Server
*/
typedef enum dataflag {
Data=0x00,
ReqLogin=0x01,
ResLogin=0x10
} DataFlag;
class MsgData
{
public: // Constructor & Destructor
MsgData();
MsgData(DataFlag, QString &srcID);
MsgData(DataFlag, QString &srcID, QString &dstID);
MsgData(DataFlag, QString &srcID, QString &dstID, QByteArray &data);
public: // Attr
public: // Method
void setFlag (short);
private: // Attr
int m_flag;
QString m_srcID;
QString m_dstID;
QByteArray *m_data;
private: // Method
};
#endif // MSGDATA_H
#include "server.h"
#include <QMessageBox>
#define DEFAULT_SOCK 5
/////////////////////////////////////
// beginning constructor from here //
/////////////////////////////////////
Server::Server (QWidget *parent) : QTcpServer (parent)
{
window = parent;
this->setMaxPendingConnections(DEFAULT_SOCK);
if (!this->listen(QHostAddress::Any, (quint16)8858)) {
QMessageBox::critical(window, tr("Server"),
tr("Unable to start the server: %1.")
.arg(this->errorString()));
return;
}
}
///////////////////////////////////////
// beginning public method from here //
///////////////////////////////////////
void Server::setPortNum(quint16 port)
{
/*
ToDo : Setting the port number that running server currently.
*/
}
quint16 Server::getPortNum()
{
return this->serverPort();
}
//////////////////////////////////////
// beginning private slot from here //
//////////////////////////////////////
void Server::addConnection()
{
// Adding new connection to connection list
QTcpSocket *connection = nextPendingConnection();
connections.append(connection);
// Adding new buffer to hash table (K : Connection, V : Buffer)
QBuffer *buffer = new QBuffer (this);
buffer->open (QIODevice::ReadWrite);
buffers.insert (connection, buffer);
// Connect signal to slot method
connect (connection, SIGNAL(readyRead()), SLOT(recvData()));
connect (connection, SIGNAL(disconnected()), SLOT(removeConnection()));
}
void Server::removeConnection()
{
QTcpSocket *socket = static_cast <QTcpSocket *> (sender());
QBuffer *buffer = buffers.take(socket);
buffer->close();
buffer->deleteLater();
connections.removeAll(socket);
socket->deleteLater();
}
void Server::sendData()
{
// sending...
}
void Server::recvData()
{
QTcpSocket *socket = static_cast <QTcpSocket *> (sender());
QBuffer *buffer = buffers.value (socket);
qint64 bytes = buffer->write (socket->readAll());
buffer->seek (buffer->pos() - bytes);
/*
ToDo : Getting Destination ID
*/
/*
ToDo : BroadCasting Message
while (buffer->canReadLine()) {
QByteArray line = buffer->readLine();
foreach (QTcpSocket *connection, connections) {
connection->write (line);
}
}
*/
}
int Server::parseFlag(QBuffer *buffer)
{
/*
ToDo : Parsing Data Flag
*/
return 0;
}
#ifndef SERVER_H
#define SERVER_H
#include <QtCore>
#include <QtNetwork>
#include "msgdata.h"
class Server : public QTcpServer
{
Q_OBJECT
public: // Constructor && Destructor
explicit Server (QWidget *parent = 0);
QWidget *window;
public: // Method
void setPortNum(quint16 port);
quint16 getPortNum();
private: // Attr
QList <QTcpSocket *> connections;
QHash <QTcpSocket *, QBuffer *> buffers;
private slots: // Slot
void addConnection();
void removeConnection();
void sendData();
void recvData();
int parseFlag(QBuffer *buffer);
};
#endif // SERVER_H
#include "serverwindow.h"
#include "server.h"
ServerWindow::ServerWindow(QWidget *parent) :
QWidget(parent)
{
Server srv (this);
}
#ifndef SERVERWINDOW_H
#define SERVERWINDOW_H
#include <QWidget>
#include <QtGui>
class ServerWindow : public QWidget
{
Q_OBJECT
public:
explicit ServerWindow(QWidget *parent = 0);
private:
signals:
public slots:
};
#endif // SERVERWINDOW_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment