I have downloaded an example project for a TCP socket server running on a Raspberry Pi. Code copied from the example for socket communication Linux SL.
See the code below:
The problem with this code arrises when bind is called after a complete download of my project. I've connected xOpen and xClose to the start en stop system events of the controller (see TaskConfiguration.png). So after a download xOpen will become active for 30 sec. and it will start the server. This works when the controller is stopped and started.
The problem is that after a download a created socket will remain active on the OS. So we use the socket option SOCKET_SO_REUSEADDR. However the bind function will fail with error code 519 Address in use. So it doesn't reuse the address. You can also specify SOCKET_SO_REUSEPORT however that results in an error.
I thought i solved the problem by creating a System event stopping the server just before a download commences. With the idea in mind that the socket will also be released on the OS.
However I've tried several system events without any result. The system events i've tried are: PrepareDownload and PrepareExitTasks and PrepareOnlineChange.
Found the problem:
I've stored the socket handles in a %MD100 and %MD104 DWORD. Now I'm able to close the socket in the system event routines before a download.
When I use CODESYS V3.5 SP18.0 for socket programming, the SysSockBind() function returns "16#0207" error.
The help file says "ERR_SOCK_ADDRINUSE, The provided address is already in use."
I also want to know how to solve this problem, can you explain it, I need your help. Can you give me an example of how to solve it?
Thank you.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Certainly,
You need to execute the following steps:
1. First place the socket variable at a %MD and also xopen and xclose should be placed at a%M.
2. Secondly define some eventroutines when the socket should be closed to prevent it from being reoped / reused. You can do this in Taskconfiguration->System events.
3. Now yo need to define the event routines
Start socket server
Stop the socket server - called before the application stops
Stop the socket server - called before an online change
Stop the socket server - called before a download
Now sockets will be closed when the soft PLC is stopped or changed
I have downloaded an example project for a TCP socket server running on a Raspberry Pi. Code copied from the example for socket communication Linux SL.
See the code below:
The problem with this code arrises when bind is called after a complete download of my project. I've connected xOpen and xClose to the start en stop system events of the controller (see TaskConfiguration.png). So after a download xOpen will become active for 30 sec. and it will start the server. This works when the controller is stopped and started.
The problem is that after a download a created socket will remain active on the OS. So we use the socket option SOCKET_SO_REUSEADDR. However the bind function will fail with error code 519 Address in use. So it doesn't reuse the address. You can also specify SOCKET_SO_REUSEPORT however that results in an error.
I thought i solved the problem by creating a System event stopping the server just before a download commences. With the idea in mind that the socket will also be released on the OS.
However I've tried several system events without any result. The system events i've tried are: PrepareDownload and PrepareExitTasks and PrepareOnlineChange.
Found the problem:
I've stored the socket handles in a %MD100 and %MD104 DWORD. Now I'm able to close the socket in the system event routines before a download.
When I use CODESYS V3.5 SP18.0 for socket programming, the SysSockBind() function returns "16#0207" error.
The help file says "ERR_SOCK_ADDRINUSE, The provided address is already in use."
I also want to know how to solve this problem, can you explain it, I need your help. Can you give me an example of how to solve it?
Thank you.
Certainly,
You need to execute the following steps:
1. First place the socket variable at a %MD and also xopen and xclose should be placed at a%M.
2. Secondly define some eventroutines when the socket should be closed to prevent it from being reoped / reused. You can do this in Taskconfiguration->System events.
3. Now yo need to define the event routines
Start socket server
Stop the socket server - called before the application stops
Stop the socket server - called before an online change
Stop the socket server - called before a download
Now sockets will be closed when the soft PLC is stopped or changed
Last edit: broos72482 2022-05-06