title: Message queue stack smashing
body: <p>I'm trying to implement a message queue and I have named the one process server and the other one client (as you can see I wanted to stick with the conventional names). The is a piece of code above the server that you don't need to care about since it works fine. I just posted it in case it messes up the whole effort.</p>
  <p>My actual question is how the server is supposed to know the pid of the client since they cannot connect without the key, which is made using ftok and the pid as a function argument?</p>
  <p>If you want to execute it yourself, you need to run the server program first and the client program afterward (no cmd args needed).
  I appreciate even the slightest kind of help.
  Thank you all in advance.</p>
  <pre><code>//HEADER FILE NAMED : v1.h
  #include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;sys/types.h&gt;
  #include&lt;unistd.h&gt;
  #include&lt;sys/ipc.h&gt;
  #include&lt;sys/msg.h&gt;
  #include &lt;sys/stat.h&gt;
  #include&lt;string.h&gt;
  
  #define N 95
  #define STRLIM 60
  #define PID 25922   
  #define MSGQ_KEY 1234
  #define REQ 1
  #define RSP 2
  
  #define REQ_SIZE (sizeof(struct request))
  #define RSP_SIZE (sizeof(struct response))
  
  struct request{
      long type;
      int mqid ;
      char* msg;
  };
  
  struct response{
      long type ;
      int res;
      char* msg;
  };
  
  int estab(char** contact);
  
  int estab(char** contact){
  char* test;
  test=&quot;hi&quot;;
  int ret;
  ret=strcmp(*contact,test);
  if(ret&lt;0){
      return 1;
  }
      return 0;
  }//end of estab
  
  </code></pre>
  <pre><code>//SERVER 
  #include &lt;fcntl.h&gt;  
  #include&quot;v1.h&quot;  
  
  char** cat;
  int fd;
  
  int main(int argc, char const *argv[])
  {   int i;
      int mqid;
      struct response rsp;
      struct request req;
          
      cat=(char**)malloc(sizeof(char*)*N);
      fd=open(&quot;dictionary.txt&quot;,O_RDONLY); 
      if(fd==-1){
          printf(&quot;fd error %d&quot; , fd);
          perror(&quot;Error:&quot;);        
      }
      
      for(i=0;i&lt;N;i++){
          cat[i]=(char*)malloc(sizeof(char)*STRLIM);
         
          read(fd,cat[i],40);
          printf(&quot;%s&quot;, cat[i]);
      }
      //////////////////end of char ** /////////////////
      int pid=getpid();
      key_t key=ftok(&quot;/home/myName/Desktop/as3/v1/v1s.c&quot;,PID);
      printf(&quot;The key is : %d&quot;,key);
      mqid=msgget(key,IPC_CREAT|S_IRWXU|IPC_NOWAIT);
          
      int ret=0;
      do{       
          msgrcv(mqid,&amp;req,REQ_SIZE,0,0);   
          perror(&quot;its:&quot;);
          ret=estab(&amp;req.msg);        
      }while(ret!=0);
      msgsnd(mqid,&amp;rsp,RSP_SIZE,0);
      printf(&quot;connected&quot;);
  
     return 0;}//end of main 
  
  
  </code></pre>
  <pre><code>#include&quot;v1.h&quot;
  
  int main(int argc, char const *argv[])
  {   int mqid,ret,pid;
      struct request req;
      struct response rsp={0};
    
      req.msg=&quot;hi&quot;;
      pid=getpid();
      printf(&quot;client's pid : %d&quot;,pid);
      pid=PID;
      mqid=msgget(MSGQ_KEY,0);
  
      msgsnd(mqid,&amp;req,REQ_SIZE,0);
  
      do{
          msgrcv(mqid,&amp;rsp,RSP_SIZE,0,0);
        
          ret=estab(&amp;rsp.msg);
            
      }while(ret!=0);
      printf(&quot;connected from client!&quot;);
      return 0;
  }
  
  </code></pre>