c++ - To Stop server->how to verify if thread is stopped ->pthread -
c++ - To Stop server->how to verify if thread is stopped ->pthread -
main task -> stop server client has disconnected.
overview of problem: have big application , within application, have implemented little server code take connection , receive packets client. wanted long client connected , sending packets, server should not disconnect client has disconnected, server should stop running.
to achieve have created seperate thread receive packets client , thread automatically terminates client disconnect istself server.
now next target find out if detached thread has finished , if yes close "accept socket" ( below code nsk variable). accomplish how know detached process has finished? havn't got much info net. secondly, suggested utilize pthreadkill how find pid ?
can please bail me out .
static int sco_listen() { printf(" in sco_listen_function\n" ); struct sockaddr_sco addr; struct sco_conninfo conn; socklen_t optlen; int sk, nsk; char ba[18]; int *new_sock; /* create socket */ sk = socket(pf_bluetooth, sock_seqpacket, btproto_sco); if (sk < 0) { syslog(log_err, "can't create socket: %s (%d)", strerror(errno), errno); exit(1); } /* bind local address */ memset(&addr, 0, sizeof(addr)); addr.sco_family = af_bluetooth; bacpy(&addr.sco_bdaddr, &bdaddr); if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) { syslog(log_err, "can't bind socket: %s (%d)", strerror(errno), errno); goto error; } /* hear connections */ if (listen(sk, 10)) { syslog(log_err,"can not hear on socket: %s (%d)", strerror(errno), errno); goto error; } syslog(log_info,"waiting connection ..."); while (1) { memset(&addr, 0, sizeof(addr)); optlen = sizeof(addr); nsk = accept(sk, (struct sockaddr *) &addr, &optlen); if (nsk < 0) { syslog(log_err,"accept failed: %s (%d)", strerror(errno), errno); goto error; } if( fcntl( nsk, f_setfl, o_nonblock ) < 0 ) { printf(" failed set non blocking ...\n"); close( nsk ); goto error; } /* connection info */ memset(&conn, 0, sizeof(conn)); optlen = sizeof(conn); if (getsockopt(nsk, sol_sco, sco_conninfo, &conn, &optlen) < 0) { syslog(log_err, "can't sco connection information: %s (%d)", strerror(errno), errno); close(nsk); goto error; } ba2str(&addr.sco_bdaddr, ba); syslog(log_info, "connect %s [handle %d, class 0x%02x%02x%02x]", ba, conn.hci_handle, conn.dev_class[2], conn.dev_class[1], conn.dev_class[0]); pthread_t sniffer_thread; new_sock = malloc(1); *new_sock = nsk; if( pthread_create( &sniffer_thread, null, connection_handler, (void*)new_sock )< 0 ) { perror(" not create thread"); homecoming 1; } // here check if sniffer_thread running or not. //if siniffer thread not running close nsk? // , terminate server? puts (" handler assigned"); } homecoming 0 ; error: close(sk); exit(1); } void *connection_handler( void *socket_desc) { // socket descriptor int sock = *(int*)socket_desc; int read_size; char*message, message_client[ 200 ]; //receive messgae client while( read_size = recv( sock, message_client, 200, 0 ) > 0 ) { printf(" good\n"); } if( read_size == 0 ) { printf("clinet disconnected \n"); fflush( stdout); } else if( read_size == -1 ) { printf("received failed \n"); perror( " recv fialed"); } printf(" before memeory freee\n"); free( socket_desc ); homecoming 0; }
put entire server functionality single thread, both accept() , recv() loop. accept() ever called once, can homecoming once, 1 connection ever accepted. when client disconnects, (ie. recv() returns 0), clean closing listening socket , terminating thread. if connection half-closes, (eg. powerfulness fail @ client), thread may hang around forever, stuck on recv(), hey, cares? utilize keepalive close sockets , terminate thread, there's no real need - it's code , stack paged out, , it's orphaned , lost in space, (mehh..sob..).
oh - , rid of non_blocking. don't understand why set in first place? you're soaking 1 core next-to-nothing, except in case of half-close, in case you're soaking 1 core absolutely nothing.
c++ c pthreads
Comments
Post a Comment