Jump to content

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?

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.