Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. // srednia.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6.  
  7. #ifndef UNICODE
  8. #endif
  9. #define WIN32_LEAN_AND_MEAN
  10. #pragma warning(disable:4996)
  11. #include <winsock2.h>
  12. #include <ws2tcpip.h>
  13. #include <stdio.h>
  14. #include <conio.h>
  15. #include <string>
  16. #include <string.h>
  17. #include <vector>
  18. #include <algorithm>
  19. #pragma pack(1)
  20. #pragma comment(lib, "ws2_32.lib")
  21. int wmain()
  22. {
  23.  
  24. WSADATA wsaData;
  25. int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  26. if (iResult != NO_ERROR) {
  27. wprintf(L"WSAStartup function failed with error: %d\n", iResult);
  28. return 1;
  29. }
  30.  
  31. SOCKET ConnectSocket;
  32. ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  33. if (ConnectSocket == INVALID_SOCKET) {
  34. wprintf(L"socket function failed with error: %ld\n", WSAGetLastError());
  35. WSACleanup();
  36. return 1;
  37. }
  38.  
  39. sockaddr_in clientService;
  40. clientService.sin_family = AF_INET;
  41. clientService.sin_addr.s_addr = inet_addr("192.168.0.3");
  42. clientService.sin_port = htons(7600);
  43.  
  44.  
  45. iResult = connect(ConnectSocket, (SOCKADDR *)& clientService, sizeof(clientService));
  46. if (iResult == SOCKET_ERROR) {
  47. wprintf(L"connect function failed with error: %ld\n", WSAGetLastError());
  48. iResult = closesocket(ConnectSocket);
  49. if (iResult == SOCKET_ERROR)
  50. wprintf(L"closesocket function failed with error: %ld\n", WSAGetLastError());
  51. WSACleanup();
  52. return 1;
  53. }
  54.  
  55. wprintf(L"Connected to server.\n");
  56.  
  57. char recvbuf[1000];
  58. int recvbuflen = sizeof(recvbuf);
  59.  
  60. iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
  61. recvbuf[iResult] = 0;
  62. printf("Bytes received: %s\n", recvbuf);
  63. std::string s = "";
  64. s += recvbuf[0];
  65. s += recvbuf[1];
  66. s += recvbuf[2];
  67. if (s == "200")
  68. {
  69. int n = 0, size = 10000;
  70. int i = 0;
  71. int mod = 0;
  72. int l = 0;
  73. float sum;
  74. int counter = 0;
  75. const char* ss = "SORT\r\n";
  76. iResult = send(ConnectSocket, ss, strlen(ss), 0);
  77. //sssss
  78. do {
  79. iResult = recv(ConnectSocket, recvbuf + n, recvbuflen - n, 0);
  80. n += iResult;
  81. if(n>=4)
  82. {
  83. mod = n%4;
  84. n = mod;
  85. sum += (float)recvbuf[l];
  86. printf("Aktualna suma to %d", sum);
  87. l += 4;
  88. counter++;
  89. if(counter%2==0)
  90. {
  91. sum = sum/2;
  92. iResult = send(ConnectSocket, (char*)&sum, sizeof(sum), 0);
  93. sum = 0;
  94. }
  95. if(counter%3==0)
  96. {
  97. char a = 0xDD;
  98. iResult = send(ConnectSocket,(char*)&a, sizeof(a), 0);
  99. }
  100.  
  101.  
  102. }
  103.  
  104. } while (iResult != 0);
  105.  
  106. }
  107. else
  108. {
  109. WSACleanup();
  110. return 1;
  111. }
  112. iResult = closesocket(ConnectSocket);
  113. if (iResult == SOCKET_ERROR) {
  114. wprintf(L"closesocket function failed with error: %ld\n", WSAGetLastError());
  115. WSACleanup();
  116. return 1;
  117. }
  118. _getch();
  119. WSACleanup();
  120. return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement