Advertisement
Guest User

Untitled

a guest
Sep 14th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #pragma comment(lib,"ws2_32.lib")
  2. #pragma warning(disable: 4996)
  3. #include <winsock2.h>
  4. #include <iostream>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string>
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12.     WSAData wsaData; //Запрашиваемая версия библиотеки winsock. понадобится, чтобы загрузить библиотеку
  13.     WORD dllVersion = MAKEWORD(2, 1); //Загружаем библиотеку. Первый параметр - запрашиваемая версия библиотеки. Второй - ссылка на структуру
  14.     if (WSAStartup(dllVersion, &wsaData) != 0) {
  15.         cout << "Error in library loading" << endl;
  16.         exit(1);
  17.     }
  18.     SOCKADDR_IN addr;
  19.     addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //Localhost
  20.     addr.sin_port = htons(1111); //Семейство портов
  21.     addr.sin_family = AF_INET; //Семейство протоколов, протокол
  22.  
  23.     SOCKET sListen = socket(AF_INET, SOCK_STREAM, NULL);
  24.     bind(sListen, (SOCKADDR*)&addr, sizeof(addr));
  25.     listen(sListen, SOMAXCONN);
  26.     SOCKET connection = socket(AF_INET, SOCK_STREAM, NULL);
  27.  
  28.  
  29.     if (connect(connection, (SOCKADDR*)&addr, sizeof(addr)) != 0) {
  30.         cout << "Connection is failed" << endl;
  31.         return 1;
  32.     }
  33.     else
  34.     {
  35.  
  36.         cout << "Successful connection!" << endl;
  37.         /*const char size = 10;
  38.         char *arr = new char[size];
  39.         cout << "ARRAY:" << endl;
  40.         for (int i = 0; i < size; i++)
  41.         {
  42.             cout << "a[" << i << "] = ";
  43.             cin >> arr[i];
  44.             send(connection, arr, sizeof(arr), NULL);
  45.         }
  46.         delete[] arr;*/
  47.  
  48.         cout << "Enter the amount of numbers" << endl;
  49.         string am;
  50.         cin >> am;
  51.         send(connection, am.c_str(), sizeof(am), NULL);
  52.         for (int i = 0; i < atoi(am.c_str()); i++) {
  53.             cout << "Enter the number" << endl;
  54.             string num;
  55.             cin >> num;
  56.             send(connection, num.c_str(), sizeof(num), NULL);
  57.         }
  58.         recv(connection, (char*) am.c_str(), sizeof(am), NULL);
  59.         cout << am << endl;
  60.  
  61.     }
  62.  
  63.     system("pause");
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement