服务器发送的图像不显示

时间:2014-02-15 09:56:17

标签: php c++ sockets

我使用C ++作为服务器,php作为客户端,通过套接字从服务器发送图像回客户端。我在客户端收到图像的主要问题,但它完全是白色的。在HTML中有没有任何图片的图片1920 * 1080。你能建议我在哪里找错吗?

服务器部分图片加载:

iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
    printf("listen failed with error: %d\n", WSAGetLastError());
    closesocket(ListenSocket);
    WSACleanup();
    return;
}

// Accept a client socket
ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
    printf("accept failed with error: %d\n", WSAGetLastError());
    closesocket(ListenSocket);
    WSACleanup();
    return;
}

// No longer need server socket
closesocket(ListenSocket);

// Receive until the peer shuts down the connection
do {

    iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
    if (iResult > 0) {
        printf("Bytes received: %d\n", iResult);



     /*send file*/
        int fd;
        int sockfd;
        ssize_t nbytes;
        char buf[LEN];
        memset(buf,0, LEN+1);
        int len;

        FILE *in = fopen("desktop.png","rb");
        while ((iResult = fread(buf,sizeof(buf),1, in)) > 0) {
            send(ClientSocket,buf,sizeof(buf),0);
        }



        if (iSendResult == SOCKET_ERROR) {
            printf("send failed with error: %d\n", WSAGetLastError());
            closesocket(ClientSocket);
            WSACleanup();
            return;
        }
        printf("Bytes sent: %d\n", iSendResult);

    }
    else if (iResult == 0)
        printf("Connection closing...\n");
    else  {
        printf("recv failed with error: %d\n", WSAGetLastError());
        closesocket(ClientSocket);
        WSACleanup();
        return;
    }

} while (iResult > 0);

// shutdown the connection since we're done
//iResult = shutdown(ClientSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
    printf("shutdown failed with error: %d\n", WSAGetLastError());
    closesocket(ClientSocket);
    WSACleanup();
    return;
}

// cleanup
closesocket(ClientSocket);
WSACleanup();

所以我打包图片并发送给客户。

客户端

function connect($host)
{
    //$host    = "127.0.0.1";
    $port    = 27001;
    $message = " testing."
    echo "Message To server :".$message;
    // create socket
    $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
    // connect to server
    $result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");  
    // send string to server
    socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
    // get server response

    $buf;
    if (!socket_recv($socket, $buf, 4096, MSG_WAITALL)) {
                echo "Socket Receive Error...\n";
                $error_msg = 'Unable to receive data: ' . socket_strerror(socket_last_error());
            } else {
                echo 'BIND_TRANSMITTER Response: ' . var_dump($buf) . "\n"; 
    }

    $imgData = base64_encode($buf);
    $img = "<img src= 'data:image/png;base64, $imgData' />";
    print($img);

    socket_close($socket);
}

当我查看HTML时,我会看到下一个细节:

<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAB4AAAAQ4CAYAAADo08FDAAAgAElEQVR4nOy9edBlx1Un+DuZ97731aoq7SpJJcmyLWspy5LKsizbWLKRjTHGLHa4aXDgaKLHjYkYuiGgHTFBx4yBwTTQ000PZhimB3qgGxjongAM2NggwIu8YSzJ1m5rqdKuUi3fV/V9792beeaPc05m3vvu+7aqsmUgK756790ll5Mnz34y6dt/8iOMDZQwPYHp8lG0zQo4Drw6VBvNr4/nvMRcvkbpKe4/ynHVpjm9373LPPzC8GUebCD9ZIb8K+vm4jltnzxcPcZoYSd8vTW9IY9yp/qo74fJCTTLi4jtBGxj5fw0F++XdZVd5t73wQnn7nhiOQQuxla+otcJADN1bjLzTDvd8fbGbBUVbdAgLpXtkL7DM/eJSH86uGoMN94JV2/V5ynhFoHAlF8lALFZRpwugttJB7/6iNzvSnesuf6h+6ssiZkH1/3sOgv3vxt+cP6dcYWLZ7j3DHcqYgi8UY3hRjtB9ZaB8fRWNQHcnECcLIJDH97Fc+sqQ0DOfUz4xDaqmAgBcQAzS/v6l36DM8Fg7o2bO2NaT7H3Ka0RyldnCFz5Xnc8gMLbjeDqbUC10HmSiv9L0HC7Ap4eB8fpILzXMYCTK31w2e+TrJfm/ih+ll+IQLpCiUg=">

所以图片在页面上有它的宽度和高度,并且有一个带有该图片的矩形,但它不显示,只有白色背景。我不明白我的错误在哪里,你能告诉我什么吗?

*的 更新 * 我将我的服务器发送块更改为。

    while ((len = fread(buf,sizeof(buf),1, in)) > 0) {
        send(ClientSocket,buf,len,0);
    }

现在我认为客户端出现问题,导致服务器出现写入错误: recv错误失败,错误10053

0 个答案:

没有答案