Hi i am new to C++ I am have a error like the one above:
on a linux working in the termial
commands
c++ round.cpp -lpthread -o moo.exe -- build file
./moo.exe -- running file

get
*** glibc detected *** ./moo.exe: free(): invalid pointer: 0xb7f226ec ***
then shows
======= Backtrace: =========
stuff
======= Memory map: ========
stuff
Aborted

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <errno.h>
#include <dirent.h>
#include <sys/types.h>
#include <cstring>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <pthread.h>
using namespace std;
#define MAX_THREAD 65535
  void *scanipport (void *arg);
  void *ping (void *arg);

  typedef
      struct info  {
        string ips	;
        int   startport;         //start port
        int    endport;         //end port
     } scout;

  // typedef
      // struct
     // {
        // int id;
        // int nproc;
     // }  parm;

   void *ping (void *arg)
  {
     scout *p = (scout *) arg;
     int i =0;
     static string bohica;
     bohica = p[i].ips;
     cout << "131.204.36.xxx subnet" << endl;
     int please = system (("ping " + bohica+" -c 3 -W .0005 -w .0005").c_str());
  }

   void *scanipport (void *arg)
  {
     int   sd;         //socket descriptor
     int   start;         //start port
     int    end;         //end port
     int    rval;         //socket descriptor for connect
     char    responce[1024];      //to receive data
     char   *message="shell";       //data to send
     struct hostent *hostaddr;   //To be used for IPaddress
     struct sockaddr_in servaddr;   //socket structure
     static string bohica;
     int port;
     const char* host;
     int s = 0;
     scout *p = (scout *) arg;
     bohica = p[s].ips;
     start = p[s].startport;
     end   = p[s].endport;

     host = bohica.c_str();
     port = start;
     sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); //created the tcp socket
     if (sd == -1)
     {
        perror("Socket()\n");
        cout<<errno<<endl;
     }
     memset( &servaddr, 0, sizeof(servaddr));
     servaddr.sin_family = AF_INET;
     servaddr.sin_port = htons(port); //set the port #
     hostaddr = gethostbyname( host ); //get the ip 1st argument
    // memcpy(&servaddr.sin_addr, hostaddr->h_addr, hostaddr->h_length);
        //connects to the ip in hostaddr
     rval = connect(sd, (struct sockaddr *) &servaddr, sizeof(servaddr));
     if (rval == -1)
     {
              //printf("Port %d is closed\n", port);
        close(sd);
     }
     else
        printf("Port %d is open\n",port);

     close(sd);         //socket descriptor

  }


   int main(int argc, char **argv)
  {
     int   start;         //start port
     int    end;         //end port
     static string bohica;
     static string host;
     string ip[255];
  // creates array of the ips to scan a see if alive  
     ip[0]="131.204.36.0";   	ip[1]="131.204.36.10";    ip[2]="131.204.36.20"; 	ip[3]="131.204.36.30";		ip[4]="131.204.36.1";    	ip[5]="131.204.36.11"; 	
// these used for testing add the rest of range in after code works
     int i =0;
     int s=0;

     scout *p;

     pthread_t *threads;
     pthread_attr_t pthread_custom_attr;

     threads=(pthread_t *)malloc(end*sizeof(*threads));
     pthread_attr_init(&pthread_custom_attr);


     bohica = argv[1];
     start = atoi(argv[2]);
     end   = atoi(argv[3]);

     p[s].ips = bohica;
     p[s].startport = start;
     p[s].endport = end;

     if (argc > 1 )
     {
        for (i=0; i<end; i++)
        {
           pthread_create(&threads[i], &pthread_custom_attr, scanipport, (void *)(p));
           start++;
           p[s].startport = start;
        }

     /* Synchronize the completion of each thread. */

        for (i=0; i<end; i++)
        {
           pthread_join(threads[i],NULL);
        }
        //free(p);
        if (end >= 65536)
           cout << "There are only 65535 ports"<<endl;
     }
        // pthreads creation one for each ip
     else
     {
        for(int gh=0; gh<256; gh++) // this does ip range of 131.204.36.0 – 131.204.36.255
        {
           host = ip[gh];
           p[s].ips = host;
           pthread_create(&threads[gh], &pthread_custom_attr, ping, (void *)(p));
        }
        for (int kt=0; kt<256; kt++)
        {
           pthread_join(threads[kt],NULL);
        }
        //free(p);
     }

     return 0;
  }

Recommended Answers

All 15 Replies

Well I see in your main() function scout *p; and then

p[s].ips = bohica;
 p[s].startport = start;
 p[s].endport = end;

But I don't see you allocating memory for p anywhere ? You need to to do that.

Ok thank you I missed that but it didn't fix my problem I change the section of code in the main after the array to:

bohica = argv[1];
     start = atoi(argv[2]);
     end   = atoi(argv[3]);


     int i =0;
     int s=0;

     scout *p;

     pthread_t *threads;
     pthread_attr_t pthread_custom_attr;

     threads=(pthread_t *)malloc(end*sizeof(*threads));
     pthread_attr_init(&pthread_custom_attr);
		p=(scout *)malloc(sizeof(scout)*end);



     p[s].ips = bohica;
     p[s].startport = start;
     p[s].endport = end;

> bohica = argv[1];
Your log shows NO command line parameters, not 3 like you assume here.
Always check argc before looking at argv

> for (int kt=0; kt<256; kt++)
Woah, first you ignore the input, now you assume a specific number.

scout *p;
p = new scout; //allocate the memory for a new scout struct

Ok first like I said I am new to C++, second I thank you all for the help, and third as to the input in the command the program runs with it or without it and I get the same problem. I have made a few changes so I putting the copy of the code in and yes I am still getting the same problem. Oh before I forget thanks for all the help.

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <errno.h>
#include <dirent.h>
#include <sys/types.h>
#include <cstring>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <pthread.h>

   using namespace std;
#define MAX_THREAD 65535
   void *scanipport (void *arg);
   void *ping (void *arg);

   typedef
       struct info  {
         string ips	;
         int   startport;         //start port
         int    endport;         //end port
      } scout;

    void *ping (void *arg)
   {
      scout *p = (scout *) arg;
      int i =0;
      static string bohica;
      bohica = p[i].ips;
      cout << "131.204.36.xxx subnet" << endl;
      int please = system (("ping " + bohica+" -c 3 -W .0005 -w .0005").c_str());
   }

    void *scanipport (void *arg)
   {
      int   sd;         //socket descriptor
      int   start;         //start port
      int    end;         //end port
      int    rval;         //socket descriptor for connect
      char    responce[1024];      //to receive data
      char   *message="shell";       //data to send
      struct hostent *hostaddr;   //To be used for IPaddress
      struct sockaddr_in servaddr;   //socket structure
      static string bohica;
      int port;
      const char* host;
      int s = 0;
      scout *p = (scout *) arg;
      bohica = p[s].ips;
      start = p[s].startport;
      end   = p[s].endport;
      host = bohica.c_str();
      port = start;
      sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); //created the tcp socket
      if (sd == -1)
      {
         perror("Socket()\n");
         cout<<errno<<endl;
      }
      memset( &servaddr, 0, sizeof(servaddr));
      servaddr.sin_family = AF_INET;
      servaddr.sin_port = htons(port); //set the port #
      hostaddr = gethostbyname( host ); //get the ip 1st argument
    // memcpy(&servaddr.sin_addr, hostaddr->h_addr, hostaddr->h_length);
        //connects to the ip in hostaddr
      rval = connect(sd, (struct sockaddr *) &servaddr, sizeof(servaddr));
      if (rval == -1)
      {
        //printf("Port %d is closed\n", port);
         close(sd);
      }
      else
         printf("Port %d is open\n",port);
   
      close(sd);         //socket descriptor
   
   }


    int main(int argc, char **argv)
   {
      int   start;         //start port
      int    end;         //end port
      static string bohica;
      static string host;
      string ip[255];
   // creates array of the ips to scan a see if alive
      ip[0]="131.204.36.0";   	ip[1]="131.204.36.1";    	ip[2]="131.204.36.2";   	ip[3]="131.204.36.3";   	ip[4]="131.204.36.4";   	ip[5]="131.204.36.5";   	
   
      if (argc > 1 )
      {
         bohica = argv[1];
         start = atoi(argv[2]);
         end   = atoi(argv[3]);
      
         int i =0;
         int s=0;
         scout *p;
         pthread_t *threads;
         pthread_attr_t pthread_custom_attr;
         threads=(pthread_t *)malloc(end*sizeof(*threads));
         pthread_attr_init(&pthread_custom_attr);
         p=(scout *)malloc(sizeof(scout)*end);
         p[s].ips = bohica;
         p[s].startport = start;
         p[s].endport = end;
      
         for (i=0; i<end; i++)
         {
            pthread_create(&threads[i], &pthread_custom_attr, scanipport, (void *)(p));
            start++;
            p[s].startport = start;
         }
      
      /* Synchronize the completion of each thread. */
      
         for (i=0; i<end; i++)
         {
            pthread_join(threads[i],NULL);
         }
        //free(p);
         if (end >= 65536)
            cout << "There are only 65535 ports"<<endl;
      }
        // pthreads creation one for each ip
      else
      {
         int i =0;
         int s=0;
         int spec = 256;
         scout *p;
      
         pthread_t *threads;
         pthread_attr_t pthread_custom_attr;
      
         threads=(pthread_t *)malloc(end*sizeof(*threads));
         pthread_attr_init(&pthread_custom_attr);
         p=(scout *)malloc(sizeof(scout)*spec);
      
         for(int gh=0; gh<256; gh++) // this does ip range of 131.204.36.0 – 131.204.36.255
         {
            host = ip[gh];
            p[s].ips = host;
            pthread_create(&threads[gh], &pthread_custom_attr, ping, (void *)(p));
         }
         for (int kt=0; kt<256; kt++)
         {
            pthread_join(threads[kt],NULL);
         }
        //free(p);
      }
   
      return 0;
   }

I have made a few changes so I putting the copy of the code in and yes I am still getting the same problem.

Rather use new instead of malloc() for allocating the scout structures (because scout contains a std::string member variable)

> if (argc > 1 )
Still doesn't do much for the safety of argv[2] and [3].

> Ok first like I said I am new to C++
Then try it without threads first. There's a whole bunch of mistakes in there that I'm not even going to explain to you.
First understand exactly how a single threaded solution works.

And you're STILL running off the end of an array which is supposed to be limited by user input.

I did it without threads first that code works it just takes to a few hours to run.

Whole bunch of //!! comments

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <errno.h>
#include <dirent.h>
#include <sys/types.h>
#include <cstring>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <pthread.h>

using namespace std;
#define MAX_THREAD 65535
void *scanipport (void *arg);
void *ping (void *arg);

//!! typedef (here) is unnecessary
typedef
struct info  {
    string  ips;
    int     startport;       //start port
    int     endport;         //end port
} scout;

void *ping (void *arg)
{
    scout *p = (scout *) arg;
    int i =0;
    static string bohica;   //!! again, static in a thread - see below
    bohica = p[i].ips;
    cout << "131.204.36.xxx subnet" << endl;
    int please = system (("ping " + bohica+" -c 3 -W .0005 -w .0005").c_str());
}

void *scanipport (void *arg)
{
    int     sd;                 //socket descriptor
    int     start;              //start port
    int     end;                //end port
    int     rval;               //socket descriptor for connect
    char    responce[1024];     //to receive data
    char   *message="shell";    //data to send
    struct hostent *hostaddr;   //To be used for IPaddress
    struct sockaddr_in servaddr;//socket structure
    static string bohica;       
    //!! why static?  Especially since you only have ONE OF THEM
    //!! across all the threads.
    int     port;
    const char* host;
    int     s = 0;

    scout *p = (scout *) arg;
    bohica = p[s].ips;
    start = p[s].startport;
    end   = p[s].endport;
    host = bohica.c_str();  //!! host = p[s].ips.c_str(); would save confusion with bohica
    port = start;

    sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); //created the tcp socket
    if (sd == -1)
    {
        perror("Socket()\n");
        cout<<errno<<endl;
    }
    memset( &servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(port); //set the port #
    hostaddr = gethostbyname( host ); //get the ip 1st argument
    // memcpy(&servaddr.sin_addr, hostaddr->h_addr, hostaddr->h_length);
    //connects to the ip in hostaddr
    rval = connect(sd, (struct sockaddr *) &servaddr, sizeof(servaddr));
    if (rval == -1)
    {
        //printf("Port %d is closed\n", port);
        close(sd);
    }
    else
    printf("Port %d is open\n",port);   //!! use C++

    close(sd);         //socket descriptor

}


int main(int argc, char **argv)
{
    int     start;          //start port
    int     end;            //end port
    static string bohica;
    static string host;
    string ip[255];

    // creates array of the ips to scan a see if alive
    ip[0]="131.204.36.0";   	
    ip[1]="131.204.36.1";    	
    ip[2]="131.204.36.2";   	
    ip[3]="131.204.36.3";   	
    ip[4]="131.204.36.4";   	
    ip[5]="131.204.36.5";   	
    //!! What about the rest of them?
    
    if (argc > 1 )
    {
        bohica = argv[1];
        start = atoi(argv[2]);
        end   = atoi(argv[3]);
        //!! argc is not sufficient to protect these assignments
        
        int i =0;
        int s=0;
        scout *p;
        pthread_t *threads;
        pthread_attr_t pthread_custom_attr;

        //!! call new, not malloc, then you don't need these horrible casts
        threads=(pthread_t *)malloc(end*sizeof(*threads));
        pthread_attr_init(&pthread_custom_attr);
        p=(scout *)malloc(sizeof(scout)*end);   //!! you MUST call new for this one, to construct the string ips

        p[s].ips = bohica;
        p[s].startport = start;
        p[s].endport = end;
        
        for (i=0; i<end; i++)
        {
            //!! each thread gets the SAME instance information in p
            //!! (void*)&p[i] would seem to make more sense.
            pthread_create(&threads[i], &pthread_custom_attr, scanipport, (void *)(p));
            start++;
            p[s].startport = start;
            //!! except s isn't incremented here?
            //!! should it be?
        }
        
        /* Synchronize the completion of each thread. */
        
        for (i=0; i<end; i++)
        {
            pthread_join(threads[i],NULL);
        }
        //free(p);
        if (end >= 65536)
        cout << "There are only 65535 ports"<<endl;
    }
    // pthreads creation one for each ip
    else
    {
        int i =0;
        int s=0;
        int spec = 256;
        scout *p;
        
        pthread_t *threads;
        pthread_attr_t pthread_custom_attr;

        //!! previous notes about calling new
        //!! end is random garbage in this branch of the code.
        threads=(pthread_t *)malloc(end*sizeof(*threads));
        pthread_attr_init(&pthread_custom_attr);
        p=(scout *)malloc(sizeof(scout)*spec);

        //!! use the limit count, not a magic number
        for(int gh=0; gh<256; gh++) // this does ip range of 131.204.36.0 – 131.204.36.255
        {
            host = ip[gh];  //!! did you initialise all 256 ?
            p[s].ips = host;
            //!! again, is it p or &p[gh] ?
            //!! remember, your thread can't see your loop variable.
            pthread_create(&threads[gh], &pthread_custom_attr, ping, (void *)(p));
        }
        //!! ditto with this one
        for (int kt=0; kt<256; kt++)
        {
            pthread_join(threads[kt],NULL);
        }
        //!! you should be using delete [] here
        //free(p);
    }

    return 0;
}

