Jump to content

[SOLVED] Multiple sockets


mcky

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/115550-solved-multiple-sockets/
Share on other sites

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.