Blog Origin : The author of the whim, do collect 4 USB Camera Screen Small experiment, but encountered in the computer can only open at the same time at most 3 such headaches (personal analysis is the problem of the computer), so out of this way, in the client hanging 1, the server hangs 3 cameras, Using the socket for video transmission, this article is using the TCP protocol. The author's humble opinion, welcome to put forward criticism.
Description: Before this I have never contacted socket programming, also do not understand TCP/IP, UDP and other basic knowledge. In order to avoid making readers feel cumbersome, I will use primary and junior high school to study the total, connecting the writing skills to explain, trying to achieve detailed slightly, concise, easy to understand. If there is still a problem, you can directly reply to the blog or illustrations of the problem e-mail to the author (cuit_hjs@163.com), the author of limited knowledge, but as far as possible to reply.
Experimental Platform: VS2010 + opencv2.4.10 (other versions are the same way)
preparatory work:
1 , a computer, capable of running VS and OpenCV, used to run server (server) and client code;
2, a camera (notebook camera, USB camera, etc., Network Camera I have not touched, temporarily do not consider).
Experiment on the same computer, that is, run the server program, and run the client program, that is, through the socket programming to achieve self-collection of data, this step through the next run server and client separate experiment is simple. Video transmission between two PCs see the next section: "Two" OPENCV with socket for video transmission (TCP protocol)
Let's start with the first one, so we don't have to wait for the next song.
Experiment 1, simple data transmission, divided into three steps.
① Create a vs empty project called Server, then create a new Demo.cpp source file, copy the following code into Demo.cpp, and close vs after compiling.
#include "stdafx.h" #include <stdio.h> #include <winsock2.h> #pragma comment (lib, "Ws2_32.lib") int main (voi
d) {//initialize WSA WORD sockversion = Makeword (2,2);
Wsadata Wsadata;
if (WSAStartup (sockversion, &wsadata)!=0) {return 0;
}//Create socket Socket Slisten = socket (af_inet, sock_stream, ipproto_tcp);
if (Slisten = = Invalid_socket) {printf ("SOCKET error!");
return 0;
}//Bind IP and port sockaddr_in sin;
sin.sin_family = af_inet;
Sin.sin_port = htons (8888); Sin.sin_addr. S_un.
S_ADDR = Inaddr_any;
if (Bind (Slisten, (lpsockaddr) &sin, sizeof (sin)) = = Socket_error) {printf ("Bind ERROR!");
}//Start listening if (Listen (Slisten, 5) = = Socket_error) {printf ("Listen ERROR!");
return 0;
}//Loop receive data SOCKET sclient;
Sockaddr_in remoteaddr;
int naddrlen = sizeof (REMOTEADDR);
Char revdata[255];
while (true) {printf ("Wait for connection ... \ n");
Sclient = Accept (Slisten, (SOCKADDR *) &remoteaddr, &naddrlen); if (sclient = = Invalid_socket) {printf ("AccepT error! ");
Continue
} printf ("Accepted to a connection:%s \ r \ n", Inet_ntoa (REMOTEADDR.SIN_ADDR));
Receive data int ret = recv (sclient, Revdata, 255, 0);
if (Ret > 0) {Revdata[ret] = 0x00;
printf (revdata); }//Send data char * senddata = "Hello, tcp client."
\ n ";
Send (Sclient, SendData, strlen (SendData), 0);
Closesocket (sclient);
} closesocket (Slisten);
WSACleanup ();
return 0;
}
② Create a vs Empty project called client, then create a new Demo.cpp source file, copy the following code into Demo.cpp, and close vs after compiling.
#include "stdafx.h" #include <winsock2. H> #include <stdio.
h> #pragma comment (lib, "Ws2_32.lib") int main (int argc, char* argv[]) {WORD sockversion = Makeword (2,2);
Wsadata data;
if (WSAStartup (sockversion, &data)! = 0) {return 0;
} Socket sclient = socket (af_inet, sock_stream, ipproto_tcp);
if (sclient = = Invalid_socket) {printf ("INVALID SOCKET!");
return 0;
} sockaddr_in seraddr;
seraddr.sin_family = af_inet;
Seraddr.sin_port = htons (8888); Seraddr.sin_addr. S_un.
S_ADDR = inet_addr ("127.0.0.1");
if (Connect (sclient, (SOCKADDR *) &seraddr, sizeof (seraddr)) = = Socket_error) {printf ("Connect ERROR!");
Closesocket (sclient);
return 0;
} char * senddata = "Hello, tcp server, I am client!\n";
Send (Sclient, SendData, strlen (SendData), 0);
Char recdata[255];
int ret = recv (sclient, Recdata, 255, 0);
if (Ret > 0) {Recdata[ret] = 0x00;
printf (recdata);
} closesocket (sclient);
WSACleanup ();
return 0; }
③ Open both the server and client solutions. Run the server first, and then run the client, and you will find that the two have established communication and successfully transmitted the data to each other.
Here we have learned how to send and receive data using the socket, there are two functions send () and recv (). Note: 127.0.0.1 This IP address is the loopback address (spontaneous self-collection), each computer's loopback address is this IP, is fixed. Many times when we assemble the computer to the computer to plug the network card, you can ping this IP in the cmd window, if not the general description of the NIC is not plugged in.
Experiment 2, simple picture transmission, divided into three steps.
Next we add the OPENCV code on the basis of experiment 1, read the picture, for as simple as possible, we will grayscale the image. Once you've set up your OpenCV include and LIB paths and dependencies, you're ready to start tapping the code.
① First look at the client side, the idea is to put the read image data into an array, and then send () sent out, the code is as follows:
#include "stdafx.h" #include <winsock2. h> #include <iostream> #include <stdio.h> #include <cv.h> #include <opencv2/core/core.hpp> # Include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #pragma comment (lib, "ws2_
32.lib ") int main (int argc, char* argv[]) {WORD sockversion = Makeword (2,2);
Wsadata data;
if (WSAStartup (sockversion, &data)! = 0) {return 0;
} Socket sclient = socket (af_inet, sock_stream, ipproto_tcp);
if (sclient = = Invalid_socket) {printf ("INVALID SOCKET!\n");
return 0;
} sockaddr_in seraddr;
seraddr.sin_family = af_inet;
Seraddr.sin_port = htons (8888); Seraddr.sin_addr. S_un.
S_ADDR = inet_addr ("127.0.0.1");
if (Connect (sclient, (SOCKADDR *) &seraddr, sizeof (seraddr)) = = Socket_error) {printf ("Connect ERROR!\n");
Closesocket (sclient);
return 0; }//Read the image and send iplimage *image_src = Cvloadimage ("wall.jpg");//Load picture iplimage *IMAGE_DST = Cvcreateimage (Cvsize (640, 480), 8, 1);//place grayscale image int I, J;
Char senddata[1000000] = "";
Cvnamedwindow ("Client", 1);
Cvcvtcolor (IMAGE_SRC, IMAGE_DST, Cv_rgb2gray); for (i = 0; i < image_dst->height; i++) {for (j = 0; J < image_dst->width; J + +) {senddata[image_dst-&
Gt;width * i + j] = ((char *) (Image_dst->imagedata + i * image_dst->widthstep)) [j];
}} cvshowimage ("Client", IMAGE_DST);
Cvwaitkey (0);//any key sends send (Sclient, SendData, 1000000, 0);//Send Cvdestroywindow ("client");
Closesocket (sclient);
WSACleanup ();
return 0;
}
② server side, the idea is to put recv () received data into an array inside, To revert to the image data, the code is as follows:
#include "stdafx.h" #include <stdio.h> #include <winsock2.h> #include <cv.h> #include <opencv2/ core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #pragma
Comment (lib, "Ws2_32.lib") int main (int argc, char* argv[]) {//initialize WSA WORD sockversion = Makeword (2,2);
Wsadata Wsadata;
if (WSAStartup (sockversion, &wsadata)!=0) {return 0;
}//Create socket Socket Slisten = socket (af_inet, sock_stream, ipproto_tcp);
if (Slisten = = Invalid_socket) {printf ("SOCKET error!");
return 0;
}//Bind IP and port sockaddr_in sin;
sin.sin_family = af_inet; Sin.sin_port = htons (8888);//Port 8888 sin.sin_addr. S_un.
S_ADDR = Inaddr_any;
if (Bind (Slisten, (lpsockaddr) &sin, sizeof (sin)) = = Socket_error) {printf ("Bind ERROR!");
}//Start listening if (Listen (Slisten, 5) = = Socket_error) {printf ("Listen ERROR!");
return 0;
}//Loop receive data SOCKET sclient;
Sockaddr_in remoteaddr;
int naddrlen = sizeof (REMOTEADDR); printf ("etcPending connection ... \ n ");
do {sclient = Accept (Slisten, (SOCKADDR *) &remoteaddr, &naddrlen);
}while (sclient = = Invalid_socket);
printf ("Accepted to a connection:%s \ r \ n", Inet_ntoa (REMOTEADDR.SIN_ADDR));
Char revdata[1000000] = "";
Iplimage *image_src = Cvcreateimage (Cvsize (640, 480), 8, 1);//Receive grayscale image int I, J;
int ret;
Cvnamedwindow ("Server", 1);
while (true) {//Receive data RET = recv (sclient, Revdata, 1000000, 0);
if (Ret > 0) {Revdata[ret] = 0x00; for (i = 0; i < image_src->height; i++) {for (j = 0; J < image_src->width; J + +) {((char *) (IM
Age_src->imagedata + i * image_src->widthstep)) [j] = Revdata[image_src->width * i + j];
}} ret = 0;
} cvshowimage ("Server", IMAGE_SRC);
Cvwaitkey (1);
} cvdestroywindow ("Server");
Closesocket (Slisten);
WSACleanup ();
return 0;
}
③ Run server first, and then run client. On the client side, press any key to send the data, and the server side will immediately receive the data and revert to the image display.
After the above experiments and explanations, you can have a certain understanding of the use of sockets programming. It would be a blessing if you could inspire the reader to explore and develop something fun, but it's not going to go deep.
All of the files required for this experiment have been packaged and uploaded, and downloaded here: http://download.csdn.net/detail/hujingshuang/8400083
Special attention:
1. This blog routine is only for learning and communication, do not use for commercial purposes.
2, Welcome to Exchange, reproduced please indicate the source: http://blog.csdn.net/hujingshuang/article/details/43191461