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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:07:26+00:00 2026-05-31T01:07:26+00:00

I have a chat server in C/Linux using TCP sockets. When using libev I

  • 0

I have a chat server in C/Linux using TCP sockets. When using libev I am able to create an ev_io watcher for read events once for a socket. Something like:

ev_io* new_watcher = (ev_io*)malloc(sizeof(ev_io));

//initialize the watcher
ev_init(new_watcher, read_cb);

//set the fd and event to fire on write
ev_io_set(new_watcher, watcher->fd, EV_READ);

//start watching
ev_io_start(loop, new_watcher);

and this works fine because the read event will only fire when there is data to read. However, I have to treat write events differently because they are constantly firing even when I don’t have data to write. To solve this problem I have my read_callback create an ev_io watcher for a write data only when there is data ready to be written, and then the write_callback will delete the watcher after it has sent it’s message.

This means that I am allocating, initialising, setting, watching, unwatching and deallocating the write watcher each time I need to handle a message. I worry that I may be handling this incorrectly and inefficiently.

What is the best method for handling write_callback events in libev?

Thanks in advance.

  • 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-31T01:07:29+00:00Added an answer on May 31, 2026 at 1:07 am

    The allocation can add some overhead, you could use a static variable instead of malloc, or malloc once and only free after the event loop is finished.
    You only need to set before write and unset after it succeeds. But yes, that’s how it needs to be done.

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

Sidebar

Related Questions

I have implemented a chat server in C/Linux that uses TCP sockets. It is
I have existing chat server code written in C/C++ that communicates over TCP/IP with
I'm trying to create my first server, a simple chat server. I have a
I have written a UDP client server chat, I am able to communicate to
Hi guys i am trying to create a simple chat server. I have installed
I have been working on a (relatively) simple tcp client/server chat program for my
I have a WCF chat service that accepts duplex tcp connections. A single duplex
i have a java chat server & client, that works fine. I made a
I have an app that connects to a remote chat server and then the
I'm building a simple chat server in java , where users can have private

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.