fastcgi 二进制图片数据返回 不完整,被截断

大大美女女 发布于 2013/08/30 11:24
阅读 1K+
收藏 0
#include <stdio.h>
#include <string>
#include <map>
#include <iostream>
#include <fstream>
#include <sys/types.h>  
#include <sys/stat.h> 
#include <fcgi_stdio.h>
using namespace std;

int main( int argc, char *argv[] )
{
  FCGX_Request request;
  FCGX_Init();
  int sock_fd = FCGX_OpenSocket("10.11.18.144:8089",100);
  FCGX_InitRequest(&request, sock_fd, 0);
  while (FCGX_Accept_r(&request) >= 0) {
      FILE *fp;
      fp = fopen("test.jpg", "rb");
      struct stat info;
      stat("test.jpg",&info);
      int size = info.st_size; 
      cout<< "hehe:" << size << endl;
      char buf[size];
      memset(buf, 0, size );
      int st = fread(buf,1,size, fp);
      fclose(fp);
      /*FILE* fp2 = fopen("test7.jpg", "ab");
      st = fwrite(buf,1,size, fp2);
      fclose(fp2);*/
      /*for (int i = 0; i < size; i++) {
        cout<< "ls:"<< buf[i]<< endl;
      }*/
      FCGX_Stream* fcgi_out = request.out;
      FCGX_FPrintF(fcgi_out, "Content-type: image/jpg;Content-Transfer-Encoding: binary\r\n\r\n"); 
      //string str = "111111\09999";
      //FCGX_FPrintF(fcgi_out, "%s\n", str.c_str()); 
      FCGX_FPrintF(fcgi_out, "%s\n", buf); 
      //FCGX_PutS("111111\09999", fcgi_out);
      FCGX_Finish_r(&request);
  }
}

我想做的事情是:nginx + 后台服务,返回图片数据,写了一个demo。

代码贴出的是后台的服务代码demo

在最后数据输出的时候:

FCGX_FPrintF(fcgi_out, "%s\n", buf);
只把前几个字节返回了。我猜测被'\0'字符截取了,不知道有什么处理方法,谁有这方面经验?


加载中
0
东软ADSG
东软ADSG

如果是\0的原因,不用字符串格式打印可以避免吧

0
dancingbear
dancingbear
使用FCGX_PutStr输出二进制数据
返回顶部
顶部