5

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.

2
  • The first thing to do is to print out more details about the error. In Linux, the last error is stored in the "errno" value. Print that out and look up what the value means. Or use the "perror" function to print out a string message corresponding to the error.
    – kaylum
    Apr 1, 2015 at 21:45
  • The perror function was returning ERROR on accept, Bad File Descriptor, but Corluk explained my error. Apr 1, 2015 at 22:02

1 Answer 1

4

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.