我做了一个简单的tcp客户机/服务器程序,可以很好地处理字符串和字符数据.我想把每个帧(从一个网络摄像头)拿出来,然后发送给服务器。以下是发生错误的客户端程序的部分:
line:66 if(send(sock, frame, sizeof(frame), 0)< 0)错误:
client.cpp:66:39: error:无法将‘cv::Mat’转换为‘const *’,用于参数‘2’到‘ssize_t发送’(int,const*,size_t,int)。
在完整的客户端程序中,我认不出这个error....kindly help...the:
#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;
}发布于 2013-09-22 05:50:00
因为TCP提供了一个字节流,所以在通过TCP套接字发送某些内容之前,必须组成要发送的确切字节。使用sizeof是不正确的。sizeof函数告诉您系统需要多少字节来存储特定类型。这与数据在TCP连接上所需的字节数没有任何关系,TCP连接依赖于您正在实现的TCP之上的特定协议,该协议必须指定如何在字节级别发送数据。
发布于 2013-09-22 08:31:19
https://stackoverflow.com/questions/18940752
复制相似问题