Note that there are some explanatory texts on larger screens.

plurals
  1. POBad file descriptor error while creating a concurrent server in a client-server app (UDP)
    primarykey
    data
    text
    <p>I'm making a client-server app with udp with a concurrent server and I'm having some problems sending the information to the server.</p> <p>Here's the code for the server:</p> <pre><code>#define SERV_UDP_PORT 6777 int main() { int sockfd, newsockfd, clilen, n, pid_hijo; struct sockaddr_in cli_addr, serv_addr ; char host_name[200], buffer[1024]; if ((sockfd = socket(AF_INET, SOCK_DGRAM,IPPROTO_UDP)) &lt; 0){ perror("server: can't open datagram socket"); exit(-1); } /* if */ serv_addr.sin_family = AF_INET ; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY) ; serv_addr.sin_port = htons(SERV_UDP_PORT); if (bind(sockfd, (struct sockaddr *) &amp;serv_addr, sizeof(serv_addr)) &lt;0) { perror("server: can't bind local address") ; exit(-1); } /* if */ for (;;) { // Create the new process for the concurrente server if ((pid_hijo=fork()) == 0) { servidor_hijo(sockfd); close(sockfd); exit(0); } else { close(sockfd); while (wait((int *)0) != pid_hijo); } } } int servidor_hijo(int sockfd) { int n, clilen; char buffer[1024]; struct sockaddr_in cli_addr; // Inicializamos los buffers clilen = sizeof(cli_addr) ; bzero((char *)&amp;cli_addr,clilen); /* debe inicializarse cli_addr antes del recvfrom */ bzero(buffer, sizeof(buffer)); if ((n=recvfrom(sockfd, buffer, sizeof(buffer),0, (struct sockaddr *)&amp;cli_addr,&amp;clilen)) &lt;0) { perror("secho: error en funcion recvfrom"); exit(-1); } /* if */ printf("\n\n%s (%d bytes)\n",buffer,n); if ((n=sendto(sockfd, buffer, strlen(buffer),0, (struct sockaddr *)&amp;cli_addr,clilen)) &lt;0) { perror("secho: Error en funcion sendto"); exit(-1); } } </code></pre> <p>And here's the code for the client</p> <pre><code>/* Cliente de ECO sobre UDP*/ #include &lt;sys/types.h&gt; #include &lt;sys/socket.h&gt; #include &lt;netinet/in.h&gt; #include &lt;netdb.h&gt; #define SERV_UDP_PORT 6777 main() { int sockfd, n, clilen; struct sockaddr_in serv_addr, cli_addr ; char serv_host_addr[30], buffer[1024]; printf("Dirección IP del servidor (a.b.c.d) =&gt; "); gets(serv_host_addr); if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) &lt; 0) { perror("client: can't open datagram socket") ; return -1; } bzero((char *)&amp;cli_addr,sizeof(cli_addr)); /* puerto cero */ cli_addr.sin_family = AF_INET ; if (bind(sockfd, (struct sockaddr *) &amp;cli_addr,sizeof(cli_addr)) &lt; 0) { perror("client: can't bind a port"); exit(-1); } /* if */ serv_addr.sin_family = AF_INET ; inet_pton(AF_INET,serv_host_addr,&amp;serv_addr.sin_addr); serv_addr.sin_port = htons(SERV_UDP_PORT) ; printf("Bienvenido al Servicio de ECO ==&gt; "); gets(buffer); clilen=sizeof(serv_addr); if ((n=sendto(sockfd, buffer,strlen(buffer),0, (struct sockaddr *)&amp;serv_addr,clilen)) &lt;0) { perror("cecho: error en funcion sendto"); exit(-1); } /* if */ printf("cecho: envie %d bytes\n",n); bzero(buffer,sizeof(buffer)); clilen = sizeof(serv_addr); if ((n=recvfrom(sockfd,buffer,sizeof(buffer),0, (struct sockaddr*)&amp;serv_addr,&amp;clilen)) &lt; 0) { perror("cecho: error en funcion recvfrom"); exit(-1); } /* if */ printf("ECO&gt; %s\n",buffer); close(sockfd); } </code></pre> <p>The function servidor_hijo is what makes the server concurrent to reply different request...</p> <p>The problem is... When I execute the client app, it sends the packet to the server app, but the server doesn't send the response and prints on the screen an error message telling that has been a "Bad file descriptor error".</p> <p>Could you help me with this?</p> <p>Thank you all!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload