我正在尝试将图像文件从客户端电脑发送到连接到以太网端口的服务器电脑。我已将两台电脑上的 IP 手动设置为 192.168.1.2(服务器)和 192.168.1.3(客户端),子网掩码设置为 255.255.255.0。我能够连接客户端和服务器并从客户端发送图像数据,但服务器未接收图像数据。我的目标是在服务器 GUI 上显示从客户端发送的图像。
提前致谢!
服务器代码:
服务器.cpp:
#include "server.h"
Server::Server(QObject *parent) : QTcpServer(parent),imageSize(0)
{
listen(QHostAddress::Any, 1234); // Listen on port 1234
}
void Server::incomingConnection(qintptr socketDescriptor)
{
QTcpSocket *socket = new QTcpSocket(this);
socket->setSocketDescriptor(socketDescriptor);
emit clientConnected(socket->peerAddress().toString());
connect(socket, &QTcpSocket::readyRead, this, &Server::readyRead);
}
void Server::readyRead()
{
QTcpSocket* socket = qobject_cast<QTcpSocket*>(sender());
if (!socket)
{
return;
}
socket->read(reinterpret_cast<char*>(&imageSize), sizeof(qint64));
imageData.append(socket->read(imageSize));
QImage image;
image.loadFromData(imageData, "JPG");
emit imageReceived(image);
imageData.clear();
imageSize = 0;
}
main.cpp:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLabel label;
label.show();
label.setFixedSize(800, 600);
Server server;
QObject::connect(&server, &Server::imageReceived, [&](const QImage &image){
label.setPixmap(QPixmap::fromImage(image));
});
QObject::connect(&server, &Server::clientConnected, [&](const QString &clientAddress){
qDebug() << "Client connected:" << clientAddress;
});
return a.exec();
}
客户代码:
客户端.cpp:
#include "client.h"
#include <QDebug>
Client::Client(QObject *parent) : QObject(parent)
{
socket = new QTcpSocket(this);
connect(socket, &QTcpSocket::connected, this, &Client::connected);
connect(socket, &QTcpSocket::bytesWritten, this, &Client::bytesWritten);
connect(socket, &QTcpSocket::disconnected, this, &Client::disconnected);
}
void Client::sendImage(const QString& imagePath)
{
file.setFileName(imagePath);
if (!file.open(QIODevice::ReadOnly)) {
qDebug() << "Could not open file"<< file.errorString();
return;
}
socket->connectToHost("192.168.1.2", 1234); // Server IP and port
// Close the connection when all data has been written
connect(socket, &QTcpSocket::bytesWritten, this, &Client::checkBytesWritten);
connect(socket, &QTcpSocket::disconnected, socket, &QTcpSocket::deleteLater);
// file.close();
}
void Client::connected()
{
qDebug() << "Connected to server";
// Get the size of the file
qint64 imageSize = file.size();
// Start sending image data
QByteArray imageData = file.readAll();
socket->write(imageData);
file.close();
}
void Client::bytesWritten(qint64 bytes)
{
qDebug() << "Bytes written: " << bytes;
}
void Client::disconnected()
{
qDebug() << "Disconnected from server";
}
void Client::error(QAbstractSocket::SocketError error)
{
qDebug() << "Error: " << error;
}
void Client::readyRead()
{
qDebug() << "Received acknowledgment from server: " << socket->readAll();
// Close the connection after receiving acknowledgment
socket->close();
}
void Client::checkBytesWritten(qint64 bytes)
{
if (bytes == 0) {
// All data has been written, close the connection
socket->disconnectFromHost();
}
}
main.cpp:
#include "client.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Client client;
QString imagePath = "C:/Users/Intel/Downloads/client/moon.jpg";
// Send the image file to the server
client.sendImage(imagePath);
return a.exec();
}
在接收端,您读取图像尺寸,然后读取图像本身,但在发送端,您仅发送图像。你应该做这样的事情:
QByteArray imageData = file.readAll();
qint64 imageSize = imageData.size();
// Start sending image size
socket->write(&imageSize,sizeof(qint64));
// Start sending image data
socket->write(imageData);