Jump to content
Fórum Script Brasil
  • 0

[HELP] THREAD


SenKiLL

Question

Olá estou com um problema com um programa multi thread, ele possui uma thread para o socket(multi cliente) e outra para alterar a memoria de um determinado processo, porém o programa só executa o thread do socket...

segue abaixo um trecho do codigo.

#include <iostream>
#include <winsock2.h>
#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>
#include <conio.h>
#include <ctime>
#include <cstdlib> 
#include <string>
#include <shellapi.h>


using namespace std;


void WINAPI ThreadUm(void);
void WINAPI ThreadDois(void);

BYTE newdata[4]; 
DWORD newdatasize = sizeof(newdata);


int player = 0;
int ip = 0;

DWORD WINAPI receive_cmds(LPVOID);  

int socket()
{

DWORD thread;
  
WSADATA data;
SOCKET winsock;
SOCKADDR_IN sock;

printf("Server Mult-threaded connected\n\n");

if((WSAStartup(0x101, &data)) == SOCKET_ERROR)
{
  printf("\nerro ao iniciar socket");
  getch();
  exit(1);
}


sock.sin_family = AF_INET;
sock.sin_addr.s_addr = INADDR_ANY;
sock.sin_port=htons(8888);
  
if((winsock = socket(AF_INET, SOCK_STREAM, 0)) == SOCKET_ERROR)
{
     printf("\nerro ao criar socket");
     getch();
     exit(1);
}
  
if(bind(winsock,(SOCKADDR*)&sock,sizeof(sock)) == SOCKET_ERROR)
{
     printf("\nerro ao acessar rede");
     getch();
     exit(1);
}
  
if(listen(winsock, 5) != 0)
{
    printf("\nerro ao criar lista de socket");
    getch();
    exit(1);
}

SOCKET client;

SOCKADDR_IN from;
int fromlen = sizeof(from);
  
while(true)
{
  client = accept(winsock,(struct sockaddr*)&from, &fromlen);
  
  ip = (int)inet_ntoa(from.sin_addr);
  printf("ip: %s\n", ip);
  
CreateThread(NULL, 0, receive_cmds, (LPVOID)client, 0, &thread);  //thread
}

closesocket(winsock);
WSACleanup();

return 0;
}

DWORD WINAPI receive_cmds(LPVOID lpParam)
{
  
//continuação do socket

}




BOOL SetPrivilege(LPSTR type) // more flexible
{
HANDLE Htoken;
TOKEN_PRIVILEGES tokprivls;
if(!OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &Htoken)){
                      return 0;
                      }
tokprivls.PrivilegeCount = 1;
LookupPrivilegeValue(NULL, type, &tokprivls.Privileges[0].Luid);
tokprivls.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
BOOL Success =AdjustTokenPrivileges( Htoken, FALSE, &tokprivls, sizeof(tokprivls), NULL, NULL);
CloseHandle(Htoken);
return Success;

}// end 



int rarray() 
{ 

 srand((unsigned)time(0)); 
     
    for(int i=0; i<sizeof(newdata); i++){ 
         
        newdata[i]=(rand()%100)+1; 

 } 

}// end 


DWORD WINAPI ThreadUm(LPVOID process_name)
//int program(char process_name[32])
{    

    //edição da memoria de um programa

    return 0;    // THE END 
       
}// end 



int main(int argc, char* argv[]){

    SetConsoleTitle("Monitor"); // title console
    printf("////");
    
    socket();


DWORD id[4];
HANDLE th[4];



th[0] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadUm,(char*)"exe.exe",0,&id[0]);
th[1] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadUm,(char*)"exe.exe",0,&id[1]);
th[2] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadUm,(char*)"exe.exe",0,&id[2]);

if(th[0] == 0 || th[1] == 0 || th[2] == 0)
 {
 MessageBox(0,"Ocorreu um erro ao criar as Threads","Erro",0x10);
 return 1;
 }




WaitForMultipleObjects(4,th,FALSE,INFINITE);

    system("pause");
}//end

Diminui um pouco o codigo porque estava extenso para postar.

alguém saberia o que fazer?

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...