-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is the function call to "modbus_tcp_accept()" blocking ? #452
Comments
The call to See modbus-tcp.c (libmodbus version 3.1.4): int modbus_tcp_accept(modbus_t *ctx, int *s)
{
...
#ifdef HAVE_ACCEPT4
/* Inherit socket flags and use accept4 call */
ctx->s = accept4(*s, (struct sockaddr *)&addr, &addrlen, SOCK_CLOEXEC);
#else
ctx->s = accept(*s, (struct sockaddr *)&addr, &addrlen);
#endif
...
} The documentation for |
Thank you so much. I agree to your answer. In my case there is no pending connection initially as no client is initiating a connection at the start. But server is first trying to initialize all its components and 1 of the components is modbus-tcp. But the call to "modbus_tcp_accept()"is becoming blocking. Is there any alternative way to make the call non-blocking or do I need to go for a multi-threading approach. Is there any signal or event notification mechanism. Thanks and Regards, |
After playing around with my demo application the conclusion I draw is as follows:
Thanks and Regards, |
Good ideas. I wanted to tell you about the non-blocking socket option aswell. About the signal handler. This solution sounds good, but maybe has a drawback aswell. I assume the signal handler is called on the main thread at any arbitrary moment, right? I think this can introduce issues just like multithreading. (Maybe I am wrong.) I think one needs some event loop and the signal handler should dispatch an event into the loop, whenever a connection is requested. This way you don't have to care about creating mutexes for shared memory. |
One interesting thing to note about nonblocking with modbus_tcp_accept is that a nonblocking listen socket does not seem to have been on the developer's mind when it was written.
We had a pretty exciting debugging session because of this. Someone closed stdin repeatedly, and we did not find the cuplrit for a while. Especially because by default strace only traces the main thread if you don't give it the -f switch. So there was no close() in the trace but the descriptor magically became invalid - repeatedly. |
I'm currently running a TCP/IP server on one the threads of my application. When trying to close the application (and with no incoming TCP/IP connection), i'm unable to terminate the thread correctly, since the modbus_tcp_pi_accept function blocks it. Is there a way to force the function to return? Many thanks, |
@KsaweryRettinger Maybe you can close the socket and the function returns with an error? Have you tried? |
Several possibilities exists to cleanly exit an accept which blocks. One is the self-pipe-trick, This issue shows some C-code (inside C++ containers are used) |
Thank you for your replies. Simply closing the socket didn't help. In the end, I made the socket non-blocking, as follows: ctx = modbus_new_tcp_pi(NULL, "1502"); I realize this might not be the most efficient use of my thread, but it works for now. Regards, |
This is more CPU intensive than using poll or select with a self-pipe. But maybe this is neglectable in your application. |
Good evening. I am using libmodbus in one of our projects. It is a very efficient library.
I have one doubt regarding one of the functions named - "modbus_tcp_accept()".
First I create the modbus TCP context using "modbus_new_tcp()". Then I allocate memory map using "modbus_mapping_new()". Next I create a socket using "modbus_tcp_listen()". I capture the return type of this function in a socket variable which I will pass it to my next call to modbus_tcp_accept(). The program flow is very simple as shown below:
DISCRETE_OUTPUT_COILS_SIZE,
ANALOG_HOLDING_REGISTER_SIZE,
ANALOG_INPUT_REGISTER_SIZE);
DOUBT:
Is the last call in step 4 to function "modbus_tcp_accept()" a blocking call?
I tested in my demo application. All the above calls from step 1 till 3 are non-blocking. Please tell me if the last call in step 4 to "modbus_tcp_accept()" is blocking or not.
According to my experiment I think that it is a blocking call.
Please help me out with your answer.
Looking forward to help.
Thanks and Regards,
Sritam Paltasingh.
The text was updated successfully, but these errors were encountered: