int main() { // The string to send to the server. const char *strToSend="Hello Server"; // The buffer in which to recieve the hello from the server char buff[100]; // The size of the string the server sent size_t strSize; // The socket object ArSocket sock; // Initialize Aria. It is especially important to do // this on Windows, because it will initialize Window's // sockets system. Aria::init(); // Connect to the server ArLog::log(ArLog::Normal, "socketClientExample: Connecting to localhost TCP port 7777..."); if (sock.connect("localhost", 7777, ArSocket::TCP)) ArLog::log(ArLog::Normal, "socketClientExample: Connected to server at localhost TCP port 7777."); else { ArLog::log(ArLog::Terse, "socketClientExample: Error connecting to server at localhost TCP port 7777: %s", sock.getErrorStr().c_str()); return(-1); } // Read data from the socket. read() will block until // data is received. strSize=sock.read(buff, sizeof(buff)); // If the amount read is 0 or less, its an error condition. if (strSize > 0) { buff[strSize]='\0'; // Terminate the string with a NULL character: ArLog::log(ArLog::Normal, "socketClientExample: Server said: \"%s\"", buff); } else { ArLog::log(ArLog::Terse, "socketClientExample: Error in waiting/reading from the server."); Aria::exit(-1); return(-1); } // Send the string 'Hello Server' to the server. write() should // return the same number of bytes that we told it to write. Otherwise, // its an error condition. if (sock.write(strToSend, strlen(strToSend)) == strlen(strToSend)) ArLog::log(ArLog::Normal, "socketClientExample: Said hello to the server."); else { ArLog::log(ArLog::Terse, "socketClientExample: Error sending hello string to the server."); Aria::exit(-1); return(-1); } // Now close the connection to the server sock.close(); ArLog::log(ArLog::Normal, "socketClientExample: Socket to server closed."); // Uninitialize Aria and exit Aria::exit(0); return(0); }
void* runThread(void*) { size_t strSize; // The socket objects: one for accepting new client connections, // and another for communicating with a client after it connects. // Open the server socket if (serverSock.open(VIDEO_PORT, ArSocket::TCP)) ArLog::log(ArLog::Normal, " Opened the server port %d.", VIDEO_PORT); else ArLog::log(ArLog::Normal, " Failed to open the server port: %s.", serverSock.getErrorStr().c_str()); if (serverSock.accept(&clientSock)) ArLog::log(ArLog::Normal, " Client has connected."); else ArLog::log(ArLog::Terse, " Error in accepting a connection from the client: %s.", serverSock.getErrorStr().c_str()); //.setCloseCallback(new ArGlobalFunctor1<ArNetPacket*>(&clientCloseCallback)); //serverSock.setCloseCallback(new ArGlobalFunctor1<ArNetPacket*>(&clientCloseCallback)); while(1) { //int clinetSocketStatus = serverSock.accept(&clientSock); // MODIFIED By Yang, Try to accept new client. // //if (clinetSocketStatus > 0){ // char buf[1024] = {0}; // int recievedInt = clientSock.read(buf, 1024, 0); // cout << recievedInt << endl; // while(true) // { /* send the grayscaled frame, thread safe */ mutex_video.lock(); if (is_data_ready) { int grayImgSize=gray_img.rows*gray_img.cols; //cout <<" Sending image to the client.grayImgSize= " << grayImgSize; if ((strSize = clientSock.write(gray_img.data, grayImgSize))==grayImgSize ) ;//ArLog::log(ArLog::Normal, " Sent image to the client.grayImgSize= %d", grayImgSize); // else // ArLog::log(ArLog::Normal, " Error in sending hello string to the client."); //ArLog::log(ArLog::Normal, " String Size: \"%d\"", strSize); is_data_ready = 0; } mutex_video.unlock(); //if(!clientSock.isOpen()) //{ // clientSock.close(); // ArLog::log(ArLog::Normal, " Socket to client closed."); // if (serverSock.accept(&clientSock)) // ArLog::log(ArLog::Normal, " Client has connected."); // else // ArLog::log(ArLog::Terse, " Error in accepting a connection from the client: %s.", serverSock.getErrorStr().c_str()); //} } //} //} // Now lets close the connection to the client clientSock.close(); ArLog::log(ArLog::Normal, " Socket to client closed."); // And lets close the server port //serverSock.close(); //ArLog::log(ArLog::Normal, " Server socket closed."); // Uninitialize Aria and exit the program //Aria::exit(0); } //end of runThread
void* RobotVideo::runThread(void* arg) { cvNamedWindow("Robot", CV_WINDOW_AUTOSIZE); moveWindow("Robot", 0, 0); // The buffer in which to receive the hello from the server //unsigned char buff[12]; unsigned char imgBuff[480*640]; //Mat imgRecieve(480,640,CV_8UC1); // The size of the string the server sent size_t strSize; // The socket object ArSocket sock; unsigned char *ptr; // Initialize Aria. It is especially important to do // this on Windows, because it will initialize Window's // sockets system. // Aria::init(); // Connect to the server //ArLog::log(ArLog::Normal, "socketClientExample: Connecting to localhost TCP port 7777..."); if (sock.connect(server_ip, ROBOT_VIDEO_PORT, ArSocket::TCP)) ArLog::log(ArLog::Normal, "Connected to server at %s TCP port 11000.", server_ip); else { ArLog::log(ArLog::Terse, "Error connecting to server at %s TCP port 11000: %s",server_ip, sock.getErrorStr().c_str()); //return(-1); } //---------------------------VideoWriter--------------------------------------- robotVideo.open("robot.avi" , CV_FOURCC('M','J','P','G') /* CV_FOURCC('P','I','M','1') */, 20/*inputVideo.get(CV_CAP_PROP_FPS)*/,Size(640, 480), false); if (!robotVideo.isOpened()) { cout << "Could not open the output video for write: " /*<< source*/ << endl; } while(1) { ptr = &imgBuff[0]; strSize = 0; while (strSize < 640 * 480) { //ArLog::log(ArLog::Normal, "socketClientExample: String Size: \"%d\"", strSize); strSize += sock.read(ptr, 2); ptr += 2; } ptr=robot_img.datastart; //ArLog::log(ArLog::Normal, "socketClientExample: String Size: \"%d\"", strSize); mutex_robotVideo.lock(); for (int i = 0,k = 0; i < robot_img.rows*robot_img.cols; i++) { *(ptr++) = imgBuff[k++]; } imshow("Robot", robot_img); robotVideo << robot_img; mutex_robotVideo.unlock(); waitKey(1);/*0)==27) break;*/ } sock.close(); //outputVideo.release(); // Now close the connection to the server ArLog::log(ArLog::Normal, "Socket to server closed."); destroyWindow("Robot"); // Uninitialize Aria and exit //Aria::exit(0); return NULL; }