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

  • SEARCH
  • Home
  • 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 8619837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:26:12+00:00 2026-06-12T06:26:12+00:00

int func(boost::asio::ip::tcp::socket &socket) { boost::system::error_code ec; socket.write_some(boost::asio::buffer(hello world!), ec); cout << socket.is_open() << endl;

  • 0
int func(boost::asio::ip::tcp::socket &socket)
{
    boost::system::error_code ec;
    socket.write_some(boost::asio::buffer("hello world!"), ec);
    cout << socket.is_open() << endl;
    if(ec)
    {
        cout << boost::system::system_error(ec).what() << endl;
    }
    return 0;
}

int main(int argc, char* argv[])
{
    using namespace boost::asio;
    io_service iosev;
    ip::tcp::acceptor acceptor(iosev, ip::tcp::endpoint(ip::tcp::v4(), 1000));

    while(1)
    {
        ip::tcp::socket socket(iosev);
        acceptor.accept(socket);
        boost::thread t = boost::thread(func, boost::ref(socket));
    }
    return 0;
}

I want one new thread handle new connection. But in function “func”, the socket is not open and I got “Bad file descriptor”. I read some examples in the document and web, but they are async. I think it’s not necessary for my simple demand.

How can I fix the error? Any help is appreciated

  • 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-06-12T06:26:13+00:00Added an answer on June 12, 2026 at 6:26 am

    Your socket is a temporary object, you pass a reffence to it but the object is going out of the scope and being destroyed before the thread process it. Use shared_ptr<socket> or keep them in a container.

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

Sidebar

Related Questions

How can I template these functions? boost::function< void(int) > binder( void(*func)(int, char), int a1,
Consider following example. #include <iostream> #include <algorithm> #include <vector> #include <boost/bind.hpp> void func(int e,
the following code: /***************************************************************************/ boost::mutex m; struct func { func(int v):n(v) {} void operator()()
I have a C function int func( int a, int* b) that I need
I need the following C function in Python: int func(Uint8 *bytRecvBuffer, int *iRecvLen); I
I have a C++ program: class X { private: int a; public: int func(char
Say a function takes a void pointer as an argument, like so: int func(void
I have this function: void func(int a, int b, char s, FILE *fp, int
The below does not compile: Func<int, int> fac = n => (n <= 1)
I have the following function: func addCatsToMap(m map[string][]CatHouse, meowId int, treats Set, dog *Dog)

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.