Advertisement
Lionek1437

PSI 01 RECIVER

Apr 10th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. // UDP_Communication_Framework.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #pragma comment(lib, "ws2_32.lib")
  5. #include "stdafx.h"
  6. #include <winsock2.h>
  7. #include "ws2tcpip.h"
  8.  
  9. #include<iostream>
  10. #include<fstream>
  11. #include<string>
  12. #include<stdlib.h>
  13. using namespace std;
  14.  
  15. #define TARGET_IP "147.32.221.22"
  16.  
  17. #define BUFFERS_LEN 1024
  18.  
  19. //#define SENDER
  20. #define RECEIVER
  21.  
  22. #ifdef SENDER
  23. #define TARGET_PORT 5555
  24. #define LOCAL_PORT 8888
  25. #endif // SENDER
  26.  
  27. #ifdef RECEIVER
  28. #define TARGET_PORT 8888
  29. #define LOCAL_PORT 5555
  30. #endif // RECEIVER
  31.  
  32.  
  33. void InitWinsock()
  34. {
  35. WSADATA wsaData;
  36. WSAStartup(MAKEWORD(2, 2), &wsaData);
  37. }
  38.  
  39. int processHeader(char* buffer) {
  40. int ret = 0;
  41.  
  42. for (int i = 0; i <= 32; i++) {
  43. int tmp = (int)buffer[i] - 48;
  44. //printf("%d\n", tmp);
  45. if (tmp < 10 && tmp >= 0) {
  46. ret = ret * 10 + (int)tmp;
  47. }
  48. }
  49.  
  50.  
  51. return ret;
  52. }
  53.  
  54. //**********************************************************************
  55. int main()
  56. {
  57. SOCKET socketS;
  58.  
  59. InitWinsock();
  60.  
  61. struct sockaddr_in local;
  62. struct sockaddr_in from;
  63.  
  64. int fromlen = sizeof(from);
  65. local.sin_family = AF_INET;
  66. local.sin_port = htons(LOCAL_PORT);
  67. local.sin_addr.s_addr = INADDR_ANY;
  68.  
  69.  
  70. socketS = socket(AF_INET, SOCK_DGRAM, 0);
  71. if (bind(socketS, (sockaddr*)&local, sizeof(local)) != 0) {
  72. printf("Binding error!\n");
  73. getchar(); //wait for press Enter
  74. return 1;
  75. }
  76. //**********************************************************************
  77. char buffer_rx[BUFFERS_LEN];
  78. char buffer_tx[BUFFERS_LEN];
  79.  
  80. #ifdef SENDER
  81.  
  82. sockaddr_in addrDest;
  83. addrDest.sin_family = AF_INET;
  84. addrDest.sin_port = htons(TARGET_PORT);
  85. InetPton(AF_INET, _T(TARGET_IP), &addrDest.sin_addr.s_addr);
  86.  
  87. int i = 1313;
  88.  
  89. sprintf(buffer_tx, "%d", i);
  90.  
  91. //strncpy(buffer_tx, "something", BUFFERS_LEN); //put some data to buffer
  92. printf("Sending packet.\n");
  93. sendto(socketS, buffer_tx, strlen(buffer_tx), 0, (sockaddr*)&addrDest, sizeof(addrDest));
  94.  
  95. closesocket(socketS);
  96.  
  97. #endif // SENDER
  98.  
  99. #ifdef RECEIVER
  100.  
  101. int messageLength;
  102. char name[BUFFERS_LEN];
  103.  
  104. strncpy(buffer_rx, "", BUFFERS_LEN);
  105. printf("Recieving ...\n");
  106.  
  107. // get name
  108. if (recvfrom(socketS, buffer_rx, sizeof(buffer_rx), 0, (sockaddr*)&from, &fromlen) == SOCKET_ERROR) {
  109. printf("Header socket error!\n");
  110. getchar();
  111. return 1;
  112. }
  113. else {
  114. printf("Name: %s!\n", buffer_rx);
  115. }
  116.  
  117. strcpy(name, buffer_rx);
  118.  
  119. // get size
  120. if (recvfrom(socketS, buffer_rx, sizeof(buffer_rx), 0, (sockaddr*)&from, &fromlen) == SOCKET_ERROR) {
  121. printf("Header socket error!\n");
  122. getchar();
  123. return 1;
  124. }
  125.  
  126.  
  127. else {
  128. messageLength = processHeader(buffer_rx);
  129. printf("Size: %d\n", messageLength);
  130. }
  131.  
  132. char* fileBuffer = (char*)malloc(messageLength);
  133.  
  134. // init file
  135. FILE *p_file = fopen(name, "wb");
  136.  
  137. // process header, set counter
  138. int i;
  139. for (i = 0; i < messageLength / BUFFERS_LEN; i++) {
  140. strncpy(buffer_rx, "", BUFFERS_LEN);
  141. if (recvfrom(socketS, buffer_rx, sizeof(buffer_rx), 0, (sockaddr*)&from, &fromlen) == SOCKET_ERROR) {
  142. printf("Socket error!\n");
  143. getchar();
  144. return 1;
  145. }
  146. else {
  147. printf("Data: %i %x\n", i, buffer_rx);
  148. strncpy(fileBuffer, buffer_rx, BUFFERS_LEN);
  149. fileBuffer += BUFFERS_LEN;
  150. // fputs(buffer_rx, p_file);
  151. }
  152.  
  153. }
  154.  
  155. if (recvfrom(socketS, buffer_rx, sizeof(buffer_rx), 0, (sockaddr*)&from, &fromlen) == SOCKET_ERROR) {
  156. printf("Socket error!\n");
  157. getchar();
  158. return 1;
  159. }
  160. else {
  161. printf("Data: %i %x\n", i, buffer_rx);
  162. strncpy(fileBuffer, buffer_rx, messageLength % BUFFERS_LEN + 1);
  163. fileBuffer -= BUFFERS_LEN * i;
  164. // fputs(buffer_rx, p_file);
  165. }
  166. //for (int j = 0; j < messageLength; ++j)
  167. fwrite(fileBuffer, 1, messageLength, p_file);
  168. // fprintf(p_file, "%c", fileBuffer[j]);
  169. //fputs(fileBuffer, p_file);
  170.  
  171.  
  172. fclose(p_file);
  173.  
  174. closesocket(socketS);
  175. #endif
  176. //**********************************************************************
  177.  
  178. getchar(); //wait for press Enter
  179. return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement