1

I tried to write a PHP project to communicate with a PLC, but I have a problem when I close a socket client, it didn't close immediately, connected devices increase when I call an AJAX function to reload values, can anyone help me? I use RS_sim to simulate PLC

my page

My code to create and close socket client:

private function connect(){
// Create a protocol specific socket
if ($this->socket_protocol == "TCP"){
    // TCP socket
    $this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
} 

$result = @socket_connect($this->sock, $this->host, $this->port);
if ($result === false) {
    throw new Exception("socket_connect() failed.</br>Reason: ($result)".
        socket_strerror(socket_last_error($this->sock)));
} else {
    $this->status .= "Connected\n";
    return true;
}}

private function disconnect(){
socket_close($this->sock);
$this->status .= "Disconnected\n";}
GregP
  • 143
  • 12
tidibi272
  • 11
  • 2
  • Did you get any [error message](https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) for socket_close? If not in PHP, then in the simulator? Is this a long running script, or just a one-off connection? (Possibly also try `strace` to inspect your running script.) – mario Mar 09 '19 at 04:45
  • Does the server expect you to send a command to close the connection? Closing the connection in the client without a message to the server might not be enough – Joni Mar 09 '19 at 05:18
  • Thanks for your supports,mario and Joni, I will try with a real PLC – tidibi272 Mar 09 '19 at 07:04

0 Answers0