/* cam_comp.c * Austin Vandergon, 7/26/06 */ #include #include #include #include #include #include #include #include #include #include "cam_comp.h" /* get_image takes a char {a, b, c d} denoting a camera to get an image * from, and an pointed to a connected socket file descriptor. The image * returned from the camera is written to a fixed place in the directory * tree. */ int get_image(char cam, int* sockfd) { // send request void* image; short size, received; int fd; send(*sockfd, request, sizeof(request), 0); // receive size recv(*sockfd, &size, 2, 0); size = ntohs(size); recv(*sockfd, &received, 2, 0); if(!size) return 0; if(size < 0) { // weird thing, try reconnecting I guess... shutdown(*sockfd, SHUT_RDWR); connect_cam(cam, sockfd); return 0; } received = 0; image = malloc(size); while(received < size) received += recv(*sockfd, image + received, size - received, 0); path[8] = cam; fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU); write(fd, image, size); free(image); close(fd); return size; } /* connect_cam takes a char {a, b, c, d} denoting the camera to be * connected, and an pointer to an unconnected socket file descriptor. * It simply establishes a connection with the given camera, so that * images can be requested on a continuous basis. */ void connect_cam(char cam, int* sockfd) { struct sockaddr_in camera; switch(cam) { case 'a': inet_aton("192.168.1.250", &camera.sin_addr); break; case 'b': inet_aton("192.168.1.251", &camera.sin_addr); break; case 'c': inet_aton("192.168.1.252", &camera.sin_addr); break; case 'd': inet_aton("192.168.1.253", &camera.sin_addr); break; } camera.sin_family = AF_INET; camera.sin_port = htons(4321); *sockfd = socket(PF_INET,SOCK_STREAM,0); if(connect(*sockfd, (struct sockaddr*)&camera, sizeof(struct sockaddr)) == -1) printf("Error connecting to camera %c.\n",cam); return; }