#include #include using namespace std; // This is an example program that shows how to issue commands to // a remote computer and receive the results programmatically. // // The current reason for this is to help coordinate programs running // on a Linux desktop and a VxWorks system without having to write // an entire client server system and have the server always running // on the VxWorks machine. // // The main idea with this method is to create a couple of pipes // that can be used to redirect the stdin and stdout of the // remote shell process (telnet, rsh, ssh, ...whatever) to this // program so they can be read and written to by read() and write(). // // This is accomplished by first setting creating the pipe, then // forking this process into 2 processes, then having the "child" // fork replace his stdin, stdout pipes with the newly created ones, // and finally having the child "exec" itself into the remote // shell program. Since the remote shell will inherit its stdin // and stdout streams from the fork, it will use the pipes we setup // for that purpose. Cool huh? #include #include #include #include #include #define _DBG_ cout<<__FILE__<<":"<<__LINE__<<" " #define _DBG__ _DBG_<0){ cout<<"====================================================="< in return. Move compelete." << endl; return true; } else return false; } int main(int narg, char *argv[]) { //char *const args[]={"ls",NULL}; //execv("/bin/sh", args); //return 0; // Create 2 pipes int fdA[2]; int fdB[2]; pipe(fdA); pipe(fdB); // The following splits us into 2 identical processes except there // is a parent child relationship between the two. The "child" will // have pid==0 while the parent will not. pid_t pid = fork(); // Make fdB[0] non-blocking so we can read from it without getting stuck fcntl(fdB[0], F_SETFL, O_NONBLOCK); if(pid==0){ // The child replaces his stdin with fdA and his stdout with fdB close(fdA[1]); // This will never be used by this fork close(fdB[0]); // This will never be used by this fork dup2(fdA[0], STDIN_FILENO); dup2(fdB[1], STDOUT_FILENO); close(fdA[0]); // close one of the file descriptors (stdin is still open) close(fdB[1]); // close one of the file descriptors (stdout is still open) // Now, exec this fork into a shell process. The first argument of execl // is the actual program to run. The second argument starts the "argv" // list passed into the program which usually contains the name of the // program as invoked in the shell as the 0-th argument. char *const args[]={NULL}; execl("/usr/bin/telnet", "telnet", "192.168.1.2", 0); }else{ close(fdA[0]); // This will never be used by this fork close(fdB[1]); // This will never be used by this fork // Write a command to the remote shell sleep(1); // determining cathode plane number and orientation char plane[128], rotation[128]; cout << "Please enter the cathode plane number (enter 999 for no cathode, rock scan): " << endl; cin >> plane; cout << "Please enter the clockwise rotation from vertical (with FDC1 at top) in degrees: " << endl; cin >> rotation; // Moving laser to home position cout << "Moving laser to home position..." << endl; const char *cmd = "Laser_Home()\n"; _DBG_<<"write():"<