UDP communication, message queue and socket? -


i have client sends message server; server, before receiving data, creates child process; when client try connect server, server sends on message queue data client; child read message queue , send response client; program in not complete, because i'm doing tests; when run program, client sending request , receives response data; put printf in child code, , doesn't print anything; child executes sendto, don't understand if child readed message queue..

server code:

struct msgbuf{     long mtype;     struct sockaddr_in s; };    void    err_exit(char* str) {     perror(str);     exit(exit_failure); }     int get_msg_queue() {     key_t key = ftok(".",'b');     if(key == -1)         err_exit("ftok");     int qid = msgget(key,ipc_creat |  0666);     if(qid == -1)         err_exit("msgget");     return qid; }   void child_job(int qid, pid_t pid) {     (void)pid;     struct sockaddr_in addr;     header p;     int sockfd;     struct msgbuf msg;      msg.mtype = 1;      memset((void *)&addr, 0, sizeof(addr));     addr.sin_family = af_inet;     addr.sin_addr.s_addr = htonl(inaddr_any);     addr.sin_port = htons(0);      if ((sockfd = socket(af_inet, sock_dgram, 0)) < 0)          err_exit("errore in socket");      if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {            perror("error in bind");            exit(1);     }       if(msgrcv(qid,&msg,sizeof(msg) - sizeof(long),0,0) == -1)            err_exit("msgrcv");     printf("received data\n");      p.v[0] = 1;     p.v[1] = 1;     p.data[0] = 'a';     p.data[1] = '\0';      printf("tring sending\n");     if (sendto(sockfd, &p, sizeof(header), 0, (struct sockaddr *)&addr, sizeof(addr)) < 0) {               err_exit("sendto");     }      printf("sended request\n");     exit(exit_success); }   void initialize_processes(int qid) {       pid_t pid;     pid = fork();     if(pid == -1)         err_exit("fork");     if(pid == 0) child_job(qid,getpid());  }     void write_on_queue(struct sockaddr_in addr, int qid) {      struct msgbuf msg;      size_t size = sizeof(addr) - sizeof(long);      msg.mtype = 1;      msg.s = addr;      if(msgsnd(qid,&msg,size,0) == -1)         err_exit("msgsnd"); }     void listen_request(int qid) {     int sockfd;     struct sockaddr_in addr;     socklen_t l = sizeof(addr);     header p;      memset((void *)&addr, 0, sizeof(addr));     addr.sin_family = af_inet;     addr.sin_addr.s_addr = htonl(inaddr_any);     addr.sin_port = htons(servport); /* numero di porta del server */      if ((sockfd = socket(af_inet, sock_dgram, 0)) < 0)         err_exit("errore in socket");       if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {        printf("padre: erorre bind\n");        perror("errore in bind");        exit(1);      }       printf("listening request\n");      if ((recvfrom(sockfd, &p, maxline, 0, (struct sockaddr *)&addr, &l)) < 0){          printf("errno code: %d\n",errno);          err_exit("recvfrom\n");     }     write_on_queue(addr,qid);     printf("write on queue\n");     return; }          int main(int argc, char*argv[])     {     (void) argc, (void) argv;     int qid;       qid = get_msg_queue();     initialize_processes(qid);     listen_request(qid);     printf("server listened request\n");     wait(null);     exit(exit_success); } 

client code:

void err_exit(char* str) {     perror(str);     exit(exit_failure); }  //header typedef 2 integers , buffer 2 carachtet  int request_to_server(int sockfd,header* x,struct sockaddr_in addr) {     int n;     if(sendto(sockfd, null, 0, 0, (struct sockaddr *) &addr,    sizeof(addr)) < 0)         err_exit("sendto\n");      printf("client waiting response\n");     n = recvfrom(sockfd,x,sizeof(header),0,null,null);     if (n < 0) {         perror("errore in recvfrom");         exit(1);     }     if(n > 0) {        printf("client connected!\n");     }     return 1; }   int main(int argc, char *argv[ ]) {     int   sockfd;     struct    sockaddr_in   servaddr;     header p;     if (argc != 2) {       fprintf(stderr, "utilizzo: daytime_clientudp <indirizzo ip  server>\n");       exit(1);     }      if((sockfd = socket(af_inet, sock_dgram, 0)) < 0) { /* crea il socket   */         perror("errore in socket");         exit(1);     }      memset((void *)&servaddr, 0, sizeof(servaddr));     servaddr.sin_family = af_inet;     servaddr.sin_port = htons(servport);      if(inet_pton(af_inet, argv[1], &servaddr.sin_addr) <= 0) {        err_exit("error in inet_pton %s");     }       if(!request_to_server(sockfd,&p,servaddr))        err_exit("request server");      printf("received ack!\n");      for(;;)         ;     exit(exit_success); } 


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -