helibot.cpp

00001 
00002 #include <sys/time.h>
00003 #include <arpa/inet.h>
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <unistd.h>
00007 #include <string.h>
00008 
00009 #include "Capture.H"
00010 
00011 bool useTcp = true;
00012 
00013 int initServer()
00014 {
00015   int sock;
00016   int istrue=1;  
00017 
00018   struct sockaddr_in server_addr;
00019    
00020   if (useTcp)
00021   {
00022     if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
00023       perror("Socket");
00024       exit(1);
00025     }
00026   } else {
00027     //UDP
00028     if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
00029       perror("Socket");
00030       exit(1);
00031     }
00032   }
00033 
00034 
00035   if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&istrue,sizeof(int)) == -1) {
00036     perror("Setsockopt");
00037     exit(1);
00038   }
00039         
00040   server_addr.sin_family = AF_INET;         
00041   server_addr.sin_port = htons(5000);     
00042   server_addr.sin_addr.s_addr = INADDR_ANY; 
00043   bzero(&(server_addr.sin_zero),8); 
00044 
00045   if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
00046     perror("Unable to bind");
00047     exit(1);
00048   }
00049 
00050   if (useTcp) //No need to listen any more
00051   {
00052     if (listen(sock, 5) == -1) {
00053       perror("Listen");
00054       exit(1);
00055     }
00056 
00057     printf("\nTCPServer Waiting for client on port 5000\n");
00058     fflush(stdout);
00059   }
00060 
00061 
00062   return sock;
00063 
00064 }
00065 
00066 
00067 int main(int argc, char** argv)
00068 {
00069 
00070   Capture camera(320,240,15);
00071 
00072   camera.initCapture();
00073 
00074   int sock = initServer();
00075 
00076   while(1)
00077   {  
00078     char send_data [1024] , recv_data[1024];       
00079 
00080     int connected = -1;
00081     struct sockaddr_in client_addr;    
00082     socklen_t sin_size = sizeof(struct sockaddr_in);
00083 
00084     if (useTcp)
00085       connected = accept(sock, (struct sockaddr *)&client_addr,&sin_size);
00086 
00087     printf("\n I got a connection from (%s , %d)",
00088         inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
00089 
00090     int width = 320;
00091     int height = 240;
00092     unsigned char img[width*height*3];
00093 
00094     int bytes_recieved = -1;
00095     if (useTcp)
00096       bytes_recieved = recv(connected,recv_data,1024,0);
00097     else
00098       bytes_recieved = recvfrom(sock, recv_data, 1024, 0, (struct sockaddr *)&client_addr,&sin_size);
00099 
00100     while (1)
00101     {
00102       struct timeval real1, real2;
00103          
00104       gettimeofday(&real1, /* timezone */ 0);
00105       for(uint i=0; i<30; i++)
00106       {
00107         
00108         unsigned int frameSize = 0;
00109         unsigned char* img = camera.grabFrameRaw(frameSize);
00110 
00111         if (useTcp)
00112         {
00113           send(connected, &frameSize, sizeof(frameSize), 0);  
00114           send(connected, img, frameSize, 0);  
00115         } else {
00116           sendto(sock, &frameSize, sizeof(frameSize), 0, (const struct sockaddr*)&client_addr,sin_size);
00117           sendto(sock, img ,frameSize, 0, (const struct sockaddr*)&client_addr,sin_size);
00118         }
00119       }
00120       gettimeofday(&real2, /* timezone */ 0);
00121       const double real_secs =
00122         (real2.tv_sec - real1.tv_sec)
00123         + (real2.tv_usec - real1.tv_usec)
00124         / 1000000.0;
00125 
00126       char msg[255];
00127       printf("fps %0.2f\n", 30.0/real_secs);
00128     }
00129     close(connected);
00130   }       
00131 
00132   close(sock);
00133 
00134 
00135   return 0;
00136 }
00137 
00138 
00139 
Generated on Sun May 8 08:41:22 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3