Creating my first Unix Server/Client but getting a "shmget: Invalid argument" error and possibly more. [C] -


i doing unix, c assignment. creating server , client interact each other. not experienced tcp/ip programming apologize being slow in advance.

first, trying create basic layout of set up. compile client , server using makefile , works perfectly. however, when execute server, error:

shmget: invalid argument 

i think problem ipc resources. supposed remove ipc resources using atexit() don't think doing right.

here code server.c if helps:

#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 attach atexit()");             _exit(exit_failure);     }     /* create array of 2 semaphores key. */     semid = semget(semkey, 2, 0666 | ipc_creat);     if (semid < 0) {             perror("semget");             exit(0);     }     /* set values of semaphores */     argument.array = zeros;     if (semctl(semid, 0, setall, argument) < 0) {             printf("cannot init semaphore 0.\n");     }      /* create segment. */     if ((shmid=shmget(shmkey, sizeof(struct shared), ipc_creat|0666))<0) {             perror("shmget");             exit(1);     }      /* attach segment our data space. */     if ((shm=shmat(shmid, null, 0))==(struct shared *)-1) {             perror("shmat");             exit(1);     }     /* put things shared memory. */     s = shm->text;     (c = 'a'; c<= 'z'; c++) {             *s++ = c;     }     *s = '\0';     shm->number = 123;      //socket     srvrfd = socket(af_inet, sock_stream, 0);     if(srvrfd < 0) {             perror("socket");             exit(1);     }     srvraddr.sin_addr.s_addr = htonl(inaddr_any);     srvraddr.sin_port = htons(6060);     if(bind(srvrfd, (struct sockaddr *)&srvraddr, sizeof(srvraddr)) < 0) {             perror("bind");             exit(1);     }     listen(srvrfd, 5);     while(1) {             clntadrlen = sizeof(clntaddr);             clntfd = accept(srvrfd, (struct sockaddr*)&clntaddr, null);             if (fork() == 0) { //we're in child                     = recv(clntfd, buf, sizeof buf, 0);                     send(clntfd, buf, i, 0);                     close(clntfd);                     exit(0);             } else { //we're in 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);     } } 

thanks reading.

edit: definition of structure..

struct shared {     char text[27];     int number; } ; 

http://linux.die.net/man/2/shmget

looks either segment exists , it's smaller asked for, or you're trying create new one, it's smaller system min size (shmmin) or greater max (shmmax)

edit: turns out -- existed , smaller asking for. must have created size 27, 28, 29, 30, or 31, since works 27 not 32. if run unix command line program ipcs, show existing shared memory segments:

key        shmid      owner      perms      bytes      nattch     status       0x00000001 0          ec2-user   666        32         0                        

then ipcrm -m <key> delete it.

from i'm seeing, don't want use sys-v shared memory. use posix if can. here reference posix shared memory interface:

http://man7.org/linux/man-pages/man7/shm_overview.7.html

also check out:

http://www.cs.cf.ac.uk/dave/c/node27.html

for guide both, i'd prefer posix if it's available (and unless you're on old system dec alpha)


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -