Click here to Skip to main content
15,914,780 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
C++
#include <iostream>
#include<windows.h>
#include<winsock.h>
#include<winsock2.h>
#include<initguid.h>
#include<unistd.h>
#include<assert.h>
#include<malloc.h>
#include<thread>
#include<stdio.h>
//#include<pthread.h>
#include<boost/thread.hpp>
//#include<boost/thread/pthread/once.hpp>

void check_client(int argc, char** argv)
{
    int sockfd, conn_state;
    struct sockaddr_in serv_addr;
    struct hostent* serv;
    char* buff = (char*) malloc(sizeof(char*)*100);
    sockfd = socket(AF_INET,SOCK_STREAM,0);
    if (argc != 3)
    {
        printf("Not enough User Input!\n\n");
        //exit(0);
        //break ;

    }
    if( sockfd < 0 )
    {
        printf("[!] Error creating socket file descriptor!\n\n");
       // exit(0);
       //break ;
    }

    serv = gethostbyname(argv[1]);

    if( serv == NULL)
    {
        printf("[!] Error getting hostname's address!\n\n");
        //exit(0);
        //break ;
    }
   /* return 0 ;*/

          }

    void server_Side (int argc, char** argv)

    {

     int sockfd , bind_state , acc_state, client_len;
     struct sockaddr_in server_addr, client_addr;
     //char buff =(char*)malloc(sizeof(char*)*100);
      char* buff = (char*) malloc(sizeof(char*)*100);
     sockfd = socket(AF_INET, SOCK_STREAM,0);
     if(sockfd<0)
     {
         printf("[!] error creating socket file! \n\n");
         //exit(0);
         //break ;
     }
    printf("waiting for connection");
    listen(sockfd,5);
    client_len= sizeof(client_addr);
    acc_state = accept(sockfd, (struct sockaddr*)&client_addr , &client_len);
    printf("acc_state : %d \n\n", acc_state);
    if(acc_state<0)
    {
        printf("[!] error accepting the coming connection from cliebt \n\n");
        //exit(0);
       // break ;
    }
     printf("communication established successfully with client\n");
     printf("[~] wiating any incoming buffer ... \n\n");
     read(acc_state , buff , 100+1);
     printf("[s] thanks for connecting", sizeof("[s] thanks again ")+1);

    close(sockfd);
   /* return 0;*/
   }


   using namespace std ;
//int main (int argc, char ** argv) {
int main () {
    using namespace boost ;
    char *cv , *cd , *argv ;
    int argc ;
    /*thread_1 = boost.thread ();
    thread_2 = boost.thread();
   boost.thread thread_1 = boost.thread (check_client , int argc , char** argv);
   boost.thread thread_2 = boost.thread (server_Side, int argc , char** argv);*/


     boost :: thread thread_1 = thread (server_Side, argc, &&argv);
    boost :: thread thread_2 = thread(check_client,  argc, &&argv);


   server_Side(4,&cd);
   check_client (4,&cv);

   thread_1.join();
   thread_2.join();


    return 0;
}






     /*std :: thread first (check_client,int argc,char** argv);
     std :: thread second (server_Side,int argc,char** argv);
     std :: THREAD_PRIORITY_IDLE (server_Side,int argc,char** argv);*/
    /* std :: THREAD_PRIORITY_IDLE ;
     first  = THREAD_PRIORITY_IDLE (check_client,int argc,char** argv);
     std :: THREAD_PRIORITY_IDLE; second = THREAD_PRIORITY_IDLE (server_Side,int argc,char** argv);*/
    /* std :: THREAD_ALL_ACCESS first (check_client,int argc,char** argv);
     std :: THREAD_ALL_ACCESS second (server_Side,int argc, char** argv);*/


What I have tried:

i tried everything and i wrote them at the end of the code but non worked with me
Posted
Updated 14-May-16 14:52pm

1 solution

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

In first, put a breakpoint on very first line executed and check that variables meet your expectations. Then executed line by line.
Weird, your code contain very basic mistakes on things that you should have mastered long before turning to threads.
 
Share this answer
 
v2
Comments
Member 12523913 14-May-16 21:07pm    
actually i lack this one .. but i have another request if possible
could u redirect me to a client server code to understand it ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900