// Server/Client-related connections
connect(&tcpServer, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
connect(&tcpServer, SIGNAL(connectionAccepted()), this, SLOT(sendSpeed()));
connect(&tcpClient, SIGNAL(connected()), this, SLOT(readSpeed()));
// Set up layout
layout->addWidget(connectButton, 0, 0, 1, 2);
layout->addWidget(slider, 1, 0, 1, 2);
layout->addWidget(label, 3, 0);
//layout->addWidget(label2, 3, 0);
layout->addWidget(ipLabel, 5, 0);
win->setLayout(layout);
} // MainWindow()
////////////////////////////////////////////////////////////////////////
// SERVER FUNCTIONS //
// startServer(QString ip, quint16 port) - Starts the server
////////////////////////////////////////////////////////////////////////
void MainWindow
::startServer(QString ip, quint16 port
) {
statusBar->showMessage("Server created", 0);
{
statusBar->showMessage(tr("Unable to start the server: %1.", 0).arg(tcpServer.errorString()));
return;
}
else
{
statusBar->showMessage(tr("Listening on port %1...", 0).arg(tcpServer.serverPort()));
}
}
// acceptConnection() - Completes the connection to the client
////////////////////////////////////////////////////////////////////////
void MainWindow::acceptConnection()
{
slider->setAmpValue(10);
slider->setFreqValue(10);
clientConnection = tcpServer.nextPendingConnection();
statusBar->showMessage("Connection established.", 0);
emit connectionAccepted();
}
// sendSpeed() - Sends the slider speed to the client
////////////////////////////////////////////////////////////////////////
void MainWindow::sendSpeed()
{
value = slider->ampValue();
// Write the slider value to the output buffer
out << value;
// Send the linear slider value to the client
clientConnection->write(block);
}
// SERVER FUNCTIONS
////////////////////////////////////////////////////////////////////////
// CLIENT FUNCTIONS //
// getServer() - Establishes a connection to the server
////////////////////////////////////////////////////////////////////////
void MainWindow
::getServer(QString ip, quint16 port
) {
blockSize = 0;
}
// readSpeed() - Reads the slider speed value into a label
////////////////////////////////////////////////////////////////////////
void MainWindow::readSpeed()
{
quint8 sliderValue;
in >> sliderValue;
label->setNum(sliderValue);
}
// Server/Client-related connections
connect(&tcpServer, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
connect(&tcpServer, SIGNAL(connectionAccepted()), this, SLOT(sendSpeed()));
connect(&tcpClient, SIGNAL(connected()), this, SLOT(readSpeed()));
connect(&tcpClient, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
connect(&tcpClient, SIGNAL(state(QAbstractSocket::SocketState)), this, SLOT(displayStatus(QAbstractSocket::SocketState)));
// Set up layout
layout->addWidget(connectButton, 0, 0, 1, 2);
layout->addWidget(slider, 1, 0, 1, 2);
layout->addWidget(label, 3, 0);
//layout->addWidget(label2, 3, 0);
layout->addWidget(ipLabel, 5, 0);
win->setLayout(layout);
} // MainWindow()
////////////////////////////////////////////////////////////////////////
// SERVER FUNCTIONS //
// startServer(QString ip, quint16 port) - Starts the server
////////////////////////////////////////////////////////////////////////
void MainWindow::startServer(QString ip, quint16 port)
{
statusBar->showMessage("Server created", 0);
if (!tcpServer.listen(QHostAddress(ip), port))
{
statusBar->showMessage(tr("Unable to start the server: %1.", 0).arg(tcpServer.errorString()));
return;
}
else
{
statusBar->showMessage(tr("Listening on port %1...", 0).arg(tcpServer.serverPort()));
}
}
// acceptConnection() - Completes the connection to the client
////////////////////////////////////////////////////////////////////////
void MainWindow::acceptConnection()
{
slider->setAmpValue(10);
slider->setFreqValue(10);
clientConnection = tcpServer.nextPendingConnection();
statusBar->showMessage("Connection established.", 0);
emit connectionAccepted();
}
// sendSpeed() - Sends the slider speed to the client
////////////////////////////////////////////////////////////////////////
void MainWindow::sendSpeed()
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
value = slider->ampValue();
// Write the slider value to the output buffer
out.setVersion(QDataStream::Qt_4_0);
out << value;
// Send the linear slider value to the client
clientConnection->write(block);
}
// SERVER FUNCTIONS
////////////////////////////////////////////////////////////////////////
// CLIENT FUNCTIONS //
// getServer() - Establishes a connection to the server
////////////////////////////////////////////////////////////////////////
void MainWindow::getServer(QString ip, quint16 port)
{
blockSize = 0;
tcpClient.connectToHost(QHostAddress(ip), port);
}
// readSpeed() - Reads the slider speed value into a label
////////////////////////////////////////////////////////////////////////
void MainWindow::readSpeed()
{
QDataStream in(&tcpClient);
in.setVersion(QDataStream::Qt_4_0);
quint8 sliderValue;
in >> sliderValue;
label->setNum(sliderValue);
}
To copy to clipboard, switch view to plain text mode
Bookmarks