jshag
jshag

Reputation: 33

Callback on sockets in linux

Is there any way to associate callback functions with sockets in Linux?

For example,when the function

connect(sock, (struct sockaddr *)peeraddr, sizeof(*peeraddr))

has connected the client with the server, is there a way to associate a function "afterRecv" with the socket of name sock such that after function recv has read some data from sock, afterRecv gets called?

That sock is of blocking type. This is due to the many internal send/recv calls on sock during the handshake period of OpenSSL.

If I modify OpenSSL, it would be cumbersome to modify every recv. So I wondered, if I can add a callback instead.

The flow should be:

  1. connect to socket

  2. recv function

  3. When recv has read data, afterRecv gets called as a callback on recv

Upvotes: 3

Views: 7264

Answers (1)

Some event libraries implement such callback hooks:

and all graphical or HTTP server libraries (e.g. Gtk/Glib, Qt, LibOnion, ...) provide (or use) such event libraries (around a multiplexing syscall like poll(2) etc).

Both Glib (from GTK) and QtCore (from Qt) are event libraries usable without any GUI

Read also about the C10K problem

Upvotes: 4

Related Questions