Jump to content

Recommended Posts

I got an example to create a socket and listen:

<?php
set_time_limit(0);
// example of "daemon" in UDP
while(TRUE) { // never stop the daemon
    $socketD = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); // create an UDP socket

    echo 'Creating socket... <br>'; // YES, the socket is creating EACH TIME the while loops

    if($socketD === FALSE) { //omg, something went wrong
        echo 'socket_create failed: '.socket_strerror(socket_last_error())."\n";
        exit(1);
    }
   
    // let the daemon listen on the port 27123 on all interfaces
    if(!@socket_bind($socketD, "localhost", 1026)) {
        socket_close($socketD);
        echo 'socket_bind failed: '.socket_strerror(socket_last_error())."\n";
        exit(1);
    }
   ob_flush();
    echo "OK\nNow ready to accept connections.\nListening on the socket ... \n";
    // so far, our socket is open and bound to a port: it's listening for ONE incoming connection

    // this is a special way of socket_read()'ing what's on the socket once someone establishes a connection
    socket_recvfrom($socketD, $buf, 65535, 0, $clientIP, $clientPort);
    if($buf === FALSE) { // something went wrong
        echo 'socket_read() returned false : '.socket_strerror(socket_last_error())."\n";
        continue;
    } elseif(strlen($buf) === 0) { // this should mean "client closed the connection"
        echo 'socket_read() returned an empty string : '.socket_strerror(socket_last_error())."\n";
        continue;
    }

    echo 'Incoming connection from '.$clientIP.':'.$clientPort.' ... ';

    // ok now thats the tricky part
    // if you want to reply to the client on the UDP socket it opened, you need to connect your listening socket on them
    // this won't work unless the client puts their IP in the IP   header of the packets it's sending to your socket
    // UDP sessions don't require them to do so, and if they don't, you have no way to tell who's talking to you on the socket, and therefore no way to reply
    // so if you want to interact with the client, you have to connect your socket on them, but by doing so, you're no longer listening to other incoming connections
    // one way to fixe that would be to fork the script when an incoming connection arrives and then stop the forked script once the client exits
    if(!socket_connect($socketD, $clientIP, $clientPort)) {
        echo 'socket_create failed to connect the clientsock on '.$clientIP.':'.$clientPort.' : '.socket_strerror(socket_last_error())."\n";
        socket_close($socketD);
        continue;
    }
    echo 'connected'."\n"; // cool, now we can reply to the client
ob_flush();
    // now you can interact with the client using socket_write() and socket_read()

    // once you're finished, you need to socket_close() your sockets and let the while loop and re-create the listenning socket (because you're no longer listenning for new incoming connections)
} // end of the infinite while
?>

It tells me that everything works fine, but when I try to connect to that port, the connection refuses and does nothing.

 

Can someone help me?

Link to comment
https://forums.phpfreaks.com/topic/47850-solved-create-socket-and-listen/
Share on other sites

Hi!

I don't know what I'm making wrong.

My code...

<?php
echo 'Listen...';
@ob_flush();

$fd = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$port = 5000;
socket_bind($fd, "127.0.0.1", $port);

while(true) {
	usleep(25);
	if (($remote_fd = @socket_accept($fd)))
		remoteHandle($remote_fd);
}

function remoteHandle($fd) {
	socket_recvfrom($fd, $buf, 1024, 0, $clientIP, $clientPort);
	echo $buf;
	exit;
}
?>

When I try to connect to my server, the connection refuses.

Guest prozente

You forgot to run socket_listen

<?php
        function remoteHandle(&$remote_fd) {
                $buf = socket_read($remote_fd, 1024, PHP_NORMAL_READ);
                echo $buf;
                socket_close($remote_fd);
        }

        echo 'Listen...';

        $fd = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

        if(!socket_bind($fd, '127.0.0.1', 5000)){
                socket_close($fd);
                exit( 'socket_bind failed: '.socket_strerror(socket_last_error()));
        }

        if(!socket_listen($fd)){
                socket_close($fd);
                exit( 'socket_listen failed: '.socket_strerror(socket_last_error()));
        }

        while(true) {
                usleep(25);
                if (($remote_fd = socket_accept($fd))){
                        remoteHandle($remote_fd);
                }else{
                        socket_close($fd);
                        exit( 'socket_accept failed: '.socket_strerror(socket_last_error()));
                }
        }

?>

:o thank you :D

Now we're comming close...

<?php
//...

function remoteHandle($fd) {
	socket_recvfrom($fd, $buf, 1024, 0, $clientIP, $clientPort);
	echo $buf;
	exit;
}
?>

[code=php:0]socket_recvfrom($fd, $buf, 1024, 0, $clientIP, $clientPort);

this line makes trouble

Warning: socket_recvfrom() [function.socket-recvfrom]: Unsupported socket type 0 in /Users/lumio/Sites/temp.php on line 75
[/code]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.