Sanjeev Kumar Jha
Sanjeev Kumar Jha

Reputation: 1263

Transport endpoint is not connected

My code is this but not working

<?php
    $sock = socket_create(AF_INET, SOCK_STREAM, 0);
    $sk = socket_connect($sock,"10.0.1.43","1234");
    socket_set_nonblock($sock);

    while (1) {        
        sleep(2);
        $buffer=socket_read($sock,512);
        echo "Buffer = $buffer \n";
        echo "Last Error = ".socket_last_error($sock).socket_strerror(socket_last_error($sock))."\n";        
    }        
?>

It display error:

unable to read from socket [107]: Transport endpoint is not connected PHP Stack trace: Buffer =
Last Error = 107Transport endpoint is not connected

Thanks

Upvotes: 2

Views: 10197

Answers (3)

trejder
trejder

Reputation: 17505

As I wrote in a comment to previous answer, you can find fine example of solving this problem on PHP.net Examples Page. In the main article you'll find a good example, how to make a listener without error, you're refering to, for one client and in javier's notice below, how to have the same for multi-client purpose.

Upvotes: 0

Bogdan Constantinescu
Bogdan Constantinescu

Reputation: 5356

You have to accept a connection first! Inside your while() loop, do another while() like this:

while($client = socket_accept($sock)) {
    $buffer=socket_read($client, 512);
    echo "Buffer = $buffer \n";
}

It should work, as you intend.

Upvotes: 2

Martin James
Martin James

Reputation: 24887

OK, so the client socket is not connected.

What is protocol '0'? Are you sure that '0' is TCP on your system? Not sure if you can change the socket block/non-block state after a connect() - never tried such a thing. If $sk is false, what is the last error? Is the server at 10.0.1.43:1234 reachable with TCP?

Rgds, Martin

Upvotes: 1

Related Questions