Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SOCKET ConnectSocket, AcceptSocket;
- sockaddr_in Local;
- struct addrinfo *result = NULL;
- struct addrinfo hints;
- int iResult;
- WSAData data;
- ConnectSocket = NULL;
- AcceptSocket = NULL;
- if (!AfxSocketInit(&data))
- {
- SendSysMessage((char *)"winsock initialise failed");
- return;
- }
- Local.sin_family = AF_INET;
- Local.sin_addr.s_addr = INADDR_ANY;
- Local.sin_port = htons(NPort);
- ZeroMemory(&hints, sizeof(hints));
- hints.ai_family = AF_INET;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- hints.ai_flags = AI_PASSIVE;
- // Resolve the server address and port
- char NPortStr[10];
- _ltoa(NPort, NPortStr, 10);
- iResult = getaddrinfo(NULL, NPortStr, &hints, &result);
- if (iResult != 0) {
- SendSysMessage((char *)"getaddrinfo failed");
- WSACleanup();
- return ;
- }
- while (nServiceRunning)
- {
- ConnectSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
- if (ConnectSocket == INVALID_SOCKET) {
- SendSysMessage((char *)"socket failed");
- freeaddrinfo(result);
- WSACleanup();
- return;
- }
- // Setup the TCP listening socket
- iResult = bind(ConnectSocket, result->ai_addr, (int)result->ai_addrlen);
- if (iResult == SOCKET_ERROR) {
- SendSysMessage((char *)"bind failed");
- freeaddrinfo(result);
- closesocket(ConnectSocket);
- WSACleanup();
- return;
- }
- iResult = listen(ConnectSocket, SOMAXCONN);
- if (iResult == SOCKET_ERROR) {
- SendSysMessage((char *)"listen failed");
- closesocket(ConnectSocket);
- WSACleanup();
- return;
- }
- AcceptSocket = accept(ConnectSocket, NULL,NULL);
- if (AcceptSocket == INVALID_SOCKET) {
- SendSysMessage((char *)"accept failed");
- closesocket(ConnectSocket);
- WSACleanup();
- return;
- }
- DWORD dwThreadId;
- CreateThread(NULL, 0, ProcessClient, (LPVOID)AcceptSocket, 0, &dwThreadId);
- if (ConnectSocket)
- {
- closesocket(ConnectSocket); // allow next connection
- ConnectSocket = NULL;
- }
- }
- WSACleanup();
- freeaddrinfo(result);
- DWORD WINAPI ProcessClient(LPVOID lpParameter)
- {
- SOCKET ClientSocket = (SOCKET)lpParameter;
- do {
- int Count = 0;
- memset(cmdstr, 0, 501);
- do
- {
- iResult = DBXInt.Receive(&cmdstr[Count], cmdstrlen, 0);
- if (iResult >= 0)
- {
- Count += iResult;
- }
- } while (cmdstr[Count - 1] != 0x0d && iResult > 0);
- /*
- code to decode message removed
- */
- } while (!Error && iResult > 0);
- shutdown(ClientSocket, SD_BOTH);
- closesocket(ClientSocket);
- ClientSocket = NULL;
- CoUninitialize();
- AfxEndThread(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement