Advertisement
Guest User

asdasd

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