Lumio Posted April 20, 2007 Share Posted April 20, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/47850-solved-create-socket-and-listen/ Share on other sites More sharing options...
Guest prozente Posted April 20, 2007 Share Posted April 20, 2007 This is UDP, ensure you're trying to connect using UDP and not TCP Quote Link to comment https://forums.phpfreaks.com/topic/47850-solved-create-socket-and-listen/#findComment-233826 Share on other sites More sharing options...
Lumio Posted April 20, 2007 Author Share Posted April 20, 2007 Ah right Do you know how to make it with TCP? I didn't found one. Quote Link to comment https://forums.phpfreaks.com/topic/47850-solved-create-socket-and-listen/#findComment-233833 Share on other sites More sharing options...
Guest prozente Posted April 20, 2007 Share Posted April 20, 2007 change SOL_UDP to SOL_TCP Quote Link to comment https://forums.phpfreaks.com/topic/47850-solved-create-socket-and-listen/#findComment-233835 Share on other sites More sharing options...
Lumio Posted April 20, 2007 Author Share Posted April 20, 2007 -.- I'm a fool thanks Quote Link to comment https://forums.phpfreaks.com/topic/47850-solved-create-socket-and-listen/#findComment-233846 Share on other sites More sharing options...
Lumio Posted April 20, 2007 Author Share Posted April 20, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/47850-solved-create-socket-and-listen/#findComment-233936 Share on other sites More sharing options...
Guest prozente Posted April 21, 2007 Share Posted April 21, 2007 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())); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/47850-solved-create-socket-and-listen/#findComment-234605 Share on other sites More sharing options...
Lumio Posted April 21, 2007 Author Share Posted April 21, 2007 thank you 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] Quote Link to comment https://forums.phpfreaks.com/topic/47850-solved-create-socket-and-listen/#findComment-234611 Share on other sites More sharing options...
Guest prozente Posted April 21, 2007 Share Posted April 21, 2007 Look at the code I provided, it works. Quote Link to comment https://forums.phpfreaks.com/topic/47850-solved-create-socket-and-listen/#findComment-234618 Share on other sites More sharing options...
Lumio Posted April 21, 2007 Author Share Posted April 21, 2007 ah thanks! Quote Link to comment https://forums.phpfreaks.com/topic/47850-solved-create-socket-and-listen/#findComment-234623 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.