Sooldierr

TCP sort

Apr 25th, 2017
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.52 KB | None | 0 0
  1. #pragma comment(lib,"ws2_32.lib")
  2. #include <winsock2.h>
  3. #include <iostream>
  4. #include <conio.h>
  5. #include <string>
  6. using namespace std;
  7.  
  8. void wyswietl(const int *tab, const int n)
  9. {
  10.     cout << "\nDane\n\n";
  11.     for (int i = 0; i < n; i++)
  12.     {
  13.         cout << tab[i] << endl;
  14.     }
  15. }
  16.  
  17. void sortuj(int *tab, const int size, const int znak)
  18. {
  19.     if( znak > 0)
  20.         for (int i = size - 1; i >= 0; i--)
  21.         {
  22.             int temp = tab[0];
  23.             int id = 0;
  24.             for (int j = 0; j <= i; j++)
  25.             {
  26.                 if (temp < tab[j])
  27.                 {
  28.                     temp = tab[j];
  29.                     id = j;
  30.                 }
  31.             }
  32.             tab[id] = tab[i];
  33.             tab[i] = temp;
  34.         }
  35.  
  36.     else if(znak <= 0)
  37.         for (int i = size - 1; i >= 0; i--)
  38.         {
  39.             int temp = tab[0];
  40.             int id = 0;
  41.             for (int j = 0; j <= i; j++)
  42.             {
  43.                 if (temp > tab[i])
  44.                 {
  45.                     temp = tab[i];
  46.                     id = j;
  47.                 }
  48.             }
  49.             tab[id] = tab[i];
  50.             tab[i] = temp;
  51.         }
  52. }
  53.  
  54. int main()
  55. {
  56.     WSADATA Data;
  57.     WSAStartup(MAKEWORD(2,2), &Data);
  58.     sockaddr_in Adr;
  59.     //Adr.sin_addr.s_addr = inet_addr("192.168.0.104");
  60.     Adr.sin_addr.s_addr = inet_addr("127.1.0.0");
  61.     Adr.sin_port = htons(7700);
  62.     Adr.sin_family = AF_INET;
  63.     SOCKET s;
  64.     s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  65.     int iResult;
  66.     if(connect(s, (SOCKADDR*)&Adr, sizeof(Adr)) < 0)
  67.     {
  68.         cout << "Blad w polaczeniu do serwera: " << WSAGetLastError();
  69.         _getch();
  70.         shutdown(s, 2);
  71.         closesocket(s);
  72.         WSACleanup();
  73.         return 0;
  74.     }
  75.     char buff[34] = {0};
  76.     int len = recv(s, buff, sizeof(buff), 0);
  77.     buff[len] = '\0';
  78.     cout << buff << endl;
  79.  
  80.     if(buff[0] == '4')
  81.     {
  82.         cout << "Blad w polaczeniu do serwera: " << WSAGetLastError();
  83.         _getch();
  84.         shutdown(s, 2);
  85.         closesocket(s);
  86.         WSACleanup();
  87.         return 0;
  88.     }
  89.  
  90.     //send(s, "SORT", sizeof("SORT"), 0);
  91.     signed char znak;
  92.     do
  93.     {
  94.         send(s, (char*)"SORT", sizeof("SORT"), 0);
  95.         iResult = recv(s, (char*)&znak, sizeof(znak), 0);
  96.         if (iResult == SOCKET_ERROR) {
  97.             printf("send failed: %d\n", WSAGetLastError());
  98.             closesocket(s);
  99.             WSACleanup();
  100.             return 1;
  101.         }    
  102.         //signed char znak = liczba;
  103.         int size = abs(znak);
  104.         int *tab = new int[size];
  105.  
  106.         iResult = recv(s, (char*)&(*tab), sizeof(tab) * size, 0);
  107.         if (iResult == SOCKET_ERROR) {
  108.             printf("send failed: %d\n", WSAGetLastError());
  109.             closesocket(s);
  110.             WSACleanup();
  111.             return 1;
  112.         }
  113.         cout << "Dane przed sortowaniem\n\n";
  114.         wyswietl(tab, size);
  115.         int temp;
  116.         int id;
  117.  
  118.         sortuj(tab, size, znak);
  119.         cout << "Dane po sortowaniu\n\n";
  120.         wyswietl(tab, size);
  121.         ////odesłanie w postaci binarnej
  122.         //iResult = send(s, (char*)tab, sizeof(tab) * size, 0);
  123.         //if (iResult == SOCKET_ERROR) {
  124.         //    printf("send failed: %d\n", WSAGetLastError());
  125.         //    closesocket(s);
  126.         //    WSACleanup();
  127.         //    return 1;
  128.         //}
  129.         //odesłanie w postaci tekstowej
  130.  
  131.         iResult = send(s, (char*)&(*tab), sizeof(tab) * size, 0);
  132.         if (iResult == SOCKET_ERROR) {
  133.             printf("send failed: %d\n", WSAGetLastError());
  134.             closesocket(s);
  135.             WSACleanup();
  136.             return 1;
  137.         }
  138.         memset(buff, 0, sizeof(buff));
  139.         recv(s, buff, sizeof(buff), 0);
  140.         cout << buff << endl;
  141.         _getch();
  142.     } while (true);
  143.  
  144.     _getch();
  145.     shutdown(s, 2);
  146.     closesocket(s);
  147.     WSACleanup();
  148.     return 0;
  149. }
Add Comment
Please, Sign In to add comment