Created
May 28, 2016 08:32
-
-
Save yamaguchi1024/9a9449b2fcf31900c1cf291de3049161 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <winsock2.h> | |
#include<windows.h> | |
#include<string.h> | |
#include<tchar.h> | |
int State;//状態 | |
int X;//何分の平均を取るか | |
void kansoku(HANDLE hsemaphore[]) { | |
BYTE NewKeyState[256],OldKeyState[256]; | |
memset(NewKeyState,0,sizeof(NewKeyState)); | |
int Count=0; | |
int t=X; | |
while(true){ | |
Start: | |
Count=0; | |
for(int k=0;k<t;k++){ | |
for(int i=0;i<200;i++){ //this is one minute | |
Sleep(300); | |
WaitForSingleObject(hsemaphore[0], INFINITE); | |
if(t!=X){ | |
t=X; | |
ReleaseSemaphore(hsemaphore[0], 1, NULL); | |
goto Start; | |
} | |
ReleaseSemaphore(hsemaphore[0], 1, NULL); | |
for(int i=0;i<256;i++){ | |
NewKeyState[i] = GetKeyState(i); | |
if(NewKeyState[i]!=OldKeyState[i]){ | |
Count++; | |
} | |
OldKeyState[i]=NewKeyState[i]; | |
} | |
} | |
} | |
Count=Count/t; | |
WaitForSingleObject(hsemaphore[1], INFINITE); | |
if(Count<20) State=1; | |
else if(Count>=20 && Count<80) State=2; | |
else if(Count>=80) State=3; | |
else State=0; | |
ReleaseSemaphore(hsemaphore[1], 1, NULL); | |
} | |
return; | |
} | |
int tsushin(LPVOID arg) { | |
WSADATA wsaData; | |
WSAStartup(MAKEWORD(2, 0), &wsaData); | |
SOCKET s; | |
SOCKADDR_IN name, client; | |
HANDLE *hsemaphore = (HANDLE *)arg; | |
int client_addr = sizeof(SOCKADDR); | |
s = socket(AF_INET, SOCK_STREAM, 0); | |
name.sin_family = AF_INET; | |
name.sin_port = htons(2000); | |
name.sin_addr.s_addr = inet_addr("192.168.10.5"); | |
connect(s, (SOCKADDR *)&name, sizeof(name)); | |
while(true){ | |
char q[3];//改行含めて3文字にしてね!Xが10を超えたい時は、アスキーコード的に9のあとにあるやつにしてね!! | |
int nBytesRecv =recv(s, q, sizeof(q), 0); | |
if(nBytesRecv == SOCKET_ERROR){ | |
printf("サーバからの受信失敗です\n"); | |
printf("エラー%dが発生しました\n", WSAGetLastError()); | |
break; | |
} // 受信終わり | |
if(q[0]=='T'){ | |
if(q[1]!='0'){ | |
WaitForSingleObject(hsemaphore[0], INFINITE); | |
X=q[1]-'0'; | |
ReleaseSemaphore(hsemaphore[0], 1, NULL); | |
char OK[4]="OK\n"; | |
send(s,OK,3,0); | |
} | |
}else if(q[0]=='S'){ | |
char status[3]; | |
WaitForSingleObject(hsemaphore[1], INFINITE); | |
status[0]='S'; | |
status[1]=State+'0'; | |
status[2]='\n'; | |
ReleaseSemaphore(hsemaphore[1], 1, NULL); | |
send(s,status,3,0); | |
} | |
} | |
closesocket(s); | |
WSACleanup(); | |
return 0; | |
} | |
int main(void) { | |
HANDLE hthread,hsemaphore[2]; | |
DWORD tid; | |
hsemaphore[0] = CreateSemaphore(NULL, 1, 1, "SEMA_X"); | |
hsemaphore[1] = CreateSemaphore(NULL, 1, 1, "SEMA_State"); | |
State=0; | |
X=5; | |
hthread = CreateThread(NULL, 0,(LPTHREAD_START_ROUTINE)tsushin, (LPVOID)hsemaphore, 0, &tid); | |
kansoku(hsemaphore); | |
CloseHandle(hthread); | |
CloseHandle(hsemaphore[0]); | |
CloseHandle(hsemaphore[1]); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment