Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1.  
  2. #include "stdafx.h"
  3.  
  4. #include <stdio.h>
  5. #include <winsock.h>
  6. #include <conio.h>
  7.  
  8. #pragma comment(lib,"ws2_32.lib") //Winsock Library
  9.  
  10. #define SERVER "192.168.0.13"  //ip address of udp server
  11. #define BUFLEN 512  //Max length of buffer
  12. #define PORT 7505   //The port on which to listen for incoming data
  13.  
  14. struct temperature
  15. {
  16.     short year;
  17.     char month;
  18.     char day;
  19.     double value;
  20. };
  21.  
  22. struct pressure
  23. {
  24.     short year;
  25.     char month;
  26.     char day;
  27.     float value;
  28. };
  29.  
  30. int main(void)
  31. {
  32.     struct temperature* T;
  33.     struct pressure* P;
  34.     int n;
  35.  
  36.     struct sockaddr_in si_other;
  37.     int s, slen = sizeof(si_other);
  38.     char buf[BUFLEN];
  39.     char message[BUFLEN];
  40.     WSADATA wsa;
  41.  
  42.     //Initialise winsock
  43.     printf("\nInitialising Winsock...");
  44.     if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
  45.     {
  46.         printf("Failed. Error Code : %d", WSAGetLastError());
  47.         exit(EXIT_FAILURE);
  48.     }
  49.     printf("Initialised.\n");
  50.  
  51.     //create socket
  52.     if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR)
  53.     {
  54.         printf("socket() failed with error code : %d", WSAGetLastError());
  55.         exit(EXIT_FAILURE);
  56.     }
  57.  
  58.     //setup address structure
  59.     memset((char *)&si_other, 0, sizeof(si_other));
  60.     si_other.sin_family = AF_INET;
  61.     si_other.sin_port = htons(PORT);
  62.     si_other.sin_addr.S_un.S_addr = inet_addr(SERVER);
  63.  
  64.     //start communication
  65.     while (1)
  66.     {
  67.         printf("Enter message : ");
  68.         scanf("%s",message);
  69.  
  70.         if (strcmp(message, "exit") == 0)
  71.             break;
  72.  
  73.         //send the message
  74.         if (sendto(s, message, strlen(message), 0, (struct sockaddr *) &si_other, slen) == SOCKET_ERROR)
  75.         {
  76.             printf("sendto() failed with error code : %d", WSAGetLastError());
  77.             getch();
  78.             exit(EXIT_FAILURE);
  79.         }
  80.  
  81.         //receive a reply and print it
  82.         //clear the buffer by filling null, it might have previously received data
  83.         memset(buf, '\0', BUFLEN);
  84.         //try to receive some data, this is a blocking call
  85.         if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen) == SOCKET_ERROR)
  86.         {
  87.             printf("recvfrom() failed with error code : %d", WSAGetLastError());
  88.             getch();
  89.             exit(EXIT_FAILURE);
  90.         }
  91.        
  92.         if ((short)buf[0] == -1)
  93.         {
  94.             printf("Nieprawidlowe polecenie");
  95.         }
  96.  
  97.         if (buf[0] == 0x17)
  98.         {
  99.             n = (int)buf[1];
  100.             printf("Temperatura\n");
  101.             for (int i = 0; i < n; i++)
  102.             {
  103.                 T = (temperature*)buf[5 + i * sizeof(temperature)];
  104.                 printf("%d-%d-%d\t%lf\n", T->year, T->month, T->day, T->value);
  105.             }
  106.         }
  107.  
  108.         if (buf[0] == 0x18)
  109.         {
  110.             n = (int)buf[1];
  111.             printf("Cisnienie");
  112.             for (int i = 0; i < n; i++)
  113.             {
  114.                 P = (pressure*)buf[5 + i * sizeof(pressure)];
  115.                 printf("%d-%d-%d\t%f\n", P->year, P->month, P->day, P->value);
  116.             }
  117.         }
  118.        
  119.  
  120.  
  121.     }
  122.  
  123.     closesocket(s);
  124.     WSACleanup();
  125.  
  126.     return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement