SHARE
TWEET

Untitled

a guest Oct 21st, 2016 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <netdb.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10. #include <ifaddrs.h>
  11. #include <pthread.h>
  12.  
  13. #define BUFSIZE 1024
  14. #define SERVER_PORT 1234
  15. #define MAX_THREADS 4
  16.  
  17. int myPort;
  18. const char* myIp;
  19. float recValue;
  20.  
  21. float distance = 5.5;
  22.  
  23. pthread_t pth;  // this is our thread identifier
  24.  
  25. struct sockaddr_in server;
  26.  
  27. int main() {
  28.     printf("Hello World");
  29. }
  30.  
  31. /* This is our thread function.  It is like main(), but for a thread */
  32. void *threadFunc(void *arg) {
  33.  
  34.     int sockfd; /* socket */
  35.     int portno; /* port to listen on */
  36.     int clientlen; /* byte size of client's address */
  37.     struct sockaddr_in serveraddr; /* server's addr */
  38.     struct sockaddr_in clientaddr; /* client addr */
  39.     struct hostent *hostp; /* client host info */
  40.     char buf[BUFSIZE]; /* message buf */
  41.     char *hostaddrp; /* dotted decimal host addr string */
  42.     int optval; /* flag value for setsockopt */
  43.     int n; /* message byte size */
  44.  
  45.     /*
  46.      * socket: create the parent socket
  47.      */
  48.     sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  49.  
  50.     if (sockfd < 0)
  51.         printf("ERROR opening socket");
  52.  
  53.     /* setsockopt: Handy debugging trick that lets
  54.      * us rerun the server immediately after we kill it;
  55.      * otherwise we have to wait about 20 secs.
  56.      * Eliminates "ERROR on binding: Address already in use" error.
  57.      */
  58.     optval = 1;
  59.     setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const void *) &optval, sizeof(int));
  60.  
  61.     /*
  62.      * build the server's Internet address
  63.      */
  64.     bzero((char *) &serveraddr, sizeof(serveraddr));
  65.     serveraddr.sin_family = AF_INET;
  66.     serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
  67.     serveraddr.sin_port = htons(SERVER_PORT);
  68.  
  69.     /*
  70.      * bind: associate the parent socket with a port
  71.      */
  72.     if (bind(sockfd, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0)
  73.         printf("ERROR on binding");
  74.  
  75.     /*
  76.      * main loop: wait for a datagram, then echo it
  77.      */
  78.     clientlen = sizeof(clientaddr);
  79.     while (1) {
  80.  
  81.         /*
  82.          * recvfrom: receive a UDP datagram from a client
  83.          */
  84.         bzero(buf, BUFSIZE);
  85.         n = recvfrom(sockfd, buf, BUFSIZE, 0, (struct sockaddr *) &clientaddr, &clientlen);
  86.         if (n < 0)
  87.             error("ERROR in recvfrom");
  88.  
  89.         /*
  90.          * gethostbyaddr: determine who sent the datagram
  91.          */
  92.         hostp = gethostbyaddr((const char *) &clientaddr.sin_addr.s_addr, sizeof(clientaddr.sin_addr.s_addr), AF_INET);
  93.         if (hostp == NULL)
  94.             error("ERROR on gethostbyaddr");
  95.         hostaddrp = inet_ntoa(clientaddr.sin_addr);
  96.         if (hostaddrp == NULL)
  97.             error("ERROR on inet_ntoa\n");
  98.         printf("server received datagram from %s (%s)\n", hostp->h_name, hostaddrp);
  99.         printf("server received %d/%d bytes: %s\n", strlen(buf), n, buf);
  100.  
  101.         /*
  102.          * sendto: echo the input back to the client
  103.          */
  104.         n = sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *) &clientaddr, clientlen);
  105.         if (n < 0)
  106.             error("ERROR in sendto");
  107.     }
  108. }
  109.  
  110. char** str_split(char* a_str, const char a_delim) {
  111.     char** result = 0;
  112.     size_t count = 0;
  113.     char* tmp = a_str;
  114.     char* last_comma = 0;
  115.     char delim[2];
  116.     delim[0] = a_delim;
  117.     delim[1] = 0;
  118.  
  119.     /* Count how many elements will be extracted. */
  120.     while (*tmp) {
  121.         if (a_delim == *tmp) {
  122.             count++;
  123.             last_comma = tmp;
  124.         }
  125.         tmp++;
  126.     }
  127.  
  128.     /* Add space for trailing token. */
  129.     count += last_comma < (a_str + strlen(a_str) - 1);
  130.  
  131.     /* Add space for terminating null string so caller
  132.      knows where the list of returned strings ends. */
  133.     count++;
  134.  
  135.     result = malloc(sizeof(char*) * count);
  136.  
  137.     if (result) {
  138.         size_t idx = 0;
  139.         char* token = strtok(a_str, delim);
  140.  
  141.         while (token) {
  142.             *(result + idx++) = strdup(token);
  143.             token = strtok(0, delim);
  144.         }
  145.         *(result + idx) = 0;
  146.     }
  147.  
  148.     return result;
  149. }
  150.  
  151. EXTRA_INST_SETUP(struct inst_data *ip, const char *name, int argc, const char**argv) {
  152.     printf("UDP Inst setup\n");
  153.     int x;
  154.  
  155.     recValue = 5.5;
  156.     char** tokens;
  157.     char* parValue;
  158.  
  159.     for (x = 0; x < argc; x++) {
  160.         //hal_print_msg(RTAPI_MSG_ERR,"argv[%d] = %s", x, argv[x]);
  161.         parValue = argv[x];
  162.         tokens = str_split(parValue, "=");
  163.         if (tokens[0] == "ip")
  164.             myIp = (char*) (*(tokens + 1));
  165.         if (tokens[0] == "port")
  166.             myPort = atoi(*(tokens + 1));
  167.     }
  168.  
  169.     int i = 0;
  170.  
  171.     /* Create worker thread */
  172.     pthread_create(&pth, NULL, threadFunc, "processing...");
  173.  
  174.     return 0;
  175. }
  176.  
  177. FUNCTION( _) {
  178.     distance = recValue;
  179. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top