Advertisement
Guest User

Server

a guest
Jan 17th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. #define WIN32_LEAN_AND_MEAN
  2.  
  3. #include <windows.h>
  4. #include <winsock2.h>
  5. #include <ws2tcpip.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include "conio.h"
  9.  
  10. #pragma comment (lib, "Ws2_32.lib")
  11. #pragma comment (lib, "Mswsock.lib")
  12. #pragma comment (lib, "AdvApi32.lib")
  13.  
  14. #define SERVER_PORT 27016
  15. #define BUFFER_SIZE 256
  16.  
  17. // TCP server that use blocking sockets
  18. int main()
  19. {
  20. // Socket used for listening for new clients
  21. SOCKET listenSocket = INVALID_SOCKET;
  22.  
  23. // Socket used for communication with client
  24. SOCKET acceptedSocket = INVALID_SOCKET;
  25. int n=0;
  26. // Variable used to store function return value
  27. int iResult;
  28.  
  29. // Buffer used for storing incoming data
  30. char dataBuffer[BUFFER_SIZE];
  31.  
  32. // WSADATA data structure that is to receive details of the Windows Sockets implementation
  33. WSADATA wsaData;
  34.  
  35. // Initialize windows sockets library for this process
  36. if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
  37. {
  38. printf("WSAStartup failed with error: %d\n", WSAGetLastError());
  39. return 1;
  40. }
  41.  
  42.  
  43. // Initialize serverAddress structure used by bind
  44. sockaddr_in serverAddress;
  45. memset((char*)&serverAddress,0,sizeof(serverAddress));
  46. serverAddress.sin_family = AF_INET; // IPv4 address family
  47. serverAddress.sin_addr.s_addr = INADDR_ANY; // Use all available addresses
  48. serverAddress.sin_port = htons(SERVER_PORT); // Use specific port
  49.  
  50.  
  51. // Create a SOCKET for connecting to server
  52. listenSocket = socket(AF_INET, // IPv4 address family
  53. SOCK_STREAM, // Stream socket
  54. IPPROTO_TCP); // TCP protocol
  55.  
  56. // Check if socket is successfully created
  57. if (listenSocket == INVALID_SOCKET)
  58. {
  59. printf("socket failed with error: %ld\n", WSAGetLastError());
  60. WSACleanup();
  61. return 1;
  62. }
  63.  
  64. // Setup the TCP listening socket - bind port number and local address to socket
  65. iResult = bind(listenSocket,(struct sockaddr*) &serverAddress,sizeof(serverAddress));
  66.  
  67.  
  68. // Check if socket is successfully binded to address and port from sockaddr_in structure
  69. if (iResult == SOCKET_ERROR)
  70. {
  71. printf("bind failed with error: %d\n", WSAGetLastError());
  72. closesocket(listenSocket);
  73. WSACleanup();
  74. return 1;
  75. }
  76.  
  77. unsigned long mode = 1; //non-blocking mode
  78. iResult = ioctlsocket(listenSocket, FIONBIO, &mode);
  79.  
  80. // Set listenSocket in listening mode
  81. iResult = listen(listenSocket, SOMAXCONN);
  82. if (iResult == SOCKET_ERROR)
  83. {
  84. printf("listen failed with error: %d\n", WSAGetLastError());
  85. closesocket(listenSocket);
  86. WSACleanup();
  87. return 1;
  88. }
  89.  
  90. printf("Server socket is set to listening mode. Waiting for new connection requests.\n");
  91.  
  92.  
  93.  
  94. do
  95. {
  96. // Struct for information about connected client
  97. sockaddr_in clientAddr;
  98.  
  99. int clientAddrSize = sizeof(struct sockaddr_in);
  100.  
  101. // Accept new connections from clients
  102. acceptedSocket = accept(listenSocket, (struct sockaddr *)&clientAddr, &clientAddrSize);
  103.  
  104. // Check if accepted socket is valid
  105. if (acceptedSocket == INVALID_SOCKET)
  106. {
  107. printf("accept failed with error: %d\n", WSAGetLastError());
  108. closesocket(listenSocket);
  109. WSACleanup();
  110. return 1;
  111. }
  112.  
  113. printf("\nNew client request accepted. Client address: %s : %d\n", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port));
  114.  
  115. do
  116. {
  117. // Receive data until the client shuts down the connection
  118. iResult = recv(acceptedSocket, dataBuffer, BUFFER_SIZE, 0);
  119.  
  120. if (iResult != SOCKET_ERROR) {
  121. // Neblokirajuca operacija koja je uspesno izvrsena
  122. dataBuffer[iResult] = '\0';
  123.  
  124. // Log message text
  125. printf("Client sent: %s.\n", dataBuffer);
  126. n=0;
  127. }
  128. else
  129. {
  130. if (WSAGetLastError() == WSAEWOULDBLOCK) {
  131. printf("Cekam.Broj pokusaja je:%d\n",n);
  132. n++;
  133. Sleep(1000);
  134.  
  135. // U pitanju je blokirajuca operacija koja zbog rezima
  136. // soketa nece biti izvrsena
  137. }
  138. else {
  139. return 0;
  140. // Desila se neka druga greska prilikom poziva operacije
  141. }
  142. }
  143. if(n==15){
  144. break;
  145. }
  146. /*if (iResult > 0) // Check if message is successfully received
  147. {
  148.  
  149.  
  150. }
  151. else if (iResult == 0) // Check if shutdown command is received
  152. {
  153. // Connection was closed successfully
  154. printf("Connection with client closed.\n");
  155. closesocket(acceptedSocket);
  156. }
  157. else // There was an error during recv
  158. {
  159.  
  160. printf("recv failed with error: %d\n", WSAGetLastError());
  161. closesocket(acceptedSocket);
  162. }*/
  163.  
  164. }while(iResult > 0);
  165.  
  166. // Here is where server shutdown loguc could be placed
  167.  
  168. } while (true);
  169.  
  170. // Shutdown the connection since we're done
  171. iResult = shutdown(acceptedSocket, SD_BOTH);
  172.  
  173. // Check if connection is succesfully shut down.
  174. if (iResult == SOCKET_ERROR)
  175. {
  176. printf("shutdown failed with error: %d\n", WSAGetLastError());
  177. closesocket(acceptedSocket);
  178. WSACleanup();
  179. return 1;
  180. }
  181.  
  182. //Close listen and accepted sockets
  183. closesocket(listenSocket);
  184. closesocket(acceptedSocket);
  185.  
  186. // Deinitialize WSA library
  187. WSACleanup();
  188.  
  189. return 0;
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement