Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. // ConsoleApplication1.cpp : Ten plik zawiera funkcję „main”. W nim rozpoczyna się i kończy wykonywanie programu.
  2. ////serwer
  3.  
  4. #undef UNICODE
  5.  
  6. #define WIN32_LEAN_AND_MEAN
  7.  
  8. #include "pch.h"
  9. #include <iostream>
  10. #include <winsock2.h>
  11. #include <ws2tcpip.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <thread>
  15.  
  16. // Need to link with Ws2_32.lib
  17. #pragma comment (lib, "Ws2_32.lib")
  18. // #pragma comment (lib, "Mswsock.lib")
  19.  
  20. #define DEFAULT_BUFLEN 512
  21. #define DEFAULT_PORT "27015"
  22.  
  23.  
  24. int __cdecl main(void)
  25. {
  26. WSADATA wsaData;
  27. int iResult;
  28.  
  29. SOCKET ListenSocket = INVALID_SOCKET;
  30. SOCKET ClientSocket = INVALID_SOCKET;
  31.  
  32. struct addrinfo *result = NULL;
  33. struct addrinfo hints;
  34.  
  35. int iSendResult;
  36. char recvbuf[DEFAULT_BUFLEN];
  37. int recvbuflen = DEFAULT_BUFLEN;
  38.  
  39. // Initialize Winsock
  40. std::cout << "Server started" << std::endl;
  41. iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  42. if (iResult != 0) {
  43. printf("WSAStartup failed with error: %d\n", iResult);
  44. return 1;
  45. }
  46.  
  47. ZeroMemory(&hints, sizeof(hints));
  48. hints.ai_family = AF_INET;
  49. hints.ai_socktype = SOCK_STREAM;
  50. hints.ai_protocol = IPPROTO_TCP;
  51. hints.ai_flags = AI_PASSIVE;
  52.  
  53. // Resolve the server address and port
  54. iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
  55. if (iResult != 0) {
  56. printf("getaddrinfo failed with error: %d\n", iResult);
  57. WSACleanup();
  58. return 1;
  59. }
  60.  
  61. // Create a SOCKET for connecting to server
  62. ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
  63. if (ListenSocket == INVALID_SOCKET) {
  64. printf("socket failed with error: %ld\n", WSAGetLastError());
  65. freeaddrinfo(result);
  66. WSACleanup();
  67. return 1;
  68. }
  69.  
  70. // Setup the TCP listening socket
  71. iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
  72. if (iResult == SOCKET_ERROR) {
  73. printf("bind failed with error: %d\n", WSAGetLastError());
  74. freeaddrinfo(result);
  75. closesocket(ListenSocket);
  76. WSACleanup();
  77. return 1;
  78. }
  79.  
  80. freeaddrinfo(result);
  81.  
  82. iResult = listen(ListenSocket, SOMAXCONN);
  83. if (iResult == SOCKET_ERROR) {
  84. printf("listen failed with error: %d\n", WSAGetLastError());
  85. closesocket(ListenSocket);
  86. WSACleanup();
  87. return 1;
  88. }
  89.  
  90. // Accept a client socket
  91. std::cout << "Waiting for a client to connect..." << std::endl;
  92. ClientSocket = accept(ListenSocket, NULL, NULL);
  93. std::cout << "Client connected" << std::endl;
  94. if (ClientSocket == INVALID_SOCKET) {
  95. printf("accept failed with error: %d\n", WSAGetLastError());
  96. closesocket(ListenSocket);
  97. WSACleanup();
  98. return 1;
  99. } //to tez
  100.  
  101.  
  102. // No longer need server socket
  103. closesocket(ListenSocket);
  104. bool palindrom = true;
  105. // Receive until the peer shuts down the connection
  106. do {
  107.  
  108. iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
  109. if (iResult > 0) {
  110. printf("Bytes received: %d\n", iResult);
  111. for (int i = 0; i < recvbuflen; i++) {
  112. if(recvbuf[i]== recvbuf[recvbuflen-i])
  113. {
  114. }
  115. else
  116. {
  117. palindrom = false;
  118.  
  119. }
  120. }
  121.  
  122.  
  123. // Echo the buffer back to the sender
  124. if (palindrom) {
  125. iSendResult = send(ClientSocket, "Palindrom", strlen("Palindrom"), 0);
  126. if (iSendResult == SOCKET_ERROR) {
  127. printf("send failed with error: %d\n", WSAGetLastError());
  128. closesocket(ClientSocket);
  129. WSACleanup();
  130. return 1;
  131. }
  132. }
  133. else {
  134. iSendResult = send(ClientSocket, "Nie palindrom", strlen("Nie palindrom"), 0);
  135. if (iSendResult == SOCKET_ERROR) {
  136. printf("send failed with error: %d\n", WSAGetLastError());
  137. closesocket(ClientSocket);
  138. WSACleanup();
  139. return 1;
  140. }
  141. }
  142.  
  143.  
  144. printf("Bytes sent: %d\n", iSendResult);
  145. }
  146. else if (iResult == 0)
  147. printf("Connection closing...\n");
  148. else {
  149. printf("recv failed with error: %d\n", WSAGetLastError());
  150. closesocket(ClientSocket);
  151. WSACleanup();
  152. return 1;
  153. }
  154.  
  155. } while (iResult > 0);
  156.  
  157.  
  158.  
  159. // shutdown the connection since we're done
  160. iResult = shutdown(ClientSocket, SD_SEND);
  161. if (iResult == SOCKET_ERROR) {
  162. printf("shutdown failed with error: %d\n", WSAGetLastError());
  163. closesocket(ClientSocket);
  164. WSACleanup();
  165. return 1;
  166. }
  167.  
  168. // cleanup
  169. closesocket(ClientSocket);
  170. WSACleanup();
  171.  
  172.  
  173. return 0;
  174. }
  175.  
  176.  
  177. // Uruchomienie programu: Ctrl + F5 lub menu Debugowanie > Uruchom bez debugowania
  178. // Debugowanie programu: F5 lub menu Debugowanie > Rozpocznij debugowanie
  179.  
  180. // Porady dotyczące rozpoczynania pracy:
  181. // 1. Użyj okna Eksploratora rozwiązań, aby dodać pliki i zarządzać nimi
  182. // 2. Użyj okna programu Team Explorer, aby nawiązać połączenie z kontrolą źródła
  183. // 3. Użyj okna Dane wyjściowe, aby sprawdzić dane wyjściowe kompilacji i inne komunikaty
  184. // 4. Użyj okna Lista błędów, aby zobaczyć błędy
  185. // 5. Wybierz pozycję Projekt > Dodaj nowy element, aby utworzyć nowe pliki kodu, lub wybierz pozycję Projekt > Dodaj istniejący element, aby dodać istniejące pliku kodu do projektu
  186. // 6. Aby w przyszłości ponownie otworzyć ten projekt, przejdź do pozycji Plik > Otwórz > Projekt i wybierz plik sln
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement