Advertisement
Magnus9998

ServerClass

Aug 22nd, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. #define DEFAULT_PORT "27015"
  2.  
  3. #ifndef _WIN32_WINNT
  4. #define _WIN32_WINNT 0x501 //For getaddr function
  5. #endif
  6. #ifndef WIN32_LEAN_AND_MEAN
  7. #define WIN32_LEAN_AND_MEAN
  8. #endif
  9.  
  10. #include <cstring>
  11. #include <winsock2.h>
  12. #include <ws2tcpip.h>
  13. #include <iphlpapi.h>
  14. #include <windows.h>
  15. #include <windowsx.h>
  16. #include <tchar.h>
  17. #include "ServerClass.h"
  18.  
  19. /*SOCKET ListenSocket = INVALID_SOCKET;
  20. SOCKET ClientSocket = INVALID_SOCKET;
  21. char recvbufx[DEFAULT_BUFLEN];
  22. int recvbuflen;*/
  23.     int ServerClass::CreateServer(HWND hwindz)
  24.     {
  25.         struct addrinfo *result = NULL, *ptr = NULL, hints;
  26.         //int iResult, iSendResult;
  27.         //int recvbuflen = DEFAULT_BUFLEN;
  28.         WSADATA wsaData;
  29.  
  30.         if(WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
  31.         {
  32.             MessageBox(hwindz, "Network error: Could not initialize socket library!", "Error", MB_OK | MB_ICONERROR);
  33.             return 1;
  34.         }
  35.  
  36.         ZeroMemory(&hints, sizeof (hints));
  37.         hints.ai_family = AF_INET;
  38.         hints.ai_socktype = SOCK_STREAM;
  39.         hints.ai_protocol = IPPROTO_TCP;
  40.         hints.ai_flags = AI_PASSIVE;
  41.  
  42.         // Resolve the local address and port to be used by the server
  43.         if (getaddrinfo(NULL, DEFAULT_PORT, &hints, &result) != 0) {
  44.             MessageBox(hwindz, "Network error: Could not resolve the server address!", "Error", MB_OK | MB_ICONERROR);
  45.             WSACleanup();
  46.             return 1;
  47.         }
  48.  
  49.         // Create a SOCKET for the server to listen for client connections
  50.  
  51.         ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
  52.  
  53.         if (ListenSocket == INVALID_SOCKET) {
  54.             MessageBox(hwindz, "Error creating socket!", "Error", MB_OK | MB_ICONERROR);
  55.             //WSAGetLastError()
  56.             freeaddrinfo(result);
  57.             WSACleanup();
  58.             return 1;
  59.         }
  60.  
  61.  
  62.         // Setup the TCP listening socket
  63.         if (bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen) == SOCKET_ERROR) {
  64.             MessageBox(hwindz, "Could not bind the socket!", "Error", MB_OK | MB_ICONERROR);
  65.             freeaddrinfo(result);
  66.             closesocket(ListenSocket);
  67.             WSACleanup();
  68.             return 1;
  69.         }
  70.  
  71.         freeaddrinfo(result);
  72.  
  73.         if ( listen( ListenSocket, SOMAXCONN ) == SOCKET_ERROR ) {
  74.             MessageBox(hwindz, "Could not listen to the socket!", "Error", MB_OK | MB_ICONERROR);
  75.             closesocket(ListenSocket);
  76.             WSACleanup();
  77.             return 1;
  78.         }
  79.  
  80.         ClientSocket = INVALID_SOCKET;
  81.  
  82.         ClientSocket = accept(ListenSocket, NULL, NULL);
  83.         if (ClientSocket == INVALID_SOCKET) {
  84.             MessageBox(hwindz, "Refuses to connect!", "Error", MB_OK | MB_ICONERROR);
  85.             closesocket(ListenSocket);
  86.             WSACleanup();
  87.             return 1;
  88.         }
  89.  
  90.         return 0;
  91.     }
  92.  
  93.     int ServerClass::CloseServer()
  94.     {
  95.         closesocket(ClientSocket);
  96.         WSACleanup();
  97.         return 0;
  98.     }
  99.  
  100.     char* ServerClass::getcbuffer()
  101.     {
  102.         return recvbufx;
  103.     }
  104.  
  105.     static int ServerClass::listensock()
  106.     {
  107.         if(recv(ClientSocket, recvbufx, recvbuflen, 0) > 0)
  108.         {
  109.             return recvbuflen;
  110.         }
  111.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement