c - Constantly checking for shared memory to change while in a loop -


i have loop true allow other clients connect. problem having how check shared memory see if changes know when time shut down server? while loop put in works not let other clients connect after. issue in while(1) loop, has wait client connect before going through loop need checking shared memory not when client connects.

    #include <sys/types.h>     #include <netinet/in.h>     #include <sys/socket.h>     #include <errno.h>     #include <stdio.h>     #include <unistd.h>     #include <stdlib.h>     #include <strings.h>     #include <sys/ipc.h>     #include <sys/shm.h>     #include <sys/wait.h>      struct sockaddr_in sn;     struct sockaddr from;      int main(int ac, char** other){       int s, ns, sl, x = 22, pid;       char b[256];       key_t key=137;        int size=1;       int shmflg=0;       int id=0,ok=0;       char shmstr[10];       char istr[10];       int *shmptr;        size=size*sizeof(int);       shmflg=ipc_creat | shm_r | shm_w;       id=shmget(key,size,shmflg);       sprintf(shmstr,"%d",id);       shmptr=shmat(id,0,0);       shmptr[0]=0;        s=socket(af_inet, sock_stream,0);       if (s<0) {         printf("server socket error\n");          exit(0);}       else {}        bzero((char *)&sn, sizeof(sn));       sn.sin_family = af_inet;       sn.sin_port = htons(3311);        if (bind(s,(struct sockaddr *)&sn,sizeof(sn))==-1){         printf("server bind error %d\n",errno);         exit(0);}       else {}        listen(s,3);    ////////////////////////////////////****************************************       while (1){         sl=sizeof(from);         ns =accept(s,&from,&sl);         if (ns <0) {           printf("server accept error");           exit(0);}         else {}         sprintf(b,"%d",ns);         if ((pid=fork()) == 0) {           sprintf(istr, "%d",ok);           execlp(other[1], other[1], b, shmstr, istr, (char*) null);          }         else {           while(shmptr[0]==0){}//allows server shut down no more clients connect             close(s);             exit(0);           }           }         }      ////////////////////////////////////////////*************************       close(s);       return 0;     } 

if understand correctly, non-blocking socket looking for.

[edit:] use select

http://www.scottklement.com/rpg/socktut/nonblocking.html

non-blocking sockets can used in conjunction select() api. in fact, if reach point want wait data on socket marked "non-blocking", simulate blocking recv() calling select() first, followed recv().


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -