Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. // ConsoleApplication20.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "pch.h"
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <winsock2.h>
  10. #include <ws2tcpip.h>
  11. #include <windows.h>
  12. using namespace std;
  13.  
  14. #pragma comment(lib, "Ws2_32.lib")
  15. #pragma warning(disable:4996)
  16.  
  17. int main()
  18. {
  19. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10); //zelena farba
  20. WSADATA wsaData; //struktura wsadata na pracu s winsock
  21. int iResult;
  22. int o;
  23.  
  24. iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); //zakladna inicilializacia winsocku
  25. if (iResult != 0)
  26. {
  27. printf("WSAStartup failed : %d\n", iResult);
  28. return 1;
  29. }
  30. struct addrinfo *result = NULL, *ptr = NULL; //praca s adresami
  31. struct addrinfo hints;
  32.  
  33. ZeroMemory(&hints, sizeof(hints));
  34. hints.ai_family = AF_UNSPEC;
  35. hints.ai_socktype = SOCK_STREAM;
  36. hints.ai_protocol = IPPROTO_TCP; //protokol TCP
  37.  
  38. iResult = getaddrinfo("147.175.115.34", "777", &hints, &result);
  39.  
  40. if (iResult != 0) //kontrola, ci nenastala chyba
  41. {
  42. printf("getaddrinfo failed : %d\n", iResult);
  43. WSACleanup();
  44. return 1;
  45. }
  46. else
  47. printf("getaddrinfo didn’t fail…\n");
  48.  
  49. //vytvorenie socketu
  50.  
  51. SOCKET ConnectSocket = INVALID_SOCKET;
  52. ptr = result;
  53. ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol); // pokus o vytvorenie socketu
  54.  
  55. if (ConnectSocket == INVALID_SOCKET) //kontrola
  56. {
  57. printf("Error at socket() : %ld\n", WSAGetLastError());
  58. freeaddrinfo(result);
  59. WSACleanup();
  60. return 1;
  61. }
  62. else
  63. printf("Error at socket DID NOT occur…\n");
  64.  
  65. //pokus o pripojenie sa na server
  66.  
  67. iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
  68.  
  69. if (iResult == SOCKET_ERROR) //kontrola, ci nenastala chyba
  70. printf("Not connected to server…\n");
  71. else
  72. printf("Connected to server!\n");
  73.  
  74. if (iResult == SOCKET_ERROR) //osetrenie chyboveho stavu
  75. {
  76. closesocket(ConnectSocket);
  77. ConnectSocket = INVALID_SOCKET;
  78. WSACleanup();
  79. return 1;
  80. }
  81. Sleep(250);
  82.  
  83. SetConsoleOutputCP(CP_UTF8);
  84. const char* sprava = "Adam Novotn" "\xc3\xbd \n";
  85. wprintf(L"%S", sprava);
  86.  
  87. while (1)
  88. {
  89. //posielanie
  90. char sendbuf[4096];
  91. int abc = 4096;
  92. int o;
  93. printf("Zadaj retazec: ");
  94. fgets(sendbuf, abc, stdin);
  95. iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
  96. if (iResult == SOCKET_ERROR)
  97. {
  98. printf("send failed : %d\n", WSAGetLastError());
  99. closesocket(ConnectSocket);
  100. WSACleanup();
  101. return 1;
  102. }
  103. printf("Bytes Sent : %ld\n", iResult); //vypisanie poctu odoslanych dat
  104.  
  105.  
  106. //prijimanie
  107.  
  108. #define DEFAULT_BUFLEN 4096 //velkost prijimacieho buffera
  109. int recvbuflen = DEFAULT_BUFLEN;
  110. int i;
  111. char recvbuf[DEFAULT_BUFLEN];
  112.  
  113. iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0); //funkcia na príjimanie
  114.  
  115. if (iResult > 0)
  116. printf("Bytes received : %d\n", iResult); //prisli validne data, vypis poctu
  117. else if (iResult == 0)
  118. printf("Connection closed\n"); //v tomto pripade server ukoncil komunikaciu
  119. else
  120. printf("recv failed with error : %d\n", WSAGetLastError()); //ina chyba
  121.  
  122.  
  123.  
  124. for (i = 0; i <= strlen(recvbuf); i++)
  125. {
  126. if (recvbuf[i] == '\n')
  127. {
  128. recvbuf[i] = '\0';
  129. }
  130.  
  131. }
  132.  
  133.  
  134. }
  135. //zavretie socketu
  136.  
  137. closesocket(ConnectSocket);
  138. WSACleanup();
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement