Advertisement
Guest User

status.c

a guest
Mar 15th, 2012
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.06 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <netdb.h>
  7.  
  8. #define SIZE 4096
  9.  
  10. int main(int argc, char* argv[]) {
  11.   int status = 200;
  12.  
  13.   int len;
  14.     int sock;
  15.     struct hostent *host;
  16.     struct sockaddr_in server;
  17.  
  18.   char buffer[SIZE];
  19.  
  20.     char handshake[] = "raw\r\n\r\n";
  21.  
  22.     sock = socket(AF_INET, SOCK_STREAM, 0);
  23.     if (sock < 0) {
  24.         exit(-1);
  25.     }
  26.  
  27.      /* host is deallocated by the socket runtime*/
  28.     if ((host = gethostbyname("127.0.0.1")) == NULL) {
  29.         exit(-1);
  30.     }
  31.  
  32.     server.sin_family = AF_INET;
  33.     server.sin_port = htons(9090);
  34.     server.sin_addr = *(struct in_addr *) host->h_addr;
  35.  
  36.     if (0 != connect(sock, (struct sockaddr *) &server, sizeof(server))) {
  37.         exit(-1);
  38.     }
  39.  
  40.     write(sock, handshake, strlen(handshake));
  41.  
  42.   len = snprintf(buffer,SIZE,"{\"receiver\":\"/wrtStatus\",\"msg\":{\"data\":%d},\"type\":\"std_msgs/UInt8\"}",status);
  43.   if (len < 0 || len >= SIZE) {
  44.     exit(-1);
  45.   }
  46.  
  47.   write(sock,"\x00",1);
  48.   write(sock,buffer,len);
  49.   write(sock,"\xFF",1);
  50.  
  51.     close(sock);
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement