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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:11:16+00:00 2026-05-22T18:11:16+00:00

I’m writing a client that can talk to multiple servers and processes user commands

  • 0

I’m writing a client that can talk to multiple servers and processes user commands on stdin or eventually from a file using Lua. The server is a custom application, so I’m handling all the communication back in C where all the code for the protocol has already been written. Here’s a bit of pseudo-code of what I have now:

int main(int argc, char **argv) {
  /* setup fd list, vars, etc */
  ...
  while (1) {
    /* process list of file descriptors to create read/write fd sets */
    ...
    select(max, &read_fds, &write_fds, NULL, NULL);
    for each file descriptor {
      if (read fd is set) {
        read data into a buffer
        if (current fd is stdin)
          process_stdin()
        else if (current fd is from server connection)
          process_remote()
      }
      if (write fd is set) {
        write data on non-blocking fd
      }
    }
  }
}

int process_stdin() {
  luaL_loadbuffer(L, stdin_buffer, len, "stdin");
  lua_pcall(L, 0, 0, 0);
}

int process_remote() {
  parse buffer into message from remote system
  if message is complete, call Lua with either a new message notification or resume
}

So here’s my problem: If the user on stdin types something like wait_for_remote_message(xyz), how do I stop at that point, return from the lua_pcall and go into the select loop to wait for more data? And then, how would process_remote resume the Lua command from that point forward?

I can imagine a solution involving pthreads, but that feels like overkill for this application and introduces a lot of extra complexity.

I can also imagine a solution where the while(1)/select loop is moved into a function and from the wait_for_remote_message(xyz) I jump back to C and call this function with stdin added to some kind of exclusion list.

Are there any better ways to do this?

  • 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-22T18:11:17+00:00Added an answer on May 22, 2026 at 6:11 pm

    This sounds like a perfect use for Lua coroutines, where you can call yield to suspend execution, and then resume later.

    Check out http://www.lua.org/pil/9.html for details

    You might do something like

    int process_stdin() {
        lua_State coroutine =  lua_newthread(L);
    
        luaL_loadbuffer(coroutine, stdin_buffer, len, "stdin");
        if (lua_resume(coroutine, 0) == LUA_YIELD) {
         // store coroutine somewhere global
        }
    }
    
    int process_remote() {
        // parse buffer into message from remote system
    
        // push the message onto the Lua stack
        lua_resume(coroutine, 1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.