Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6357275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:08:00+00:00 2026-05-24T23:08:00+00:00

I keep getting a Bad file descriptor error when I try to send data

  • 0

I keep getting a “Bad file descriptor” error when I try to send data from my tcp server to my tcp client. What does this mean in terms of sockets? I have been at this for awhile now and I don’t see what could be wrong with my code. Its basically the same code I was using two days ago and that code worked fine. I was hoping someone could tell me what are common causes of bad file descriptors when trying to send over a socket and how I can go about checking/fixing them. Any help is appreciated. I will post some code below in case it helps.

/*Waits to connect a client. Returns true if successful*/
bool TcpServer::launchServer() {
int status;

struct addrinfo hints;
struct addrinfo *servinfo;  //will point to the results

//store the connecting address and size
struct sockaddr_storage their_addr;
socklen_t their_addr_size;


memset(&hints, 0, sizeof hints); //make sure the struct is empty
hints.ai_family = AF_INET;  //ipv4
hints.ai_socktype = SOCK_STREAM; //tcp

//get server info, put into servinfo
if ((status = getaddrinfo("192.168.2.3", port, &hints, &servinfo)) != 0) {
    printf("\ngetaddrinfo error: %m", errno);
    return false;
}

//make socket
fd = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol);
if (fd < 0) {
    printf("\nserver socket failure %m", errno);
    return false;
}

//allow reuse of port
int yes=1;
if (setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(char*) &yes,sizeof(int)) == -1) {
    perror("setsockopt");
    return false;
}

//bind
if(bind (fd, servinfo->ai_addr, servinfo->ai_addrlen) < 0) {
    printf("\nBind error %m", errno);
    return false;
}

//free up space
freeaddrinfo(servinfo);

//listen
if(listen(fd, 5) < 0) {
    printf("\nListen error %m", errno);
    return false;
}
their_addr_size = sizeof(their_addr);


//accept
comm_fd = accept(fd, (struct sockaddr*)&their_addr, &their_addr_size);
if( comm_fd < 0) {
    printf("\nAccept error %m", errno);
    return false;
}

return true;
}   //END LAUNCHSERVER






void TcpServer::communicate() {


fd_set read_flags,write_flags; // the flag sets to be used
struct timeval waitd = {10, 0};          // the max wait time for an event
int sel;        // holds return value for select();
int numRead;    //holds return value for read()
int numSent;    //holds return value for send()
char in[255];   //in buffer
char out[255];  //out buffer

//clear buffersz
memset(&in, 0, 255);
memset(&out, 0, 255);


while(!done) {
    FD_ZERO(&read_flags);
    FD_ZERO(&write_flags);
    FD_SET(comm_fd, &read_flags);
    FD_SET(comm_fd, &write_flags);
    FD_SET(STDIN_FILENO, &read_flags);
    FD_SET(STDIN_FILENO, &write_flags);

    //call select
    sel = select(comm_fd+1, &read_flags, &write_flags, (fd_set*)0, &waitd);

    //if an error with select
    if(sel < 0)
        continue;

    //if socket ready for reading
    if(FD_ISSET(comm_fd, &read_flags)) {

        //clear set
        FD_CLR(comm_fd, &read_flags);

        memset(&in, 0, 255);

        numRead = recv(comm_fd, in, 255, 0);
        //if an error, exit
        if(numRead < 0) {
            printf("\nError reading %m", errno);
            myAgent->getRobot()->pauseSensorStream();
            done = true;
        }   //end if error
        //if connection closed, exit
        else if(numRead == 0) {
            printf("\nClosing socket");
            close(comm_fd);
            done = true;
        }   //end if connection closed
        //if message, call getsendback
        else if(in[0] != '\0') {
            //std::cout<<"\nClient: "<<in;
            getSendBack(in);
        }   //end if message
    }   //end if ready for read


    //if stdin is ready for reading
    if(FD_ISSET(STDIN_FILENO, &read_flags))
        fgets(out, 255, stdin);


    //if socket ready for writing
    if(FD_ISSET(comm_fd, &write_flags)) {

        //printf("\nSocket ready for write");
        FD_CLR(comm_fd, &write_flags);

        //check validity by checking for a digit
        if(isdigit(out[0])) {

            //create message to send
            std::stringstream tosend;
            tosend<<"@ "<<out;
            //std::cout<<"\ntosend: "<<tosend.str();

            //send
            //********ERROR HAPPENS HERE PRINTS OUT MESSAGE BELOW******
            numSent = send(comm_fd, tosend.str().c_str(), tosend.str().length(), 0);
        }   //end if valid message
        //if error, exit
        if(numSent < 0) {
            printf("\nError sending %m", errno);
            done = true;
        }   //end if error
        //wait for message to get there, then clear
        usleep(5000);
        memset(&out, 0, 255);
    }   //end if
}   //end while
}   //END COMMUNICATE

Client code is basically the same.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:08:02+00:00Added an answer on May 24, 2026 at 11:08 pm

    You answered your own question. Without explicitly initializing numSent and numRead, you get garbage, which may happen to be a negative number for numSent, which would cause it to error if there was no digit in the out[] array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I keep getting a Bad Request error from the URL I'm calling in my
I keep getting this error when trying to delete a record from my table.
I keep getting a exc bad access error and I think it has something
Keep getting this error after inserting a subdatasheet into a query and trying to
I keep getting a PermGen error on my Tomcat 6 server. I know what
I keep getting a 400 bad request code from a hotfile.com page when I
I keep getting: Unable to create request (bad url?) error message on a request
I keep getting this when trying to start a new project ERROR: Unable to
I keep getting tasks that are above my skill level. How can I address this without coming accross as grossly incompetent?
I keep getting compiler errors when I try to access flashVars in an AS3

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.