⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dx_listener.cpp

📁 一个很全的matlab工具包
💻 CPP
字号:
/*** dxlinkclient.cpp -- a stream socket client demo*//**************************************************************************** *                                                                          * * The program connects to an already running server                        * *                                                                          *  * All the data sent to a socket must                                       * * be preceeded by an identifier:                                           * * -> STR_RIC to send rico_wall                                             * * -> STR_OFF to send offset_wall                                           * * -> STR_MSG to send the size of a buffer                                  * * -> STR_UPD to update the dx scene * -> STR_NAM to send the name of the experiment                            * *    the files will be saved in the directory data_visualisation/$NAME/    * * -> A serie is preceeded by the message STR_SER,                          * *    it is terminated by the message END_SER                               * * -> A file is preceeded by the message STR_FIL,                           * *    it is terminated by the message END_FIL                               * * -> The end of a transfer is indicated by the message END                 * *                                                                          * * If you want to add an identifier,                                        * * its length has to be equal to LEN                                        * * The identifiers have to be of the same length                            * ****************************************************************************/#include <iostream>#include <stdlib.h>#include <unistd.h>#include <errno.h>#include <string>#include <netdb.h>#include <netinet/in.h>#include <fcntl.h>#include <sys/types.h>#include <sys/socket.h>#include <dxl.h>#include <fstream>#include "dx_parser.h"using namespace std;#define DEFAULT_PORT 3490 // the port client will be connecting to #define MAXDATASIZE 100   // max number of bytes we can get at once #define LEN 7             // length of an identifiervoid init_connection(char * host, int * sockfd, 		     unsigned int port) {  struct hostent *he;  struct sockaddr_in their_addr; // connector's address information   if ((he=gethostbyname(host)) == NULL) // get the host info     {        cerr << "gethostbyname";      exit(1);    }  if ((*sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)     {      cerr << "socket";      exit(1);    }  their_addr.sin_family = AF_INET;    // host byte order   their_addr.sin_port = htons(port);  // short, network byte order   their_addr.sin_addr = *((struct in_addr *)he->h_addr);  memset(&(their_addr.sin_zero), '\0', 8);  // zero the rest of the struct   if (connect(*sockfd, (struct sockaddr *)&their_addr,	      sizeof(struct sockaddr)) == -1)     {      cerr << "connect: make sure the server is started\n";      exit(1);    }}int main(int argc, char *argv[]){  // For the parsing of command line variables  /* Set argument defaults */  struct arguments arguments;  arguments.port = DEFAULT_PORT;  arguments.file = "data_visualisation/dx_vis_noseries.net";  arguments.host = NULL;  /* Where the magic happens */  int ret = argp_parse (&argp, argc, argv, 0, 0, &arguments);  if (ret != 0)     {      cerr << "Parsing error" << endl;      return ret;    }  // quit if host is not specified  if ( arguments.host == NULL )    {      cerr << "You must specify a host name\n";      ret = 0;      return ret;    }  char tempdx[20] = "/tmp/temp.dx";  char name[50];  char base[100]="data_visualisation/";  // CONNECTION INITIALISATION  int sockfd, numbytes;    char buf[MAXDATASIZE];  int msgsize;  init_connection( arguments.host, &sockfd, arguments.port);  // DXLINK INITIALISATION  DXLConnection *conn =0;  char result[100];  conn = DXLStartDX("dx -macros data_visualisation/ -image -cache off ", NULL);  // dx program to execute  DXLLoadVisualProgram(conn, arguments.file);  ofstream dxfile;  char ptr[MAXDATASIZE];  char msg[LEN+1];  memset(ptr, 'x', MAXDATASIZE);  if ((numbytes=recv(sockfd, msg,LEN+1, 0)) == -1)     {      cerr << "recv\n";      exit(1);    }    int i=1;  while ( strcmp(msg,"END") )    {      if ( !strcmp(msg,"STR_NAM") )	{	  // RECEIVE THE NAME OF THE EXPERIMENT	  cerr << msg << endl;	  if ((numbytes=recv(sockfd, name, MAXDATASIZE-1, 0)) == -1) 	    {	      cerr << "recv\n";	      exit(1);	    }	  name[numbytes]='\0';	  cerr << "Received name of experiment: " << name << endl;	  strcat(base,name);	  strcat(base,"/");	  if ( mkdir( base, 0777) == -1 )	    {	      cerr << "error creating directory: ";	      cerr << strerror(errno) << endl;	    }	}      else if ( !strcmp(msg,"STR_OFF") )	{	  // RECEIVE OFFSET_WALL FROM SERVER	  cerr << msg << endl;	  if ((numbytes=recv(sockfd, ptr, MAXDATASIZE-1, 0)) == -1) 	    {	      cerr << "recv\n";	      exit(1);	    }	  ptr[numbytes]='\0';	  cerr << "Received offset_wall: " << ptr << endl;	  // Set value of the offset of the wall	  DXLSetValue(conn,"dxl_offset",ptr);	}      else if ( !strcmp(msg,"STR_RIC") )	{	  // RECEIVE RICO_WALL FROM SERVER	  cerr << msg << endl;	  if ((numbytes=recv(sockfd, ptr, MAXDATASIZE-1, 0)) == -1) 	    {	      cerr << "recv\n";	      exit(1);	    }	  ptr[numbytes] = '\0';	  cerr << "Received rico_wall: " << ptr << endl;	  // Set value of the rico of the wall	  DXLSetValue(conn, "dxl_rico", ptr);	}      else if ( !strcmp(msg,"STR_MSG") )	{	  // RECEIVE MSGSIZE FROM SERVER: SIZE OF A BUFFER  	  cerr << msg << endl;	  if ((numbytes=recv(sockfd, ptr, MAXDATASIZE-1, 0)) == -1) {	    cerr << "recv\n";	    exit(1);	  }	  msgsize = atoi(ptr);	  cerr << "msgsize: " << msgsize << endl;	}      else if ( !strcmp(msg,"STR_SER") )	{	  // RECEIVE A SERIE FROM SERVER AND SEND TO DXLINK	  cerr << msg << endl;	  char stream[msgsize];	  memset(stream, 'x', msgsize);// 	  switch(i)// 	    {// 	    case 1: dxfile.open("/tmp/1.dx",ios::out); break;// 	    case 2: dxfile.open("/tmp/2.dx",ios::out); break;// 	    default: dxfile.open(tempdx,ios::out);// 	    }	  char location[100];	  sprintf(buf,"%i",i);	  strcpy(location,base);	  strcat(location,buf);	  strcat(location,".dx");	  dxfile.open(location,ios::out);	  cerr << location << endl;	  while ( strcmp(stream,"END_SER") )	    {	      if ((numbytes=recv(sockfd, stream, msgsize-1, 0)) == -1) 		{		  cerr << "recv\n";		  exit(1);		}  	      stream[numbytes]='\0';	      dxfile << stream;	    } 	  dxfile.close();	  	  // Set the location of the source file// 	  switch(i)// 	    {// 	    case 1: DXLSetValue(conn,"dxl_inputfile","\"/tmp/1.dx\""); break;// 	    case 2: DXLSetValue(conn,"dxl_inputfile","\"/tmp/2.dx\""); break;// 	    default: DXLSetValue(conn,"dxl_inputfile","\"/tmp/temp.dx\"");// 	    }	  char temp[100]="\"";	  strcat(temp,location);	  strcat(temp,"\"");	  DXLSetValue(conn,"dxl_inputfile",temp);	  i++;	}      else if ( !strcmp(msg,"STR_FIL") )	{	  // RECEIVE A FILE FROM SERVER(WITH ALL THE SERIES) AND SEND TO DXLINK	  cerr << msg << endl;	  char stream[msgsize];	  memset(stream, 'x', msgsize);	  while ( strcmp(stream,"END_FIL") )	    {	      if ((numbytes=recv(sockfd, stream, msgsize-1, 0)) == -1) 		{		  cerr << "recv\n";		  exit(1);		}  	      stream[numbytes]='\0';	      dxfile << stream;	    } 	  dxfile.close();	 	  DXLSetValue(conn,"dxl_inputfile",tempdx);	}      else if ( !strcmp(msg,"STR_UPD") )	{	  // UPDATE THE DX SCENE	  cerr << msg << endl;	  // Execute changes	  DXLExecuteOnce(conn);	}      else	{	  cerr << msg << endl;	  cerr << "invalid identifier received\n";	}      memset(msg, 'x', MAXDATASIZE);      if ((numbytes=recv(sockfd, msg, LEN+1, 0)) == -1) 	{	  cerr << msg << endl;	  cerr << "recv\n";	  exit(1);	}    }	  close(sockfd);  /*     printf("An image window will appear\n"); */  /*     printf("and a sequence of images will be created.\n"); */  cerr << "When you are finished, hit q <RET>: ";  char ch;  do     {      cin >> ch;    }  while ( ch!='q' );  DXLExitDX(conn);  return 0;} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -