向在家讨教一个多线程的问题!
是一个用FOR开了一个较多线程的代码,传过去值老是会乱掉!大家看一下,代码运行的时候还会出错!
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock.h>
#include <iostream.h>
#define IP_NUM 5 // the number of the ip to scan
#define MAX_PORT 5000 // the max port number
#define THREAD_NUM 100 // the thread number
#define SEG_LEN 200 // the segment between min_port and max_port
typedef struct port_segment {
struct in_addr dest;
unsigned int min_port;
unsigned int max_port;
} port_segment;
/*
* @dest_addr: the destinite address
* DESC: how to scan the address
*/
int do_scan( struct sockaddr_in* dest_addr )
{
SOCKET sockfd;
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
// my_error( "socket failed" );
{
cout<<"socket failed"<<endl;
return -1;
}
/* connect with the sever */
if ( connect(sockfd, (struct sockaddr *)dest_addr, sizeof(struct sockaddr)) == 0 ) {
//close( sockfd );
closesocket(sockfd);
return -1;
}
else
printf( "%s : port (%d) has found.\n", inet_ntoa(dest_addr->sin_addr), ntohs(dest_addr->sin_port) );
return 1;
}
/*
* @arg: the pointer points to the dest address
* DESC: do the scan with every port
*/
DWORD WINAPI scan( void *arg )
{
port_segment port;
struct sockaddr_in* dest_addr = new sockaddr_in;
/* copy the struct to port */
memcpy( &port, arg, sizeof(struct port_segment) );
memset( dest_addr, 0, sizeof(struct sockaddr_in) );
dest_addr->sin_family = AF_INET;
dest_addr->sin_addr.s_addr = port.dest.s_addr;
//cout<<port.min_port<<"--"<<port.max_port<<endl;
for ( int i = port.min_port; i <= port.max_port; ++i ) {
dest_addr->sin_port = htons( i );
//cout<<dest_addr->sin_port<<endl;
cout<<i<<endl;
/* do the scan with every port */
// if ( do_scan(dest_addr) < 0 )
// continue;
}
// delete dest_addr;
return 0;
}
int main( int argc, char *argv[] )
{
HANDLE pThread;
struct in_addr dest_ip[ IP_NUM ]; // IP_NUM ip address
if ( argc < 2 ) {
fprintf( stderr, "usage: ./scan [ip1] [ip2] .. [ip5]\n" );
exit ( EXIT_FAILURE );
}
/* copy all the ip address into dest_ip */
for ( int i = 1; i < argc; ++i ) {
dest_ip[i-1].S_un.S_addr=inet_addr(argv[i]);
if(0==dest_ip)
{
cout<<"invalid ip address"<<endl;
return -1;
}
// if ( inet_aton(argv[i], &dest_ip[i - 1]) == 0 ) {
// fprintf( stderr, "invalid ip address.\n" );
// exit ( EXIT_FAILURE );
// }
}
/* malloc THREAD_NUM thread */
//thread = ( pthread_t * )malloc( THREAD_NUM * sizeof(pthread_t) );
for ( int j = 0; j < argc - 1; ++j ) {
port_segment* port= new port_segment;
for ( int i = 0; i < THREAD_NUM; ++i ) {
port_segment* port= new port_segment;
port->dest = dest_ip[ 0 ];
port->min_port = i * SEG_LEN + 1;
/* the last segment */
if (i==(THREAD_NUM - 1)||(port->min_port+SEG_LEN-1)>MAX_PORT)
{
port->max_port = MAX_PORT;
break;
}
else
port->max_port = port->min_port + SEG_LEN - 1;
// cout<<port->min_port<<"-"<<port->max_port<<endl;
/* create threads to scan the ports */
// if ( pthread_create(&thread[i], NULL, scan, (void *)&port) != 0 )
// my_error( "pthread_create failed" );
pThread=CreateThread(NULL,0,scan,port,0,NULL);
CloseHandle(pThread);
/* waiting for the sub threads exit */
//pthread_join( thread[i], NULL );
// delete port;
}
delete port;
}
/* free the memory */
Sleep(5000000);
fprintf( stderr, " sleep over\n" );
return 0;
}