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

 lsp_set_drop_rate(0.5);  
  if(argc == 2)
    {
//    srand(12345);

    char *host_port = argv[1];

    char host[20];
    char port[7];
    bool flg=false;
    int j=0;
    int k=0;
    int ii=0;
    memset(host,0,20);
    memset(port,0,7);

    for(;ii<strlen(argv[1]);ii++)
    {
      if(host_port[ii] == ':')
         flg=true;
      if(flg==true && host_port[ii]!=':')
          port[j++]=host_port[ii];
      if(flg == false && host_port[ii]!=':')
          host[k++]=host_port[ii];

      //        printf("%d %d %d\n",ii,j,k);
    }


        
    lsp_client* myworker = lsp_client_create(host,atoi(port));
     if (myworker==NULL)
	{
	  printf("Worker not created\n");
     	  return true;
      }
     char message[] ="j";
    lsp_client_write(myworker, (void *) message, strlen(message));
    for(;;)
    {
         //lsp_client_write(myworker, (void *h) message, strlen(message));
         uint8_t buffer[4096];
         memset(buffer,0,4096);
         int bytes_read = lsp_client_read(myworker,buffer);
         sleep(2);
	//printf("Received bytes %d \n",bytes_read);
         if(bytes_read >0 )
         {
            //printf("Received bytes %d \n",bytes_read);
	    if(buffer[0]!='c')
		continue;
	    handle_req(buffer,bytes_read);
            lsp_client_write(myworker, (void *) global_pld, strlen(global_pld));
         }
         else if (bytes_read<0) {
             printf("Disconnected\n");
             break;
         }
    
    }

    lsp_client_close(myworker);
    //    printf("End of worker code\n");
    
    }
    else
    {
       printf("\nPlease enter correct number of arguments\n");
    }
    return 0;
} 
int main(int argc, char *argv[])
{
    srand(12345);
    
    if(argc<4)
    {
        printf("Input format should be host:port hash pswd_len\n");
        exit(1);
    }
    char* arg_buf = malloc(128);
    strcpy(arg_buf,argv[1]);
    char* host = malloc(64);
    char* phost = host;
    char* str_port = malloc(64);
    
    char* p = arg_buf;

    while(*p != ':')
        *phost++ = *p++;
    *phost = '\0';

    strcpy(str_port,strstr(arg_buf,":")+1);
    int port = atoi(str_port);
    char* hash = malloc(64);
    strcpy(hash,argv[2]);
    int len = atoi(argv[3]);
      
    lsp_set_epoch_lth(2);
    lsp_set_epoch_cnt(5);
    lsp_set_drop_rate(0.01);
    
    /////////
    //int port = SERVER_PORT;
    //////////////
    lsp_client* client = lsp_client_create(host, port);
    if(!client)
    {
        printf("Can't connect to server.\n");
        return 1;
    }

    uint8_t buf[1024] = {0};

    for(int i=0;i<5;i++)
    {    
        sprintf(buf,"ca\n%d\n%s", len, hash);
        
        if(!lsp_client_write(client, buf, strlen(buf)))
        {
            printf("Can't send message.\n");
            lsp_client_close(client);
            client = lsp_client_create(host, port);
            continue;          
        }
        else
        {    
            uint8_t* result = malloc(MAX_MSG_SIZE);
            if(lsp_client_read(client, result)==-1)
            {
                printf("Can't recv message.\n");
                lsp_client_close(client);
                client = lsp_client_create(SERVER_NAME, port);
                if(!client)
                {
                    printf("Can't connect to server.\n");
                    return 1;
                }
                continue;
            }
                              
            if ( strlen(result) > HASH_PWD_SIZE )
            {
                printf("Error: Message Exceed Max Buffer Size!");
                continue;
            }

            if(result[0] == 'f')
            {
                char* pos1 = strstr(result,"\n");
                char* pos2 = strstr(pos1 + 1,"\n");
                char* pos3 = strstr(pos2 + 1,"\n");
                char* hash = malloc(40);
                char* pwd = malloc(16);
                memset(pwd,0,16);
                memset(hash,0,40);
                memcpy(hash, pos2 + 1, (pos3-1) - (pos2+1) +1);
                memcpy(pwd,pos3 + 1,len);
                pwd[len] = '\0';

                printf("Found: %s \n",pwd);
                return 0;
            }
            else if(result[0] == 'x')
            {
                printf("Not Found\n",result);  
                return;
            }
            else if(result[0] == 'n')
            {
                printf("Server is not ready! Please retry later!\n");
                return;
            }
           
        }
    }
    printf("Crack fail. Please retry! \n");
}