Advertisement
Guest User

Server

a guest
Oct 23rd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.25 KB | None | 0 0
  1. #include <ws2tcpip.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <WinSock2.h>
  5.  
  6. #define DEFAULT_BUFLEN 1024
  7. #define DEFAULT_PORT "27016"
  8. #define SERVER_SLEEP_TIME 50
  9.  
  10. bool InitializeWindowsSockets();
  11. int Recieve(SOCKET socket, char *recvbuf, int size);
  12. int Send(SOCKET socket, char*recvbuf, int size);
  13. int Select(SOCKET socket, char funkcija);
  14.  
  15. int  main(void)
  16. {
  17.     // Socket used for listening for new clients
  18.     SOCKET listenSocket = INVALID_SOCKET;
  19.     // Socket used for communication with client
  20.     SOCKET acceptedSocket = INVALID_SOCKET;
  21.     // variable used to store function return value
  22.     int iResult;
  23.     // Buffer used for storing incoming data
  24.     char recvbuf[DEFAULT_BUFLEN];
  25.    
  26.     if(InitializeWindowsSockets() == false)
  27.     {
  28.         // we won't log anything since it will be logged
  29.         // by InitializeWindowsSockets() function
  30.         return 1;
  31.     }
  32.    
  33.     // Prepare address information structures
  34.     addrinfo *resultingAddress = NULL;
  35.     addrinfo hints;
  36.  
  37.     memset(&hints, 0, sizeof(hints));
  38.     hints.ai_family = AF_INET;       // IPv4 address
  39.     hints.ai_socktype = SOCK_STREAM; // Provide reliable data streaming
  40.     hints.ai_protocol = IPPROTO_TCP; // Use TCP protocol
  41.     hints.ai_flags = AI_PASSIVE;     //
  42.  
  43.     // Resolve the server address and port
  44.     iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &resultingAddress);
  45.     if ( iResult != 0 )
  46.     {
  47.         printf("getaddrinfo failed with error: %d\n", iResult);
  48.         WSACleanup();
  49.         return 1;
  50.     }
  51.  
  52.     // Create a SOCKET for connecting to server
  53.     listenSocket = socket(AF_INET,      // IPv4 address famly
  54.                           SOCK_STREAM,  // stream socket
  55.                           IPPROTO_TCP); // TCP
  56.  
  57.     if (listenSocket == INVALID_SOCKET)
  58.     {
  59.         printf("socket failed with error: %ld\n", WSAGetLastError());
  60.         freeaddrinfo(resultingAddress);
  61.         WSACleanup();
  62.         return 1;
  63.     }
  64.  
  65.     // Setup the TCP listening socket - bind port number and local address
  66.     // to socket
  67.     iResult = bind( listenSocket, resultingAddress->ai_addr, (int)resultingAddress->ai_addrlen);
  68.     if (iResult == SOCKET_ERROR)
  69.     {
  70.         printf("bind failed with error: %d\n", WSAGetLastError());
  71.         freeaddrinfo(resultingAddress);
  72.         closesocket(listenSocket);
  73.         WSACleanup();
  74.         return 1;
  75.     }
  76.  
  77.     // Set socket to nonblocking mode
  78.     unsigned long int nonBlockingMode = 1;
  79.     iResult = ioctlsocket(listenSocket, FIONBIO, &nonBlockingMode);
  80.  
  81.     if (iResult == SOCKET_ERROR)
  82.     {
  83.         printf("ioctlsocket failed with error: %ld\n", WSAGetLastError());
  84.         return 1;
  85.     }
  86.  
  87.  
  88.     // Since we don't need resultingAddress any more, free it
  89.     freeaddrinfo(resultingAddress);
  90.  
  91.     // Set listenSocket in listening mode
  92.     iResult = listen(listenSocket, SOMAXCONN);
  93.     if (iResult == SOCKET_ERROR)
  94.     {
  95.         printf("listen failed with error: %d\n", WSAGetLastError());
  96.         closesocket(listenSocket);
  97.         WSACleanup();
  98.         return 1;
  99.     }
  100.  
  101.     printf("Server initialized, waiting for clients.\n");
  102.  
  103.     do
  104.     {
  105.         // Wait for clients and accept client connections.
  106.         // Returning value is acceptedSocket used for further
  107.         // Client<->Server communication. This version of
  108.         // server will handle only one client.
  109.         Select(listenSocket, 'r');
  110.  
  111.         acceptedSocket = accept(listenSocket, NULL, NULL);
  112.  
  113.         if (acceptedSocket == INVALID_SOCKET)
  114.         {
  115.             printf("accept failed with error: %d\n", WSAGetLastError());
  116.             closesocket(listenSocket);
  117.             WSACleanup();
  118.             return 1;
  119.         }
  120.  
  121.         unsigned long int nonBlockingMode = 1;
  122.         iResult = ioctlsocket(acceptedSocket, FIONBIO, &nonBlockingMode);
  123.  
  124.         if (iResult == SOCKET_ERROR)
  125.         {
  126.             printf("ioctlsocket failed with error: %ld\n", WSAGetLastError());
  127.             return 1;
  128.         }
  129.  
  130.         iResult = Recieve(acceptedSocket, recvbuf, DEFAULT_BUFLEN);
  131.  
  132.     } while (1);
  133.  
  134.     // shutdown the connection since we're done
  135.     iResult = shutdown(acceptedSocket, SD_SEND);
  136.     if (iResult == SOCKET_ERROR)
  137.     {
  138.         printf("shutdown failed with error: %d\n", WSAGetLastError());
  139.         closesocket(acceptedSocket);
  140.         WSACleanup();
  141.         return 1;
  142.     }
  143.  
  144.     // cleanup
  145.     closesocket(listenSocket);
  146.     closesocket(acceptedSocket);
  147.     WSACleanup();
  148.  
  149.     return 0;
  150. }
  151.  
  152. bool InitializeWindowsSockets()
  153. {
  154.     WSADATA wsaData;
  155.     // Initialize windows sockets library for this process
  156.     if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
  157.     {
  158.         printf("WSAStartup failed with error: %d\n", WSAGetLastError());
  159.         return false;
  160.     }
  161.     return true;
  162. }
  163.  
  164. int Recieve(SOCKET socket, char *recvbuf, int size) {
  165.  
  166.     int iResult;
  167.     int pristigliBajtovi = 0;
  168.     bool firstTime = true;
  169.     int brojBajtovaZaPrimanje;
  170.  
  171.     // clientAddress will be populated from recvfrom
  172.     sockaddr_in clientAddress;
  173.     memset(&clientAddress, 0, sizeof(sockaddr_in));
  174.  
  175.     // set whole buffer to zero
  176.     memset(recvbuf, 0, DEFAULT_BUFLEN);
  177.    
  178.     do
  179.     {
  180.         Select(socket, 'r');
  181.         iResult = recv(socket, (recvbuf+pristigliBajtovi), size-pristigliBajtovi, 0);
  182.         if (iResult < 4)
  183.         {
  184.             pristigliBajtovi += iResult;
  185.             continue;
  186.         }
  187.         if (firstTime)
  188.         {
  189.  
  190.         }
  191.         pristigliBajtovi += iResult;
  192.  
  193.     } while (pristigliBajtovi < size);
  194.  
  195.     return iResult;
  196. }
  197.  
  198. int Select(SOCKET socket, char funkcija) {
  199.     int iResult;
  200.     do
  201.     {
  202.         // Initialize select parameters
  203.         FD_SET set;
  204.         timeval timeVal;
  205.  
  206.         FD_ZERO(&set);
  207.         // Add socket we will wait to read from
  208.         FD_SET(socket, &set);
  209.  
  210.         // Set timeouts to zero since we want select to return
  211.         // instantaneously
  212.         timeVal.tv_sec = 0;
  213.         timeVal.tv_usec = 0;
  214.  
  215.         if (funkcija == 'r')
  216.         {
  217.             iResult = select(0 /* ignored */, &set, NULL, NULL, &timeVal);
  218.         }
  219.         else
  220.         {
  221.             iResult = select(0 /* ignored */, NULL, &set, NULL, &timeVal);
  222.         }
  223.  
  224.         // lets check if there was an error during select
  225.         if (iResult == SOCKET_ERROR)
  226.         {
  227.             fprintf(stderr, "select failed with error: %ld\n", WSAGetLastError());
  228.             continue;
  229.         }
  230.  
  231.         // now, lets check if there are any sockets ready
  232.         if (iResult == 0)
  233.         {
  234.             // there are no ready sockets, sleep for a while and check again
  235.             Sleep(SERVER_SLEEP_TIME);
  236.             continue;
  237.         }
  238.     } while (iResult <= 0);
  239.     return 1;
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement