Networking “如何使用函数”;“发送”;(Winsock)使用OpenCV在VS10中传输视频?

Networking “如何使用函数”;“发送”;(Winsock)使用OpenCV在VS10中传输视频?,networking,opencv,winsock2,Networking,Opencv,Winsock2,我使用VS10上的OpenCV从我的网络摄像头中传输视频。我还使用winsock2从服务器客户端TCP/IP(本地主机)在同一台计算机上传输消息和图像。现在我正在尝试实时流式传输视频。我不知道使用函数“send”是否可以指定每个帧的长度,因为我认为它也可以发送一个常量字符。有人知道我可以用什么函数来解决我的问题吗 源代码: Server.cpp #include "Server.h" using namespace std; //Our main function //void main()

我使用VS10上的OpenCV从我的网络摄像头中传输视频。我还使用winsock2从服务器客户端TCP/IP(本地主机)在同一台计算机上传输消息和图像。现在我正在尝试实时流式传输视频。我不知道使用函数“send”是否可以指定每个帧的长度,因为我认为它也可以发送一个常量字符。有人知道我可以用什么函数来解决我的问题吗

源代码:

Server.cpp

#include "Server.h"

using namespace std;

//Our main function
//void main()
int _tmain(int argc, _TCHAR* argv[])
{
//We have to start de Winsock-dll
long answer;
WSAData wsaData;
WORD DLLVERSION;
DLLVERSION = MAKEWORD (2,1);
//Stard the dll
answer = WSAStartup(DLLVERSION, &wsaData);

//The dll was started, now I use the winsock funcitions in the program

//Structure for the sockets
SOCKADDR_IN addr;
//addr = our structure
int addrlen = sizeof(addr);

//Now we have to create our sockets
//sListen = socket which is listening for incoming connection
SOCKET sListen;
//sConnect = socket which is operating if a connection was found!
SOCKET sConnect;

//Now we have to setup our sockets:
//AF_INET = means that our socket belongs to the internet family
//SOCK_STREAM = means that our socket is a connection-orientated socket
sConnect = socket(AF_INET, SOCK_STREAM, NULL);

//Now we have to setup our structure
//inet_addr = IP of our socket (I use 127.0.0.1...this is the loopback adress)
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
//Retype the family
addr.sin_family = AF_INET;
//Now we have to setup the port in the structure;
//Now our server has the IP: 127.0.0.1 and the port 1234
addr.sin_port = htons (1234);

//Now we have to setup our sListen socket
sListen = socket(AF_INET, SOCK_STREAM, NULL);
//Now we blind the socket
//The socket becomes the structure addr
bind(sListen, (SOCKADDR*)&addr, sizeof(addr));
//We have to say that the socket is listening for an incoming connection
listen(sListen, SOMAXCONN);
//SOMAXCON means that the server is listenning without any limit

//We have to start the capture of the frames to send it
//Well first of all initialize the frames and the default camera
//Using OpenCV
CvCapture *capture = 0;
    IplImage  *frame = 0;
    int key = 0;
    capture = cvCaptureFromCAM(0);


//At last we have to say what the serve sould do if a connection was found!
//For this we create a endless loop:
for(;;)
{
    cout << "Waiting for an incoming connection" <<endl;

    //if a connection was found << "..." <<endl
    if(sConnect = accept(sListen, (SOCKADDR*)&addr, &addrlen))
    {
        cout << "A connection was found" <<endl;

        //Second part in reference to Youtube 2
        //1r: The server should send a message with the funcion send();
        //The number of the letters +1
        //Ex: Number of the letters: 14+1=15
        answer = send(sConnect, "Bonjour",8, NULL);

        //*************************Third part of the project
        //Sending an image captured from the cam
        //always check
        if ( !capture ) 
        {
            fprintf( stderr, "Cannot open initialize webcam!\n" );
            return 1;
        }
        //create a window for the video
        cvNamedWindow( "server", CV_WINDOW_AUTOSIZE );

        while( key != 'q' ) 
        {
            //get a frame
            frame = cvQueryFrame(capture);
            //always check
            if( !frame ) break;
            //display current frame
            cvShowImage( "server", frame );
            //exit if user press 'q'
            key = cvWaitKey(1);
        }

        char StartStream(IplImage *frame);
        int streamSize(IplImage *frame);
        if(frame!=0)
        {
            return sizeof(*frame->imageData);
        }
        char* buffer = new char[10000];
        int len = 10000;
        //int sent = 0;
        for(int i=0; i<len; i+= answer)
        {
            answer = send(sConnect, buffer+i, len-i, 0);
        }
    }
    //free memory
    cvDestroyWindow( "server" );
    cvReleaseCapture( &capture );
}
return 0;
}
#包括“Server.h”
使用名称空间std;
//我们的主要职能
//void main()
int _tmain(int argc,_TCHAR*argv[]
{
//我们必须开始反Winsock dll
长话短说;
WSAData WSAData;
词汇转换;
DLLVERSION=MAKEWORD(2,1);
//标准化dll
答案=WSAStartup(DLLVERSION和wsaData);
//dll已经启动,现在我在程序中使用winsock函数
//插座的结构
地址中的SOCKADDR_;
//addr=我们的结构
int addrlen=sizeof(addr);
//现在我们必须创建套接字
//sListen=正在侦听传入连接的套接字
套筒滑脱;
//sConnect=在找到连接时正在运行的套接字!
插座连接;
//现在我们必须设置套接字:
//AF_INET=表示我们的套接字属于互联网家族
//SOCK_STREAM=表示我们的套接字是面向连接的套接字
sConnect=socket(AF\u INET,SOCK\u STREAM,NULL);
//现在我们必须建立我们的结构
//inet_addr=socket的IP(我使用127.0.0.1…这是环回地址)
addr.sin_addr.s_addr=inet_addr(“127.0.0.1”);
//重新键入家庭成员
addr.sin_family=AF_INET;
//现在我们必须在结构中设置端口;
//现在我们的服务器拥有IP:127.0.0.1和端口1234
地址:sin_端口=htons(1234);
//现在我们必须设置sListen套接字
sListen=socket(AF\u INET,SOCK\u STREAM,NULL);
//现在我们把插座堵住了
//套接字成为结构addr
绑定(sListen,(SOCKADDR*)和addr,sizeof(addr));
//我们必须说,套接字正在侦听传入的连接
听(斯莱斯滕,索马克斯康);
//SOMAXCON表示服务器正在无限制地侦听
//我们必须开始捕获帧才能发送它
//首先初始化帧和默认相机
//使用OpenCV
CvCapture*capture=0;
IplImage*frame=0;
int键=0;
捕获=cvCaptureFromCAM(0);
//最后我们要说的是,如果发现连接,发球应该怎么做!
//为此,我们创建了一个无止境的循环:
对于(;;)
{

cout你可以试试和。

为什么你需要使用TCP?也许更好的解决方案是UDP和sendto功能。因为在那之后,我需要接收器和发射器之间的通信,UDP是单向协议。你说的单向是什么意思?从这个意义上说,TCP是“单向的”正如UDP……与UDP一样,客户端不向发射器发送确认。但是如果它不相关,我将接受新的解决方案。考虑写一些关于这个为什么会工作的细节,有时链接会死掉。
#include "Client.h"

using namespace std;

//void main()
int _tmain(int argc, _TCHAR* argv[])
{
string confirm;
char message[200];
string strmessage;
//IplImage *pImg;

//Start the functions of the Winsock-DLL
long answer;
WSAData wsaData;
WORD DLLVersion;
DLLVersion = MAKEWORD(2,1);
answer = WSAStartup(DLLVersion, &wsaData);

//Setup our Sockets and SOCKADDR_IN
SOCKADDR_IN addr;
//we will need addrlen later maybe!
int addrlen = sizeof(addr);

//Now setup our Socket
SOCKET sConnect;
sConnect = socket(AF_INET,      SOCK_STREAM, NULL);

//IP of our server
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
//Now we have to retype de family
addr.sin_family = AF_INET;
//Port of our server
addr.sin_port = htons(1234);

//Now we will ask the client if he wants to connect
cout << "Do you want to connect to your server? [Y/N]" <<endl;
cin >> confirm;

//If the user don't want to connect the client will close
if (confirm == "N")
{
    exit(1);
}
else
{
    if (confirm == "Y")
    {
        //function connect
        connect(sConnect, (SOCKADDR*)&addr, sizeof(addr));

        //Our client is working

    //Now we program the client that he should recv a message from the server
        //Our client should receive the message with the function recv();
        //With new char "message"
        answer = recv(sConnect, message, sizeof(message), NULL);
        //The client should display the message
        //With new string strmessage
        strmessage = message;
        cout << strmessage <<endl;

        //the client also should display the image recived
        char* buffer = new char[10000];
        int len = 10000;
        //int rec = 0;
        int i=0;
        for(int i=0; i<len; i+= answer)
        {
            while (answer = recv(sConnect, buffer+i, len-i, 0))
            {
            IplImage *frame = cvCreateImage(cvSize(640,480), 8, 3);
            frame->imageData = (char *)buffer;
            cvNamedWindow( "client", CV_WINDOW_AUTOSIZE );
            cvShowImage( "client", frame );
            }
            return 0;
        }
        getchar();
    }
}
getchar();
}