OK I went over the code and with the notes that was give above and clean up the code good news I fix the error now I get a Segmentation fault when I run in gdb I get
(ip address works fine with the non threaded version)
when given gdb moo.exe ip address 1 100
Attaching to program: home/moo.exe, process 131
ptrace: No such process.
/home/131.204.36.1: No such file or directory.
when given gdb moo.exe
Starting program: /home/moo.exe
Failed to read a valid object file image from memory.
[Thread debugging using libthread_db enabled]
[New Thread -1211676976 (LWP 3870)]

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1211676976 (LWP 3870)]
0xb7ce14ca in memcpy () from /lib/libc.so.6

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <errno.h>
#include <dirent.h>
#include <sys/types.h>
#include <cstring>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <pthread.h>

   using namespace std;
#define MAX_THREAD 65535
   void *scanipport (void *arg);
   void *ping (void *arg);

   typedef
       struct info  {
         string ips	;
         int   startport;         //start port
         int    endport;         //end port
      } scout;

    void *ping (void *arg)
   {
   
      scout *p ;
      p = new scout;  //allocate the memory for a new scout struct     
      int i =0;
      static string bohica;
      bohica = p[i].ips;
      cout << "131.204.36.xxx subnet" << endl;
      int please = system (("ping " + bohica+" -c 3 -W .0005 -w .0005").c_str());
   }

    void *scanipport (void *arg)
   {
      int   sd;         //socket descriptor
      int   start;         //start port
      int    rval;         //socket descriptor for connect
      char    responce[1024];      //to receive data
      char   *message="shell";       //data to send
      struct hostent *hostaddr;   //To be used for IPaddress
      struct sockaddr_in servaddr;   //socket structure
      scout *p ;
      p = new scout;  //allocate the memory for a new scout struct    
      int port;
      const char* host;
      int s = 0;
      start = p[s].startport;
      host = p[s].ips.c_str();
      port = start;
      sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); //created the tcp socket
      if (sd == -1)
      {
         perror("Socket()\n");
         cout<<errno<<endl;
      }
      memset( &servaddr, 0, sizeof(servaddr));
      servaddr.sin_family = AF_INET;
      servaddr.sin_port = htons(port); //set the port #
      hostaddr = gethostbyname( host ); //get the ip 1st argument
      memcpy(&servaddr.sin_addr, hostaddr->h_addr, hostaddr->h_length);
        //connects to the ip in hostaddr
      rval = connect(sd, (struct sockaddr *) &servaddr, sizeof(servaddr));
      if (rval == -1)
      {
        //printf("Port %d is closed\n", port);
         close(sd);
      }
      else
         printf("Port %d is open\n",port);
      close(sd);         //socket descriptor
   }


    int main(int argc, char **argv)
   {
      int   start;         //start port
      int    end;         //end port
      static string bohica;
      static string host;
      string ip[255];
   // creates array of the ips to scan a see if alive
      ip[0]="131.204.36.0";   	ip[10]="131.204.36.10";    ip[20]="131.204.36.20"; 	ip[30]="131.204.36.30";		ip[1]="131.204.36.1";    	ip[11]="131.204.36.11"; 	ip[21]="131.204.36.21"; 	ip[31]="131.204.36.31";		ip[2]="131.204.36.2";   	ip[12]="131.204.36.12"; 	ip[22]="131.204.36.22"; 	ip[32]="131.204.36.32";		ip[3]="131.204.36.3";   	ip[13]="131.204.36.13"; 	ip[23]="131.204.36.23"; 	ip[33]="131.204.36.33";
      ip[4]="131.204.36.4";   	ip[14]="131.204.36.14"; 	ip[24]="131.204.36.24"; 	ip[34]="131.204.36.34";		ip[5]="131.204.36.5";   	ip[15]="131.204.36.15"; 	ip[25]="131.204.36.25"; 	ip[35]="131.204.36.35";		ip[6]="131.204.36.6";   	ip[16]="131.204.36.16";  	ip[26]="131.204.36.26";	   ip[36]="131.204.36.36";		ip[7]="131.204.36.7";   	ip[17]="131.204.36.17";	   ip[27]="131.204.36.27"; 	ip[37]="131.204.36.37";
      ip[8]="131.204.36.8";   	ip[18]="131.204.36.18";	   ip[28]="131.204.36.28"; 	ip[38]="131.204.36.38";		ip[9]="131.204.36.9";   	ip[19]="131.204.36.19";	   ip[29]="131.204.36.29"; 	ip[39]="131.204.36.39";		ip[40]="131.204.36.40"; 	ip[50]="131.204.36.50";	   ip[60]="131.204.36.60"; 	ip[70]="131.204.36.70";		ip[41]="131.204.36.41"; 	ip[51]="131.204.36.51";	   ip[61]="131.204.36.61"; 	ip[71]="131.204.36.71";
      ip[42]="131.204.36.42"; 	ip[52]="131.204.36.52";	   ip[62]="131.204.36.62"; 	ip[72]="131.204.36.72";		ip[43]="131.204.36.43"; 	ip[53]="131.204.36.53";	   ip[63]="131.204.36.63"; 	ip[73]="131.204.36.73";		ip[44]="131.204.36.44"; 	ip[54]="131.204.36.54";	   ip[64]="131.204.36.64"; 	ip[74]="131.204.36.74";		ip[45]="131.204.36.45"; 	ip[55]="131.204.36.55";	   ip[65]="131.204.36.65"; 	ip[75]="131.204.36.75";
      ip[46]="131.204.36.46"; 	ip[56]="131.204.36.56";	   ip[66]="131.204.36.66"; 	ip[76]="131.204.36.76";		ip[47]="131.204.36.47"; 	ip[57]="131.204.36.57";	   ip[67]="131.204.36.67"; 	ip[77]="131.204.36.77";		ip[48]="131.204.36.48";	   ip[58]="131.204.36.58";	   ip[68]="131.204.36.68"; 	ip[78]="131.204.36.78";		ip[49]="131.204.36.49"; 	ip[59]="131.204.36.59";	   ip[69]="131.204.36.69"; 	ip[79]="131.204.36.79";
      ip[80]="131.204.36.80"; 	ip[90]="131.204.36.90";	   ip[100]="131.204.36.100";	ip[110]="131.204.36.110";	ip[81]="131.204.36.81"; 	ip[91]="131.204.36.91";	   ip[101]="131.204.36.101";	ip[111]="131.204.36.111";	ip[82]="131.204.36.82"; 	ip[92]="131.204.36.92";	   ip[102]="131.204.36.102";	ip[112]="131.204.36.112";	ip[83]="131.204.36.83"; 	ip[93]="131.204.36.93";	   ip[103]="131.204.36.103";	ip[113]="131.204.36.113";
      ip[84]="131.204.36.84"; 	ip[94]="131.204.36.94";	   ip[104]="131.204.36.104";	ip[114]="131.204.36.114";	ip[85]="131.204.36.85"; 	ip[95]="131.204.36.95";	   ip[105]="131.204.36.105";	ip[115]="131.204.36.115";	ip[86]="131.204.36.86"; 	ip[96]="131.204.36.96";	   ip[106]="131.204.36.106";	ip[116]="131.204.36.116";	ip[87]="131.204.36.87"; 	ip[97]="131.204.36.97";	   ip[107]="131.204.36.107";	ip[117]="131.204.36.117";
      ip[88]="131.204.36.88"; 	ip[98]="131.204.36.98";	   ip[108]="131.204.36.108";	ip[118]="131.204.36.118";	ip[89]="131.204.36.89"; 	ip[99]="131.204.36.99"; 	ip[109]="131.204.36.109";	ip[119]="131.204.36.119";	ip[120]="131.204.36.120";	ip[130]="131.204.36.130";	ip[140]="131.204.36.140";	ip[150]="131.204.36.150";	ip[121]="131.204.36.121";	ip[131]="131.204.36.131";	ip[141]="131.204.36.141";	ip[151]="131.204.36.151";
      ip[122]="131.204.36.122";	ip[132]="131.204.36.132";	ip[142]="131.204.36.142";	ip[152]="131.204.36.152";	ip[123]="131.204.36.123";	ip[133]="131.204.36.133";	ip[143]="131.204.36.143";	ip[153]="131.204.36.153";	ip[124]="131.204.36.124";	ip[134]="131.204.36.134";	ip[144]="131.204.36.144";	ip[154]="131.204.36.154";	ip[125]="131.204.36.125";	ip[135]="131.204.36.135";	ip[145]="131.204.36.145";	ip[155]="131.204.36.155";
      ip[126]="131.204.36.126";	ip[136]="131.204.36.136";	ip[146]="131.204.36.146";	ip[156]="131.204.36.156";	ip[127]="131.204.36.127";	ip[137]="131.204.36.137";	ip[147]="131.204.36.147";	ip[157]="131.204.36.157";	ip[128]="131.204.36.128";	ip[138]="131.204.36.138";	ip[148]="131.204.36.148";	ip[158]="131.204.36.158";	ip[129]="131.204.36.129";	ip[139]="131.204.36.139";	ip[149]="131.204.36.149";	ip[159]="131.204.36.159";
      ip[160]="131.204.36.160";	ip[170]="131.204.36.170";	ip[180]="131.204.36.180";	ip[190]="131.204.36.190";	ip[161]="131.204.36.161";	ip[171]="131.204.36.171";	ip[181]="131.204.36.181";	ip[191]="131.204.36.191";	ip[162]="131.204.36.162";	ip[172]="131.204.36.172";	ip[182]="131.204.36.182";	ip[192]="131.204.36.192";	ip[163]="131.204.36.163";	ip[173]="131.204.36.173";	ip[183]="131.204.36.183";	ip[193]="131.204.36.193";
      ip[164]="131.204.36.164";	ip[174]="131.204.36.174";	ip[184]="131.204.36.184";	ip[194]="131.204.36.194";	ip[165]="131.204.36.165";	ip[175]="131.204.36.175";	ip[185]="131.204.36.185";	ip[195]="131.204.36.195";	ip[166]="131.204.36.166";	ip[176]="131.204.36.176";	ip[186]="131.204.36.186";	ip[196]="131.204.36.196";	ip[167]="131.204.36.167";	ip[177]="131.204.36.177";	ip[187]="131.204.36.187";	ip[197]="131.204.36.197";
      ip[168]="131.204.36.168";	ip[178]="131.204.36.178";	ip[188]="131.204.36.188";	ip[198]="131.204.36.198";	ip[169]="131.204.36.169";	ip[179]="131.204.36.179";	ip[189]="131.204.36.189";	ip[199]="131.204.36.199";	ip[200]="131.204.36.200";  ip[210]="131.204.36.210";	ip[220]="131.204.36.220";	ip[230]="131.204.36.230";	ip[201]="131.204.36.201";	ip[211]="131.204.36.211";	ip[221]="131.204.36.221";	ip[231]="131.204.36.231";
      ip[202]="131.204.36.202";	ip[212]="131.204.36.212";	ip[222]="131.204.36.222";	ip[232]="131.204.36.232";	ip[203]="131.204.36.203";	ip[213]="131.204.36.213";	ip[223]="131.204.36.223";	ip[233]="131.204.36.233";	ip[204]="131.204.36.204";	ip[214]="131.204.36.214";	ip[224]="131.204.36.224";	ip[234]="131.204.36.234";	ip[205]="131.204.36.205";	ip[215]="131.204.36.215";	ip[225]="131.204.36.225";	ip[235]="131.204.36.235";
      ip[206]="131.204.36.206";	ip[216]="131.204.36.216";	ip[226]="131.204.36.226";	ip[236]="131.204.36.236";	ip[207]="131.204.36.207";	ip[217]="131.204.36.217";	ip[227]="131.204.36.227";	ip[237]="131.204.36.237";	ip[208]="131.204.36.208";	ip[218]="131.204.36.218";	ip[228]="131.204.36.228";	ip[238]="131.204.36.238";	ip[209]="131.204.36.209";	ip[219]="131.204.36.219";	ip[229]="131.204.36.229";	ip[239]="131.204.36.239";
      ip[240]="131.204.36.240";	ip[246]="131.204.36.246";	ip[247]="131.204.36.247";	ip[248]="131.204.36.248";	ip[241]="131.204.36.241";	ip[249]="131.204.36.249";	ip[250]="131.204.36.250";	ip[253]="131.204.36.253";	ip[242]="131.204.36.242";	ip[251]="131.204.36.251";	ip[252]="131.204.36.252";	ip[254]="131.204.36.254";	ip[243]="131.204.36.243";	ip[244]="131.204.36.244";	ip[245]="131.204.36.245";	ip[255]="131.204.36.255";
   
      if (argc > 1 )
      {
         bohica = argv[1];
         start = atoi(argv[2]);
         end   = atoi(argv[3]);
         int i =0;
         int s=0;
         scout *p;
         p = new scout;  //allocate the memory for a new scout struct
         pthread_t *threads;
         pthread_attr_t pthread_custom_attr;
         threads = new pthread_t;
         pthread_attr_init(&pthread_custom_attr);
         p[s].ips = bohica; // stored in p[0]
         s++;
         p[s].startport = start; // stored in p[1]
         s++;
         for (i=0; i<end; i++)
         {
            pthread_create(&threads[i], &pthread_custom_attr, scanipport, (void *)(p));
            start++;
            p[s].startport = start;  // I want the start port to 
            s++;
         }
      /* Synchronize the completion of each thread. */
         for (i=0; i<end; i++)
         {
            pthread_join(threads[i],NULL);
         }
         delete [] p;
         if (end >= 65536)
            cout << "There are only 65535 ports"<<endl;
      }
        // pthreads creation one for each ip
      else
      {
         int i =0;
         int s=0;
         int spec = 256;
         scout *p;
         p = new scout;  //allocate the memory for a new scout struct
         pthread_t *threads;
         pthread_attr_t pthread_custom_attr;
         threads = new pthread_t;
         pthread_attr_init(&pthread_custom_attr);     
         for(int gh=0; gh<256; gh++) // this does ip range of 131.204.36.0 – 131.204.36.255
         {
            host = ip[gh];
            p[s].ips = host;
            pthread_create(&threads[gh], &pthread_custom_attr, ping, (void *)(p));
         }
         for (int kt=0; kt<256; kt++)
         {
            pthread_join(threads[kt],NULL);
         }
         delete [] p;
      }
      return 0;
   }

Again, there is only one memcpy() in your code, so I'll go with that

memcpy(&servaddr.sin_addr, hostaddr->h_addr, hostaddr->h_length);

your hostaddr is a pointer to the struct, which you declare earlier, but you haven't allocated it any space, nor have you stored an address in it, so what exactly are you trying to copy over here ? Neither of these variables hostaddr->h_addr or hostaddr->h_length point to a valid place.

Well I have that line commented out right now because I am was told that it wasn't need after my last post. But I am still getting Seg. Fault. I believe that the error has to do with the threading because the code without any of the threading works just fine the only problem is it take 3 hours to run moo.exe ip port port and 9 hours to run moo.exe

Your gdb error says that your program is crashing inside a memcpy somewhere. Now I don't see any other memcpy's in the code you posted, but you might want to switch to a single threaded run, and then step through the code, or put some debug statements to narrow down where it is crashing at.

Ok so I use gdb step line by line in the code without any of the threading information in it a i had no problems I got when I ran it without the ip and 2 ports when I run it with a ip and 2 ports it can't find the location but it work just fine in a normal run.

I'd suggest putting some print statements inside your scanipport function and that will help you pinpoint the line which is causing the error. Use thread_id() to print the thread#, so you can see which thread is causing it to crash.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.