Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6564551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:58:52+00:00 2026-05-25T13:58:52+00:00

Hello i made a php socket server to get data from plc, plc is

  • 0

Hello i made a php socket server to get data from plc, plc is configured as tcp socket client.
I’ve a seriuous problem, if local net go down seems that function socket_accept stalled, plc don’be able to send me data.
If i restart my server plc reconnect correctly.

Can someone hel me?
My server code:

error_reporting(E_ALL);

/* Allow the script to hang around waiting for connections. */
set_time_limit(0);

/* Turn on implicit output flushing so we see what we're getting
 * as it comes in. */
ob_implicit_flush();

$address = ipserver;
$port = 10001;    

if (($sock = socket_create(AF_INET, SOCK_STREAM, getprotobyname("TCP"))) === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
    exit;
}

if (!socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1)) {
    echo socket_strerror(socket_last_error($sock));
    exit;
} 

if (socket_bind($sock, $address, $port) === false) {
    echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
    exit;
}

if (socket_listen($sock, 5) === false) {
    echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
    exit;
}

     while(true)
{
    //$remote_fd = socket_accept($sock);
    if (($remote_fd = socket_accept($sock)) === false) {
        echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
        break ;
    }
    do {
        $recv = "";
        if (false === ($recv = socket_read($remote_fd, 128, PHP_BINARY_READ))) {
            echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($remote_fd)) . "\n";
            #break 2;
        }
        if($recv != "") {
            echo $recv."\n";
            inserisci_letture("t_letture",trim($recv));
            if($sent=socket_write($remote_fd,"1",1)===false)
            {
                echo "socket_write() failed: reason: " . socket_strerror(socket_last_error($remote_fd)) . "\n";
            }
        }
    } 
    while($recv != ""); 
}
socket_shutdown($sock);
socket_close($sock);


?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:58:53+00:00Added an answer on May 25, 2026 at 1:58 pm

    Listen on 0.0.0.0 instead of a specific interface, so that the socket is not closed if the network (the interface) does down. This is a special address that listens on all interfaces.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I want to post some data from silverlight to a website. I found
Hello guys i am grabing results from my mysql db and have made a
I just installed Ubuntu 8.04 and Eclipse. I made a very simple Hello World
Hello again ladies and gents! OK, following on from my other question on ASP.NET
Hello I am compiling a program with make but I get the error of
i made an image resizer in php. When an image is resized, it caches
THIS PROBLEM IS NOW SOLVED. THANK YOU TO EVERYONE WHO REPLIED. Hello, I am
Hello i am working on a menu in wordpress and made it dynamicly like
I'm trying to add some functionality from a plugin I have made into a
I made a Hello purr App in Google AppInventor. Afterwards i clicked on Package

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.