Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. SOCKET ConnectSocket, AcceptSocket;
  2. sockaddr_in Local;
  3.  
  4. struct addrinfo *result = NULL;
  5. struct addrinfo hints;
  6. int iResult;
  7.  
  8. WSAData data;
  9. ConnectSocket = NULL;
  10. AcceptSocket = NULL;
  11.  
  12. if (!AfxSocketInit(&data))
  13. {
  14. SendSysMessage((char *)"winsock initialise failed");
  15. return;
  16. }
  17.  
  18. Local.sin_family = AF_INET;
  19. Local.sin_addr.s_addr = INADDR_ANY;
  20. Local.sin_port = htons(NPort);
  21.  
  22. ZeroMemory(&hints, sizeof(hints));
  23. hints.ai_family = AF_INET;
  24. hints.ai_socktype = SOCK_STREAM;
  25. hints.ai_protocol = IPPROTO_TCP;
  26. hints.ai_flags = AI_PASSIVE;
  27.  
  28. // Resolve the server address and port
  29. char NPortStr[10];
  30. _ltoa(NPort, NPortStr, 10);
  31. iResult = getaddrinfo(NULL, NPortStr, &hints, &result);
  32. if (iResult != 0) {
  33. SendSysMessage((char *)"getaddrinfo failed");
  34. WSACleanup();
  35. return ;
  36. }
  37.  
  38. while (nServiceRunning)
  39. {
  40. ConnectSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
  41. if (ConnectSocket == INVALID_SOCKET) {
  42. SendSysMessage((char *)"socket failed");
  43. freeaddrinfo(result);
  44. WSACleanup();
  45. return;
  46. }
  47.  
  48. // Setup the TCP listening socket
  49. iResult = bind(ConnectSocket, result->ai_addr, (int)result->ai_addrlen);
  50. if (iResult == SOCKET_ERROR) {
  51. SendSysMessage((char *)"bind failed");
  52. freeaddrinfo(result);
  53. closesocket(ConnectSocket);
  54. WSACleanup();
  55. return;
  56. }
  57.  
  58.  
  59. iResult = listen(ConnectSocket, SOMAXCONN);
  60. if (iResult == SOCKET_ERROR) {
  61. SendSysMessage((char *)"listen failed");
  62. closesocket(ConnectSocket);
  63. WSACleanup();
  64. return;
  65. }
  66.  
  67. AcceptSocket = accept(ConnectSocket, NULL,NULL);
  68. if (AcceptSocket == INVALID_SOCKET) {
  69. SendSysMessage((char *)"accept failed");
  70. closesocket(ConnectSocket);
  71. WSACleanup();
  72. return;
  73. }
  74.  
  75. DWORD dwThreadId;
  76. CreateThread(NULL, 0, ProcessClient, (LPVOID)AcceptSocket, 0, &dwThreadId);
  77.  
  78. if (ConnectSocket)
  79. {
  80. closesocket(ConnectSocket); // allow next connection
  81. ConnectSocket = NULL;
  82. }
  83. }
  84. WSACleanup();
  85. freeaddrinfo(result);
  86.  
  87. DWORD WINAPI ProcessClient(LPVOID lpParameter)
  88. {
  89. SOCKET ClientSocket = (SOCKET)lpParameter;
  90.  
  91. do {
  92. int Count = 0;
  93. memset(cmdstr, 0, 501);
  94. do
  95. {
  96. iResult = DBXInt.Receive(&cmdstr[Count], cmdstrlen, 0);
  97. if (iResult >= 0)
  98. {
  99. Count += iResult;
  100. }
  101. } while (cmdstr[Count - 1] != 0x0d && iResult > 0);
  102.  
  103. /*
  104.  
  105. code to decode message removed
  106.  
  107. */
  108. } while (!Error && iResult > 0);
  109.  
  110. shutdown(ClientSocket, SD_BOTH);
  111. closesocket(ClientSocket);
  112. ClientSocket = NULL;
  113.  
  114. CoUninitialize();
  115. AfxEndThread(0);
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement