title: Creating my first Unix Server/Client but getting a "shmget: Invalid argument" error and possibly more. [C]
body: <p>I am doing a Unix, C assignment. I am creating a Server and a Client which will interact with each other. I am not very experienced with TCP/IP programming so I apologize for being slow in advance.</p>
  
  <p>First, I am trying to create a basic layout of my set up. I compile the Client and Server using a Makefile and it works perfectly. However, when I execute the Server, I get this error:</p>
  
  <pre><code>shmget: Invalid argument
  </code></pre>
  
  <p>I think it is a problem with IPC resources. I am supposed to remove the IPC resources using atexit() but I don't think I am doing it right.</p>
  
  <p>Here is the code for server.c if it helps:</p>
  
  <pre><code>#include "server.h"
  
  int shmid, semid;
  struct Shared *shm;
  
  int main() {
      key_t shmkey = 0x6060, semkey = 0x6061;
      char *s, c;
      unsigned short zeros[2] = {0, 0};
  
      int srvrFd, clntFd, clntAdrLen, i; //socket
      struct sockaddr_in srvrAddr, clntAddr;
      char buf[256];
  
      if(atexit(server_exit) != 0) {
              perror("failed to attach atexit()");
              _exit(EXIT_FAILURE);
      }
      /* Create an array of 2 semaphores with key. */
      semid = semget(semkey, 2, 0666 | IPC_CREAT);
      if (semid &lt; 0) {
              perror("semget");
              exit(0);
      }
      /* Set the values of semaphores */
      argument.array = zeros;
      if (semctl(semid, 0, SETALL, argument) &lt; 0) {
              printf("Cannot init semaphore 0.\n");
      }
  
      /* Create the segment. */
      if ((shmid=shmget(shmkey, sizeof(struct Shared), IPC_CREAT|0666))&lt;0) {
              perror("shmget");
              exit(1);
      }
  
      /* Attach the segment to our data space. */
      if ((shm=shmat(shmid, NULL, 0))==(struct Shared *)-1) {
              perror("shmat");
              exit(1);
      }
      /* Put some things into the shared memory. */
      s = shm-&gt;text;
      for (c = 'a'; c&lt;= 'z'; c++) {
              *s++ = c;
      }
      *s = '\0';
      shm-&gt;number = 123;
  
      //socket
      srvrFd = socket(AF_INET, SOCK_STREAM, 0);
      if(srvrFd &lt; 0) {
              perror("socket");
              exit(1);
      }
      srvrAddr.sin_addr.s_addr = htonl(INADDR_ANY);
      srvrAddr.sin_port = htons(6060);
      if(bind(srvrFd, (struct sockaddr *)&amp;srvrAddr, sizeof(srvrAddr)) &lt; 0) {
              perror("bind");
              exit(1);
      }
      listen(srvrFd, 5);
      while(1) {
              clntAdrLen = sizeof(clntAddr);
              clntFd = accept(srvrFd, (struct sockaddr*)&amp;clntAddr, NULL);
              if (fork() == 0) { //we're in the child
                      i = recv(clntFd, buf, sizeof buf, 0);
                      send(clntFd, buf, i, 0);
                      close(clntFd);
                      exit(0);
              } else { //we're in the parent
                      close(clntFd);
              }
      }
  }
  
  void server_exit(void)
  {
      if(shm != NULL) {
              shmdt(shm);
      }
      if(semid != -1) {
              semctl(semid, 0, IPC_RMID);
      }
      if(shmid != -1) {
              shmctl(shmid, IPC_RMID, 0);
      }
  }
  </code></pre>
  
  <p>Thanks for reading.</p>
  
  <p>Edit: Definition of structure..</p>
  
  <pre><code>struct Shared {
      char text[27];
      int number;
  } ;
  </code></pre>