/*********************************************** wywolanie kamera port urzadzenie np: kamera 5095 ***********************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include int wynik; int status; #define TRUE 1 char buf[10]; char buf1[100]; int m; time_t czas1; time_t czas2; struct tm *loctime; int port, numer; sig_atomic_t child_exit_status; void clean_up_child_process(int signal_num) { /* remove child process */ int status, k; wait (&status); /* save exit status in a global variable */ child_exit_status = status; } /* S T A R T */ main (argc, argv) int argc; char *argv[]; { /* handle SIGCHLD by calling clean_up_child_process */ struct sigaction sigchild_action; memset(&sigchild_action, 0, sizeof(sigchild_action)); sigchild_action.sa_handler = &clean_up_child_process; sigaction(SIGCHLD, &sigchild_action, NULL); // zaprogramuj socket int sockfd, newsockfd, portno, clilen; struct sockaddr_in serv_addr, cli_addr; sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("ERROR opening socket"); exit(1); } bzero((char *) &serv_addr, sizeof(serv_addr)); portno = atoi(argv[1]); //arbitralny zakres 5090 - 5099 serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { perror("ERROR on binding"); exit(1); } /* czekaj na polaczenie od klienta */ listen(sockfd,5); clilen = sizeof(cli_addr); while (TRUE) { /* Przyjmij polaczenie klienta */ restart: newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen); if (newsockfd < 0) { if (errno == EINTR) goto restart; perror("ERROR on accept"); exit(1); } /* czytaj rozkazy z serwera */ bzero(buf,10); // Odczytaj numer portu usb kamery time (&czas1); time (&czas2); m=0; while (czas2 - czas1 < 5) // 5 sekund na odczyt, potem timeout { if ((read (newsockfd, &buf[m], 1)) != -1) { if( m>100) break; // padl port if (buf[m] == 0x0a) { buf[m+1]=0x0; wynik=1; break; } m++; } time (&czas2); } // Odczytaj numer zdjecia time (&czas1); time (&czas2); m=0; while (czas2 - czas1 < 5) // 5 sekund na odczyt, potem timeout { if ((read (newsockfd, &buf1[m], 1)) != -1) { if( m>100) break; // padl port if (buf1[m] == 0x0a) { buf1[m+1]=0x0; wynik=1; break; } m++; } time (&czas2); } if (! (m = fork()) ) execl("/home/rcp/pstryk", "/home/rcp/pstryk", buf, buf1, (char *)0); shutdown(newsockfd, 2); close(newsockfd); } //od while true } //od main