mcky Posted July 19, 2008 Share Posted July 19, 2008 Hello, Basically I'm very new to PHP and am trying to write an IRC bot that will also listen to a game server. I can get both sockets to work fine individually, but I don't know how to have them work simultaneously (ie. something is triggered by the game server socket, and then a command is sent to the IRC socket). These are cut down versions of each socket: $socket = stream_socket_server('udp://123.456.789:1234', $errno, $errstr, STREAM_SERVER_BIND); if ($socket) { while (!feof($socket)) { $data = stream_socket_recvfrom($socket, 4096); /* Do stuff */ } fclose ($connection); } else { print "Unable to connect!\n"; } $con = array(); init(); function init() { global $con, $CONFIG; $con['socket'] = fsockopen($CONFIG['server'], $CONFIG['port']); if (!$con['socket']) { print ("Could not connect to: ". $CONFIG['server'] ." on port ". $CONFIG['port']); } else { while (!feof($con['socket'])) { $con['buffer']['all'] = trim(fgets($con['socket'], 4096)); /* Do stuff */ } } } Can anyone help me out/point me in the right direction? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/115550-solved-multiple-sockets/ Share on other sites More sharing options...
ratcateme Posted July 19, 2008 Share Posted July 19, 2008 you could open both sockets then run a while loop with while(true) and inside do !fefo checks with two if statements on for each socket Quote Link to comment https://forums.phpfreaks.com/topic/115550-solved-multiple-sockets/#findComment-594054 Share on other sites More sharing options...
mcky Posted July 19, 2008 Author Share Posted July 19, 2008 I managed to get that method working for two UDP sockets (a duplicate of $socket) but I can't seem to get it to work with the IRC socket. Quote Link to comment https://forums.phpfreaks.com/topic/115550-solved-multiple-sockets/#findComment-594075 Share on other sites More sharing options...
mcky Posted July 19, 2008 Author Share Posted July 19, 2008 I guess what I'm asking is if someone could show me an example using the code I gave in the first post. Quote Link to comment https://forums.phpfreaks.com/topic/115550-solved-multiple-sockets/#findComment-594102 Share on other sites More sharing options...
mcky Posted July 20, 2008 Author Share Posted July 20, 2008 This is what I've come up with so far. $irc = fsockopen($CONFIG['server'], $CONFIG['port']); $socket = stream_socket_server('udp://123.456.789:12341', $errno, $errstr, STREAM_SERVER_BIND); if (!$irc) { print ("Could not connect to: ". $CONFIG['server'] ." on port ". $CONFIG['port']); } else { /* Initial info for the IRC server */ cmd_send("USER ". $CONFIG['nick'] ." nothing nothing :". $CONFIG['name']); cmd_send("NICK ". $CONFIG['nick'] ." nothing"); } while (true) { if (!feof($socket)) { $data = stream_socket_recvfrom($socket, 4096); echo "1".$data; unset($data); } if (!feof($irc)) { $con['buffer']['all'] = trim(fgets($irc, 4096)); } else { /* Nothing yet */ } } If I comment out the if (!feof($socket)) statement, it connects to the IRC server fine, but as soon as I leave it in, the script just hangs. Can anyone help? Quote Link to comment https://forums.phpfreaks.com/topic/115550-solved-multiple-sockets/#findComment-594775 Share on other sites More sharing options...
mcky Posted July 21, 2008 Author Share Posted July 21, 2008 I've realised that the sockets do work as long as there is data coming for both. If one socket isn't receiving any data, the other socket hangs as well. Is there a simple way to fix this? Quote Link to comment https://forums.phpfreaks.com/topic/115550-solved-multiple-sockets/#findComment-595238 Share on other sites More sharing options...
.josh Posted July 21, 2008 Share Posted July 21, 2008 php doesn't support threading (asynchronous processing). Google "php forking" Quote Link to comment https://forums.phpfreaks.com/topic/115550-solved-multiple-sockets/#findComment-595245 Share on other sites More sharing options...
mcky Posted July 21, 2008 Author Share Posted July 21, 2008 Well that was surprisingly easy. Thanks Crayon Violent. Quote Link to comment https://forums.phpfreaks.com/topic/115550-solved-multiple-sockets/#findComment-595254 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.