2013-09-22 51 views
0

我已經完成了簡單的tcp客戶端/服務器程序,它可以很好地處理字符串和字符數據...我想把每幀(從網絡攝像頭)發送到服務器..這裏是客戶端程序的一部分,其中發生錯誤:用tcp使用opencv和socket進行流式傳輸

line:66 if(send(sock, frame, sizeof(frame), 0)< 0) 

錯誤:

client.cpp:66:39: error: cannot convert ‘cv::Mat’ to ‘const void*’ for argument ‘2’ to ‘ssize_t send(int, const void*, size_t, int)

我不能承認這個錯誤....好心幫...以下完整的客戶端程序:

#include<stdio.h> 
#include<sys/types.h> 
#include<sys/socket.h> 
#include<netinet/in.h> 
#include<string.h> 
#include<stdlib.h> 
#include<netdb.h> 
#include<unistd.h> 
#include "opencv2/objdetect.hpp" 
#include "opencv2/highgui.hpp" 
#include "opencv2/imgproc.hpp" 
#include <iostream> 

using namespace std; 
using namespace cv; 


int main(int argc,char *argv[]) 
{ 
    int sock; 
struct sockaddr_in server; 
struct hostent *hp; 
char buff[1024]; 
VideoCapture capture; 
    Mat frame; 
capture.open(1); 
    if (! capture.isOpened()) { printf("--(!)Error opening video capture\n"); return -1; } 

begin: 
capture.read(frame); 

if(frame.empty()) 
    { 
     printf(" --(!) No captured frame -- Break!"); 
     goto end; 
    } 

sock=socket(AF_INET,SOCK_STREAM,0); 
if(sock<0) 
{ 
    perror("socket failed"); 
    exit(1); 
} 

server.sin_family =AF_INET; 

hp= gethostbyname(argv[1]); 
if(hp == 0) 
{ 
    perror("get hostname failed"); 
    close(sock); 
    exit(1); 
} 

memcpy(&server.sin_addr,hp->h_addr,hp->h_length); 
server.sin_port = htons(5000); 

if(connect(sock,(struct sockaddr *) &server, sizeof(server))<0) 
{ 
    perror("connect failed"); 
    close(sock); 
    exit(1); 
} 
int c = waitKey(30); 
    if((char)c == 27) { goto end; } 
if(send(sock, frame, sizeof(frame), 0)< 0) 
{ 
    perror("send failed"); 
    close(sock); 
    exit(1); 
} 
goto begin; 
end: 
printf("sent\n",); 
close(sock); 

    return 0; 
    } 

回答

1

因爲TCP提供了一個字節流,所以在你可以通過TCP套接字發送某些東西之前,你必須編寫你想發送的確切字節。您使用sizeof不正確。 sizeof函數告訴你係統需要多少字節來存儲特定的類型。這與數據將通過TCP連接需要的字節數沒有任何關係,這取決於您正在實現的TCP頂層的特定協議,它必須指定在字節級別如何發送數據。

0
  • 像大衛已經說過,你的長度錯了。的sizeof()將不會幫助,你想要什麼可能是

    frame.total()* frame.channels()

  • 你不能發送墊目標,但你可以發送像素(數據指針),因此這將是:

    發送(襪子,frame.data,frame.total()* frame.channels(),0)

    但仍然是一個壞主意。通過網絡發送未壓縮的像素? bahh。

    請看imencode/imdecode

  • 我敢肯定,你有反向的客戶機/服務器角色在這裏。 通常服務器保存要檢索的信息(網絡攝像機),並且客戶端連接到該 並請求圖像。

+0

那麼如何將圖像轉換爲灰色......並將像素值檢索爲單個整數數組,然後使用上述程序發送它? –

+0

仍然是,網絡攝像頭應該進入你的服務器,而不是客戶端。 (您希望多個客戶端能夠觀看相同的流,不是嗎?) – berak