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 7809035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:18:23+00:00 2026-06-02T03:18:23+00:00

This is my case, I have a server listening for connections, and a client

  • 0

This is my case, I have a server listening for connections, and a client that I’m programming now. The client has nothing to receive from the server, yet it has to be sending status updates every 3 minutes.

I have the following at the moment:

    WSAStartup(0x101,&ws);
    sock = socket(AF_INET,SOCK_STREAM,0);
    sa.sin_family = AF_INET;
    sa.sin_port = htons(PORT_NET);
    sa.sin_addr.s_addr = inet_addr("127.0.0.1"); 
    connect(sock,(SOCKADDR*)&sa,sizeof(sa));
    send(sock,(const char*)buffer,128,NULL);

How should my approach be? Can I avoid looping recv?

  • 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-02T03:18:24+00:00Added an answer on June 2, 2026 at 3:18 am

    That’s rather dependant on what behaviour you want and your program structure.
    By default a socket will block on any read or write operations, which means that if your try and have your server’s main thread poll the connection, you’re going to end up with it ‘freezing’ for 3 minutes or until the client closes the connection.

    The absolute simplest functional solution (no multithreadding) is to set the socket to non-blocking, and poll in in the main thread. It sounds like you want to avoid doing that though.

    The most obvious way around that is to make a dedicated thread for every connection, plus the main listener socket. Your server listens for incoming connections and spawns a thread for each stream socket it creates. Then each connection thread blocks on it’s socket until it receives data, and either handles it itself or shunts it onto a shared queue.
    That’s a bulky and complex solution – multiple threads which need opening and closing, shared resources which need protecting.

    Another option is to set the socket to non-blocking (Under win32 use setsockopt so set a timeout, under *nix pass it the O_NONBLOCK flag). That way it will return control if there’s no data available to read. However that means you need to poll the socket at reasonable intervals (“reasonable” being entirely up to you, and how quickly you need the server to act on new data.)

    Personally, for the lightweight use you’re describing I’d use a combination of the above: A single dedicated thread which polls a socket (or an array of nonblocking sockets) every few seconds, sleeping in between, and simply pushed the data onto a queue for the main thread to act upon during it’s main loop.

    There are a lot of ways to get into a mess with asynchronous programs, so it’s probably best to keep it simple and get it working, until you’re comfortable with the control flow.

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

Sidebar

Related Questions

I have a java program which has a server-socket listening infinitely. In case of
I have a regular .NET application. In this case I have a part that's
I have a string read from another source such as \b\bfoo\bx. In this case,
This error has been driving me nuts. We have a server running Apache and
I've got a small client/server test application where I have a Flex app that
I have an server application (running as a Windows Service) that receives calls from
I have this code for Server in socket programming application, and I have this
I have this case <WrapPanel> <CheckBox>Really long name</CheckBox> <CheckBox>Short</CheckBox> <CheckBox>Longer again</CheckBox> <CheckBox>Foo</CheckBox> <Slider MinWidth=200
I Have two problem in this Case : I want to pass a JSON
So I have a Rails app (which in this case seems like it would

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.