• Welcome to Valhalla Legends Archive.
 

WaitForMultipleObjects

Started by Eli_1, June 21, 2004, 12:49 PM

Previous topic - Next topic

Eli_1

I'm in a really big hurry to leave for a doctors appointment. I'll clean this post up when I get back if no one responds. The bot crashes _seemingly_ right after WaitForMultipleObjects returns. If anyone can figure out why, please post.


#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <conio.h>

// defines
#define BOT_COUNT 8

// prototypes
DWORD WINAPI recvthread(LPVOID);
DWORD WINAPI inthread(LPVOID);
void setbot(char *, char *, char *, int);
void bconnect(char *, int);
void bclose(int);
void bconnected(int);

// global variables
int holdref;
HANDLE hevents[BOT_COUNT];
HANDLE hinthread;
HANDLE recvhandle[BOT_COUNT];
SOCKET sckbot[BOT_COUNT];
DWORD recvret;
char* busername[BOT_COUNT];
char* bpassword[BOT_COUNT];
char* bhome[BOT_COUNT];
bool isconnected[BOT_COUNT];
bool isrunning = true;

// structs
struct sockaddr_in sin;


int main() {
   hinthread = CreateThread(NULL, 0, inthread, &holdref, 0, &recvret);

   WSAData wsdata;
   WSAStartup(MAKEWORD(2, 0), &wsdata);
   
   setbot("aD", "kdf", "hideout", 0);
   setbot("ASDF", "kdf", "hideout", 1);
   setbot("sdff", "dfk", "hideout", 2);
   setbot("ddff", "dsfk", "hideout", 3);

   setbot("dde", "dfk", "dark", 4);
   setbot("fdff", "2ssk", "dark", 5);
   setbot("43d3d", "k3d", "dark", 6);
   setbot("dsfff8", "k3d", "dark", 7);



   for (int i = 0; i < BOT_COUNT; i++) {
      char tmp[64];
      sprintf(tmp, "DisconnectEvent%d", i);
      hevents[i] = CreateEvent(NULL, TRUE, FALSE, tmp);
   }


   printf("Connecting... ");
   for (int i =0; i < 1; i++) { bconnect("66.197.163.168", i); }
   

   while (isrunning == true) {
      int retval = WaitForMultipleObjects(8, hevents, FALSE, INFINITE);
      retval -= WAIT_OBJECT_0;
      
      // disconnect events
      if (retval < 8) {
         ResetEvent(hevents[retval]);
         CloseHandle(recvhandle[retval]);
         closesocket(sckbot[retval]);
         bconnect("66.197.163.168", retval);
      }
   }

   for (int i = 0; i < BOT_COUNT; i++) {
      closesocket(sckbot[i]);
      CloseHandle(recvhandle[i]);
   }
   CloseHandle(hinthread);
   WSACleanup();
   return 0;
}













void bconnect(char *server, int index) {
   sckbot[index] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

   sin.sin_family = AF_INET;
   sin.sin_port = htons(6112);
   sin.sin_addr.s_addr = inet_addr(server);

   int i = connect(sckbot[index], (sockaddr *)&sin, sizeof(sin));
   if (i == SOCKET_ERROR)
      SetEvent(hevents[index]);
   else
      bconnected(index);
}







void bconnected(int index) {
   printf("Connected!\n");
   isconnected[index] = true;

   char login[256];
   sprintf(login, "\3\4%s\r\n%s\r\n/join %s\r\n\0", busername[index], bpassword[index], bhome[index]);
   printf("Logging in!\n");
   send(sckbot[index], login, strlen(login), 0);
   
   printf("Creating receive thread (%d)... ", index);
   holdref = index;
   recvhandle[index] = CreateThread(NULL, 0, recvthread, &holdref, 0, &recvret);
   printf("Created!\n");
}





void setbot(char *username, char *pass, char *home, int index) {
   busername[index] = username;
   bpassword[index] = pass;
   bhome[index] = home;
}







DWORD WINAPI recvthread(LPVOID index) {
   char buffer[1024];
   int i = *(int*)index;
   int bytesrecv = 1;


   while (bytesrecv > 0) {
      bytesrecv = recv(sckbot[i], buffer, 1024, 0);
      buffer[bytesrecv] = '\0';

      char *message = strtok(buffer, "\r\n");
      while (message != NULL) {
         if (stricmp(message, "LoGiN FaiLeD.") == 0) {
            printf("Failed login!\n");
            SetEvent(hevents[i]);
            return 0;
         }
         message = strtok(NULL, "\r\n");
      }
   }
   SetEvent(hevents[i]);
   return 0;
}






DWORD WINAPI inthread(LPVOID un_used) {
   char buffer[1024];
   
   while (1) {
      fgets(buffer, 1024, stdin);
      buffer[strlen(buffer) - 1] = '\0';
   
      if (stricmp(buffer, "/close") == 0)
         isrunning = false;
      else {
         for (int i = 0; i < BOT_COUNT; i++) {
            strcat(buffer, "\r\n");
            if (isconnected[i] == true) { send(sckbot[i], buffer, strlen(buffer), 0); }
         }
      }
   }
   return 0;
}

Adron


Eli_1

A error message box will pop up saying it has "performed an illegal operation" and will close when I click ok.

Banana fanna fo fanna


UserLoser.

Maybe a bit OT but:  Is there any advantage of using CreateThread() over _beginthread()?

Eibro

Quote from: UserLoser. on June 23, 2004, 09:14 PM
Maybe a bit OT but:  Is there any advantage of using CreateThread() over _beginthread()?
No, but note:
QuoteA thread in an executable that is linked to the static C run-time library (CRT) should use _beginthread and _endthread for thread management rather than CreateThread and ExitThread. Failure to do so results in small memory leaks when the thread calls ExitThread. Another work around is to link the executable to the CRT in a DLL instead of the static CRT. Note that this memory leak only occurs from a DLL if the DLL is linked to the static CRT and a thread calls the DisableThreadLibraryCalls function. Otherwise, it is safe to call CreateThread and ExitThread from a thread in a DLL that links to the static CRT.
Eibro of Yeti Lovers.