Advertisement
Guest User

Untitled

a guest
Aug 29th, 2017
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. // Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
  2.  
  3. #pragma comment(lib,"ws2_32.lib") //Winsock Library
  4.  
  5. #include "WSImuFunctionsPrivatePCH.h"
  6. #include <sstream>
  7. #include <vector>
  8. #include <string>
  9. #include <iostream>
  10. #include "WSImuFunctionsBPLibrary.h"
  11.  
  12.  
  13.  
  14. #define SERVER "192.168.0.255"  //ip address of udp server
  15. #define BUFLEN 512  //Max length of buffer
  16. #define PORT 1234 //The port on which to listen for incoming data
  17.  
  18. static struct sockaddr_in si_other;
  19. static int s, slen, countframe;
  20. static char buf[512];
  21. static char message[512];
  22.  
  23. UWSImuFunctionsBPLibrary::UWSImuFunctionsBPLibrary(const FObjectInitializer& ObjectInitializer)
  24. : Super(ObjectInitializer)
  25. {
  26.     WSADATA wsa;
  27.     //Initialise winsock
  28.     printf("\nInitialising Winsock...");
  29.     if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
  30.     {
  31.         printf("Failed. Error Code : %d", WSAGetLastError());
  32.         return;
  33.         exit(EXIT_FAILURE);
  34.     }
  35.     printf("Initialised.\n");
  36.  
  37.     //create socket
  38.     if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR)
  39.     {
  40.         printf("socket() failed with error code : %d", WSAGetLastError());
  41.         exit(EXIT_FAILURE);
  42.     }
  43.     slen = sizeof(si_other);
  44.     //setup address structure
  45.     memset((char *)&si_other, 0, sizeof(si_other));
  46.     si_other.sin_family = AF_INET;
  47.     si_other.sin_port = htons(PORT);
  48.     si_other.sin_addr.S_un.S_addr = inet_addr(SERVER);
  49.  
  50.  
  51. }
  52.  
  53. float UWSFunctionsBPLibrary::GetAngle()
  54. {
  55.     float result = 0;
  56.  
  57.     try
  58.     {
  59.  
  60.         //send the message
  61.         if (sendto(s, "", strlen(message), 0, (struct sockaddr *) &si_other, slen) == SOCKET_ERROR)
  62.         {
  63.         printf("sendto() failed with error code : %d", WSAGetLastError());
  64.         return false;
  65.         }
  66.  
  67.  
  68.  
  69.         //receive a reply and print it
  70.         //clear the buffer by filling null, it might have previously received data
  71.         memset(buf, '\0', BUFLEN);
  72.         //try to receive some data, this is a blocking call
  73.  
  74.  
  75.         struct timeval timeout;
  76.         timeout.tv_sec = 1;
  77.         timeout.tv_usec = 0;
  78.         setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout));
  79.  
  80.         int rs = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen);
  81.         if (rs == SOCKET_ERROR || rs == -1)
  82.         {
  83.         printf("recvfrom() failed with error code : %d", WSAGetLastError());
  84.         result = 0;
  85.  
  86.         }
  87.  
  88.         std::stringstream ss(buf); // Insert the string into a stream
  89.  
  90.         result = stof(ss.str());
  91.         return result;
  92.         }
  93.        
  94.     catch (std::exception &e)
  95.     {
  96.         printf(e.what());
  97.         return 0;
  98.     }
  99.     return result;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement