Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux Socket Bad File Descriptor

Tags:

c

linux

sockets

I couldn’t find a duplicate, so I decided to post. We're getting into Sockets (beginner-level) now, and was given the code below to make the client send a simple message to the server by using send() and recv(). However, everything I have tried doesn’t seem to get rid of the error: Bad File Descriptor and newsockfd always returns the value -1. I'm confused and have no idea why it doesn’t work.

defs.h

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define SERV_TCP_PORT 6000
#define SERV_HOST_ADDR  "127.0.0.1" 

char *pname;

client.c

#include "../defs.h"
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

main(argc, argv)
int argc;
char *argv[];
{
  int sockfd;
  struct sockaddr_in serv_addr;

  pname=argv[0];
  bzero((char*) &serv_addr, sizeof(serv_addr));
  serv_addr.sin_family=AF_INET;
  serv_addr.sin_addr.s_addr=inet_addr(SERV_HOST_ADDR);
  serv_addr.sin_port=htons(SERV_TCP_PORT);
  if((sockfd=socket(AF_INET,SOCK_STREAM,0)) < 0) {
    printf("client: can't open stream socket\n");
    exit(0);
  }
  if (connect(sockfd,(struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
    printf("client: can't connect to server\n");
    exit(0);
  }
 send(sockfd,"1",1,0);
  close(sockfd);
  exit(0);
}

server.c

#include "../defs.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <strings.h>
void error(char *msg)
{
    perror(msg);
    exit(1);
}
main(argc, argv)
int argc;
char *argv[];
{
  int sockfd,clilen, childpid;
 int newsockfd;
char buff[255];
  struct sockaddr_in cli_addr, serv_addr;
  if ((sockfd=socket(AF_INET,SOCK_STREAM, 0)) <0) {
    printf("server: can't open stream socket\n");
    exit(0);
  }
  bzero((char*) &serv_addr, sizeof(serv_addr));
  serv_addr.sin_family=AF_INET;
  serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
  serv_addr.sin_port=htons(SERV_TCP_PORT);

  if(bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
    printf("server: can't bind local address\n");
    exit(0);
  }
  listen(sockfd, 5);
  for ( ; ; ) {
    clilen=sizeof(cli_addr);
    newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
  printf("%d\n",newsockfd);
    if(newsockfd < 0) {
      printf("server: accept error\n");
    error("ERROR on accept");
      exit(0);
    }
    if((childpid=fork()) <0) {
      printf("server: fork error\n");
      exit(0);
    }
    else if (childpid==0) {
        close(sockfd);  
      recv(newsockfd,buff,sizeof(buff),0);
    }
    close(newsockfd);
  }
}

Thanks for your time.

like image 218
DominusMors Avatar asked Sep 28 '22 09:09

DominusMors


1 Answers

In the code of the child you close sockfd, which represents the server. But in the next iteration of the infinite for-loop, which the child also runs, you attempt to accept() a new client using sockfd. Therefore, sockfd is a Bad file descriptor.

Remove the close(sockfd); line in your code and close it when you can be absolutely sure it is not needed anymore.

like image 55
cadaniluk Avatar answered Oct 13 '22 12:10

cadaniluk