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
Share on other sites

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.