Jump to content

Socket questions..


tomfmason

Recommended Posts

This should be realitvely simple but I am running into an issue with writing to a socket..

Here is the code in question..

[code]
<?php
$config = array();
$config['server'] = 'irc.freenode.net';
$config['port'] = 6667;
$config['nick'] = 'nick';
$config['name'] = 'name';
$config['host'] = 'localhost';
$config['channel'] = '#phpfreaks';
$config['password'] = 'password';

set_time_limit(0);
$con = array();

function cmd_send($command, $con) {
    $put = fputs($con['socket'], $command . "\r\n");
if (!$put) {
    echo 'Error : Unable to write to the socket';
} else {
    print (date("[d/m @ H:i]") ."-> ". $command. "\n\r");
}
}

function startBot($config, $con) {
  $con['sock'] = fsockopen($config['server'], $config['port']);
  $firstTime = true;
 
  if (!$con['sock']) {
      echo "Unable to connect to " . $config['server'];
  } else {
  while (!feof($con['sock'])) {
        $con['buffer']['all'] = trim(fgets($con['sock'], 4096));
if ($firstTime == true) {
  cmd_send('Nick ' . $config['nick'], $con);
  cmd_send('User ' . $config['nick'] . ' ' .  $config['host'] . ' ' . $config['server'] . ' :' . $config['name'], $con);
}
print date("[d/m @ H:i]")."<- ". $con['buffer']['all'] . "\n";
if(substr($con['buffer']['all'], 0, 6) == 'PING :') {
  cmd_send('PONG :'.substr($con['buffer']['all'], 0, 6), $con);
 
  if ($firstTime == true){
      cmd_send('Nick ' . $config['nick'], $con);
              cmd_send('User ' . $config['nick'] . ' ' .  $config['host'] . ' ' . $config['server'] . ' :' . $config['name'], $con);
      cmd_send("JOIN ". $config['channel'], $con);
      $firstTime = false;
  }
} elseif ($old_buffer != $con['buffer']['all']) {
  //do something;
}
$old_buffer = $con['buffer']['all'];
  }
    }
}     
startBot($config, $con);     
?>[/code]

I am getting the error that fputs is not a valid resource..(in the cmd_send function)..

Any suggestions would be great..

Thanks,
Tom
Link to comment
https://forums.phpfreaks.com/topic/26525-socket-questions/
Share on other sites

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.