You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

以太网传输图片:客户端发图至服务器GUI无接收问题排查

以太网传输图片至服务器GUI显示问题

我尝试通过以太网端口将图片文件从客户端PC发送至服务器PC,已手动设置两台PC的IP:服务器为192.168.1.2,客户端为192.168.1.3,子网掩码均为255.255.255.0。目前客户端可与服务器建立连接并发送图片数据,但服务器无法接收数据,目标是在服务器GUI上显示客户端发送的图片。

服务器代码

server.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();
}

客户端代码

client.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();
}

内容的提问来源于stack exchange,提问作者Aarom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 20:37:02