Hi. I have tried upto my level how to create a Server to handle many clients, but my non of the debug msgs are working. Im presenting my simple Server client code.plz support me.
Teamstatus.h
Q_OBJECT
public:
~TeamStatus();
void listen();
void socketdisc();
protected:
void incomingConnection(int socketfd);
private:
Ui::TeamStatus *ui1;
QSet <QTcpSocket
*> sockets;
public slots:
void read_socket();
class TeamStatus : public QMainWindow {
Q_OBJECT
public:
TeamStatus(QWidget *parent = 0);
~TeamStatus();
void listen();
void socketdisc();
protected:
void changeEvent(QEvent *e);
void incomingConnection(int socketfd);
private:
Ui::TeamStatus *ui1;
QTcpServer *server;
QSet <QTcpSocket*> sockets;
public slots:
void read_socket();
To copy to clipboard, switch view to plain text mode
Teamstatus.cpp
TeamStatus
::TeamStatus(QWidget *parent
) : ui1(new Ui::TeamStatus)
{
ui1->setupUi(this);
TeamStatus::~TeamStatus()
{
delete ui1;
}
void TeamStatus::listen()
{
hAddr.setAddress("192.168.64.55");
qDebug("Listening to new connection");
qDebug("listening for new socket")
}
void TeamStatus::incomingConnection(int socketfd)
{{
socket=server->nextPendingConnection();
{
socket->setSocketDescriptor(socketfd);
qDebug("new connection1 established");
}
{
}
qDebug("new connection1 established");
}
read_socket();
}
void TeamStatus::read_socket()
{
qDebug("after readingy);
QByteArray buffer= socket->readLine();
... check for the buffer, do something
void TeamStatus::write_socket()
{
"How to write the data or send packet to single client or for all clients"
}
TeamStatus::TeamStatus(QWidget *parent) :
QMainWindow(parent),
ui1(new Ui::TeamStatus)
{
ui1->setupUi(this);
server=new QTcpServer(this);
TeamStatus::~TeamStatus()
{
delete ui1;
}
void TeamStatus::listen()
{
QHostAddress hAddr;
hAddr.setAddress("192.168.64.55");
server->listen(QHostAddress::Any,1234);
qDebug("Listening to new connection");
qDebug("listening for new socket")
}
void TeamStatus::incomingConnection(int socketfd)
{{
socket=server->nextPendingConnection();
if((socket->state()==QTcpSocket::ConnectedState))
{
QTcpSocket *socket = new QTcpSocket;
socket->setSocketDescriptor(socketfd);
qDebug("new connection1 established");
}
{
}
qDebug("new connection1 established");
}
read_socket();
}
void TeamStatus::read_socket()
{
QTcpSocket *socket=new QTcpSocket;
socket=(QTcpSocket*) sender();
qDebug("after readingy);
QByteArray buffer= socket->readLine();
... check for the buffer, do something
void TeamStatus::write_socket()
{
"How to write the data or send packet to single client or for all clients"
}
To copy to clipboard, switch view to plain text mode
Client.h
namespace Ui {
class TCPClient;
}
Q_OBJECT
public:
~TCPClient();
protected:
private:
Ui::TCPClient *ui;
private slots:
void connecttoserver();
void on_connected();
void write();
void write1();
void DisconnectServer();
};
#endif // TCPCLIENT_H
class QTcpSocket;
namespace Ui {
class TCPClient;
}
class TCPClient : public QWidget {
Q_OBJECT
public:
TCPClient(QWidget *parent = 0);
~TCPClient();
protected:
void changeEvent(QEvent *e);
private:
Ui::TCPClient *ui;
QLineEdit *le,*le1;
QTextEdit *te,*te1;
QPushButton *pb_connect, *pb_disconnect;
QPushButton *pb_send,*pb_send1;
QTcpSocket *socket;
QString CHAT;
private slots:
void connecttoserver();
void on_connected();
void write();
void write1();
void DisconnectServer();
};
#endif // TCPCLIENT_H
To copy to clipboard, switch view to plain text mode
Client.cpp
TCPClient
::TCPClient(QWidget *parent
) : ui(new Ui::TCPClient)
{
ui->setupUi(this);
le=ui->lineEdit;
le->setDisabled(true);
le1=ui->lineEdit_2;
le1->setDisabled(true);
te=ui->textEdit;
te->setDisabled(true);
te1=ui->textEdit_2;
te1->setDisabled(true);
pb_connect=ui->pushButton;
pb_connect->setText("CONNECT");
pb_connect->setEnabled(true);
pb_send=ui->pushButton_2;
pb_send->setText("Send");
pb_send->setEnabled(false);
pb_send1=ui->pushButton_3;
pb_send1->setText("SEND");
pb_send1->setEnabled(false);
pb_disconnect=ui->pushButton_4;
pb_disconnect->setEnabled(false);
pb_disconnect->setText("DISCONNECT");
connect(pb_connect,SIGNAL(clicked()),this,SLOT(connecttoserver()));
connect(pb_send,SIGNAL(clicked()),this,SLOT(write()));
connect(pb_send1,SIGNAL(clicked()),this,SLOT(write1()));
connect(pb_disconnect,SIGNAL(clicked()),this,SLOT(DisconnectServer()));
connect(socket,SIGNAL(connected()),this,SLOT(on_connected()));
connect(socket,SIGNAL(readyRead()),this,SLOT(read1()));
}
TCPClient::~TCPClient()
{
delete ui;
}
void TCPClient
::changeEvent(QEvent *e
) {
switch (e->type()) {
ui->retranslateUi(this);
break;
default:
break;
}
}
void TCPClient::connecttoserver()
{
hAddr.setAddress("192.168.64.55");
socket->connectToHost(hAddr,1234);
}
void TCPClient::on_connected()
{
pb_connect->setEnabled(false);
pb_send->setEnabled(true);
pb_send1->setEnabled(true);
qDebug("Connection established");
pb_disconnect->setEnabled(true);
le->setEnabled(true);
le1->setEnabled(true);
te->setEnabled(true);
te1->setEnabled(true);
}
void TCPClient::write()
{
QString Data
="",Data1
=this
->le
->text
();
CHAT="1";
Data.append(CHAT);
Data.append(Data1);
socket->write(Data.toUtf8().constData());
qDebug("Client Sent 1 to Server");
}
void TCPClient::write1()
{
QString Data
="",Data1
=this
->le
->text
();
CHAT="2";
Data.append(CHAT);
Data.append(Data1);
socket->write(Data.toUtf8().constData());
qDebug("Client Sent 2 to server");
// socket->flush();
}
TCPClient::TCPClient(QWidget *parent) :
QWidget(parent),
ui(new Ui::TCPClient)
{
ui->setupUi(this);
le= new QLineEdit;
le=ui->lineEdit;
le->setDisabled(true);
le1=new QLineEdit;
le1=ui->lineEdit_2;
le1->setDisabled(true);
te=new QTextEdit;
te=ui->textEdit;
te->setDisabled(true);
te1=new QTextEdit;
te1=ui->textEdit_2;
te1->setDisabled(true);
pb_connect=new QPushButton;
pb_connect=ui->pushButton;
pb_connect->setText("CONNECT");
pb_connect->setEnabled(true);
pb_send=new QPushButton;
pb_send=ui->pushButton_2;
pb_send->setText("Send");
pb_send->setEnabled(false);
pb_send1=new QPushButton;
pb_send1=ui->pushButton_3;
pb_send1->setText("SEND");
pb_send1->setEnabled(false);
pb_disconnect=new QPushButton;
pb_disconnect=ui->pushButton_4;
pb_disconnect->setEnabled(false);
pb_disconnect->setText("DISCONNECT");
socket=new QTcpSocket(this);
connect(pb_connect,SIGNAL(clicked()),this,SLOT(connecttoserver()));
connect(pb_send,SIGNAL(clicked()),this,SLOT(write()));
connect(pb_send1,SIGNAL(clicked()),this,SLOT(write1()));
connect(pb_disconnect,SIGNAL(clicked()),this,SLOT(DisconnectServer()));
connect(socket,SIGNAL(connected()),this,SLOT(on_connected()));
connect(socket,SIGNAL(readyRead()),this,SLOT(read1()));
}
TCPClient::~TCPClient()
{
delete ui;
}
void TCPClient::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void TCPClient::connecttoserver()
{
QHostAddress hAddr;
hAddr.setAddress("192.168.64.55");
socket->connectToHost(hAddr,1234);
}
void TCPClient::on_connected()
{
pb_connect->setEnabled(false);
pb_send->setEnabled(true);
pb_send1->setEnabled(true);
qDebug("Connection established");
pb_disconnect->setEnabled(true);
le->setEnabled(true);
le1->setEnabled(true);
te->setEnabled(true);
te1->setEnabled(true);
}
void TCPClient::write()
{
QString Data="",Data1=this->le->text();
CHAT="1";
Data.append(CHAT);
Data.append(Data1);
socket->write(Data.toUtf8().constData());
qDebug("Client Sent 1 to Server");
}
void TCPClient::write1()
{
QString Data="",Data1=this->le->text();
CHAT="2";
Data.append(CHAT);
Data.append(Data1);
socket->write(Data.toUtf8().constData());
qDebug("Client Sent 2 to server");
// socket->flush();
}
To copy to clipboard, switch view to plain text mode
Bookmarks