Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <netdb.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <ifaddrs.h>
- #include <pthread.h>
- #define BUFSIZE 1024
- #define SERVER_PORT 1234
- #define MAX_THREADS 4
- int myPort;
- const char* myIp;
- float recValue;
- float distance = 5.5;
- pthread_t pth; // this is our thread identifier
- struct sockaddr_in server;
- int main() {
- printf("Hello World");
- }
- /* This is our thread function. It is like main(), but for a thread */
- void *threadFunc(void *arg) {
- int sockfd; /* socket */
- int portno; /* port to listen on */
- int clientlen; /* byte size of client's address */
- struct sockaddr_in serveraddr; /* server's addr */
- struct sockaddr_in clientaddr; /* client addr */
- struct hostent *hostp; /* client host info */
- char buf[BUFSIZE]; /* message buf */
- char *hostaddrp; /* dotted decimal host addr string */
- int optval; /* flag value for setsockopt */
- int n; /* message byte size */
- /*
- * socket: create the parent socket
- */
- sockfd = socket(AF_INET, SOCK_DGRAM, 0);
- if (sockfd < 0)
- printf("ERROR opening socket");
- /* setsockopt: Handy debugging trick that lets
- * us rerun the server immediately after we kill it;
- * otherwise we have to wait about 20 secs.
- * Eliminates "ERROR on binding: Address already in use" error.
- */
- optval = 1;
- setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const void *) &optval, sizeof(int));
- /*
- * build the server's Internet address
- */
- bzero((char *) &serveraddr, sizeof(serveraddr));
- serveraddr.sin_family = AF_INET;
- serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
- serveraddr.sin_port = htons(SERVER_PORT);
- /*
- * bind: associate the parent socket with a port
- */
- if (bind(sockfd, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0)
- printf("ERROR on binding");
- /*
- * main loop: wait for a datagram, then echo it
- */
- clientlen = sizeof(clientaddr);
- while (1) {
- /*
- * recvfrom: receive a UDP datagram from a client
- */
- bzero(buf, BUFSIZE);
- n = recvfrom(sockfd, buf, BUFSIZE, 0, (struct sockaddr *) &clientaddr, &clientlen);
- if (n < 0)
- error("ERROR in recvfrom");
- /*
- * gethostbyaddr: determine who sent the datagram
- */
- hostp = gethostbyaddr((const char *) &clientaddr.sin_addr.s_addr, sizeof(clientaddr.sin_addr.s_addr), AF_INET);
- if (hostp == NULL)
- error("ERROR on gethostbyaddr");
- hostaddrp = inet_ntoa(clientaddr.sin_addr);
- if (hostaddrp == NULL)
- error("ERROR on inet_ntoa\n");
- printf("server received datagram from %s (%s)\n", hostp->h_name, hostaddrp);
- printf("server received %d/%d bytes: %s\n", strlen(buf), n, buf);
- /*
- * sendto: echo the input back to the client
- */
- n = sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *) &clientaddr, clientlen);
- if (n < 0)
- error("ERROR in sendto");
- }
- }
- char** str_split(char* a_str, const char a_delim) {
- char** result = 0;
- size_t count = 0;
- char* tmp = a_str;
- char* last_comma = 0;
- char delim[2];
- delim[0] = a_delim;
- delim[1] = 0;
- /* Count how many elements will be extracted. */
- while (*tmp) {
- if (a_delim == *tmp) {
- count++;
- last_comma = tmp;
- }
- tmp++;
- }
- /* Add space for trailing token. */
- count += last_comma < (a_str + strlen(a_str) - 1);
- /* Add space for terminating null string so caller
- knows where the list of returned strings ends. */
- count++;
- result = malloc(sizeof(char*) * count);
- if (result) {
- size_t idx = 0;
- char* token = strtok(a_str, delim);
- while (token) {
- *(result + idx++) = strdup(token);
- token = strtok(0, delim);
- }
- *(result + idx) = 0;
- }
- return result;
- }
- EXTRA_INST_SETUP(struct inst_data *ip, const char *name, int argc, const char**argv) {
- printf("UDP Inst setup\n");
- int x;
- recValue = 5.5;
- char** tokens;
- char* parValue;
- for (x = 0; x < argc; x++) {
- //hal_print_msg(RTAPI_MSG_ERR,"argv[%d] = %s", x, argv[x]);
- parValue = argv[x];
- tokens = str_split(parValue, "=");
- if (tokens[0] == "ip")
- myIp = (char*) (*(tokens + 1));
- if (tokens[0] == "port")
- myPort = atoi(*(tokens + 1));
- }
- int i = 0;
- /* Create worker thread */
- pthread_create(&pth, NULL, threadFunc, "processing...");
- return 0;
- }
- FUNCTION( _) {
- distance = recValue;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement