Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Non blocking tcp and modbus sockets

NotificationsYou must be signed in to change notification settings

compscidr/non-blocking-sockets

Repository files navigation

This PoC project was motivated by some work we were doing with the libmodbus-dev library.

We were creating a server which was doing listen / accept in a thread, and wanting to cleanly shut it down. Once theaccept call is reached, from what I cantell, it's impossible to get it to unblock until an actual connection is made.

This differs from java, and other high level languages in that, typically you can simply close the socket and it will release the block. This is not the casewith c++/c sockets.

At first, I though it might be just a libmodbus problem, but I built another PoC using just simple tcp sockets and found it was the same.

This PoC shows how it can be cleanly shutdown using both tcp and modbus sockets.

self pipe, select approach

There are a few approaches which could be taken, however this one uses low cpu (doesn't poll, doesn't require timeouts).

The approach I'm using is toset the sockets into non-blocking mode and (not necessary, and didn't work with libmodbus) useSELECT to monitor groups of file descriptors which are ready to read (accept) and write.

A self-pipe is used, and added to the read descriptors. A signal handler is registered to theSIGUSR1 signal. When this signal is received, itwrites to the self-pipe. This triggers theSELECT since it is waiting on a read from the pipe + the server socket. It then breaks out of itslistening loop and cleanly closes.

Astop call can used to raise the SIGUSR1 signal to kick this off from another thread.

Three programs

  1. tcp_blocking_server: This demonstrates the problem using a tcp server
  2. tcp_server: A PoC showing the fix
  3. modbus_server: A libmodbus PoC with the fix

About

Non blocking tcp and modbus sockets

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp