Jump to content

Working with Sockets


KingOfHeart

Recommended Posts

I'm trying to use sockets to see if I can connect to a chatroom. I got it started but not sure where to go from here.

 

<?
$host = "79.99.135.253";
$port = 6667;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$data = "NICK OpenZelda";
$data .= chr(13);
$data .= 'USER whoever "thegaminguniverse.com" "irc.thegaminguniverse.nl" :OZ Website';
$data .= chr(13);
socket_connect($socket, $host, $port);
socket_sendto($socket, $data, strlen($data), 0,$host,$port);

socket_recv($socket, $buffer, 1024, 0);
echo $buffer;
?>

I managed to receive one message. However I know for a fact there were two messages sent back. So how do I set it up to keep receiving messages?

Link to comment
Share on other sites

Update!

I got it to stay connected and receive messages.

Now I wonder how I can send messages without disconnecting.

 

<?
$host = "79.99.135.253";
$port = 6667;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$data = "NICK Freddy_Krueger";
$data .= chr(13);
$data .= 'USER Freddy_Krueger "thegaminguniverse.com" "irc.thegaminguniverse.nl" :Murderer';
$data .= chr(13);
socket_connect($socket, $host, $port);
socket_sendto($socket, $data, strlen($data), 0,$host,$port);

$join;

while(true)
{
socket_recv($socket, $buffer, 1024, 0);
if(strlen($buffer) > 0)
	echo "Recieved: $buffer<br>";

$pingpos = strpos($buffer,"PING :");	
  $pingcode;	
if($pingpos !== false)
{	
	for($n = $pingpos + 6; $n < strlen($buffer); $n++)
    {
        if($buffer[$n] == 13 || $buffer[$n] == 10)
				 break;
        $pingcode .= $buffer[$n];

    }
    $data = "PONG :" . $pingcode . chr(13);		
    socket_sendto($socket,$data, strlen($data), 0,$host,$port);
     echo "Sent: $data<br>";	
    if($join == false)		
    {		
       $data = "JOIN #oz" . chr(13);		
		socket_sendto($socket,$data, strlen($data), 0,$host,$port);
     echo "Sent: $data<br>";
	}		 
}	
}	
?>

Link to comment
Share on other sites

It sounds like you want the MSG_DONTWAIT flag, aka "non-blocking".  That will let your code continue even if there's nothing to read from the socket.

 

Have you looked at socket_select() ?  That's one way to deal with the problem of knowing when to read and when to write to a socket.

 

I still don't know what you mean with "include".  Can you show me that code?

Link to comment
Share on other sites

<?
$host = "79.99.135.253";
$port = 6667;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$data = "NICK Freddy_Krueger2";
$data .= chr(13);
$data .= 'USER Freddy_Krueger2 "thegaminguniverse.com" "irc.thegaminguniverse.nl" :Murderer';
$data .= chr(13);
socket_connect($socket, $host, $port);
socket_sendto($socket, $data, strlen($data), 0,$host,$port);

$join;
$chatmsg;
while(true)
{
  include "msgs.php"; 
  if($join)
{
    echo "<u>" . $msgs . "| $nomsg</u><br>";	
	if($msgs != $chatmsg)
    {		
          $chatmsg = $msgs;
          $data = "PRIVMSG #oz :" . $chatmsg . chr(13);					
				socket_sendto($socket,$data, strlen($data), 0,$host,$port);
				echo "Sent: $chatmsg<br>";
	}							
  }	
  $sock = array($socket,$socket);
  if(socket_select($sock[0],$write = null, $except = null, null)	== 0)
	continue;
socket_recv($socket, $buffer, 1024, 1);
if(strlen($buffer) == 0)
	continue;
	echo "Recieved: $buffer<br>";
    $nomsg = 0;		
  

$pingpos = strpos($buffer,"PING :");	
  $pingcode;	
if($pingpos !== false)
{	
	for($n = $pingpos + 6; $n < strlen($buffer); $n++)
    {
        if($buffer[$n] == 13 || $buffer[$n] == 10)
				 break;
        $pingcode .= $buffer[$n];

    }
    $data = "PONG :" . $pingcode . chr(13);		
    socket_sendto($socket,$data, strlen($data), 0,$host,$port);
     echo "Sent: $data<br>";	
    if($join == false)		
    {		
       $data = "JOIN #oz" . chr(13);		
		  socket_sendto($socket,$data, strlen($data), 0,$host,$port);
        echo "Sent: $data<br>";
        $join = true;		 
	}		 
}	
}
?>

 

include "msgs.php";  < This is what I meant when I said include. The java part can wait a bit, just want to get it to send and receive properly first.

I don't think socketselect will work for just one socket. Even when I made it an array it still didn't work.

Link to comment
Share on other sites

I don't think socket select was needed as I got it to loop much faster now.

 

<?
$host = "79.99.135.253";
$port = 6667;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$data = "NICK Freddy_Krueger2";
$data .= chr(13);
$data .= 'USER Freddy_Krueger2 "thegaminguniverse.com" "irc.thegaminguniverse.nl" :Murderer';
$data .= chr(13);
socket_connect($socket, $host, $port);
socket_sendto($socket, $data, strlen($data), 0,$host,$port);

$join;
$chatmsg;
$n = 0;
while(true)
{
if ($n == 0)
  {	
	include "msgs.php"; 
	if($join)
	{
		echo "<u>" . $msgs . "| $nomsg</u><br>";	
		if($msgs != $chatmsg)
		{		
          $chatmsg = $msgs;
          $data = "PRIVMSG #oz :" . $chatmsg . chr(13);					
				socket_sendto($socket,$data, strlen($data), 0,$host,$port);
				echo "Sent: $chatmsg<br>";
		}							
	}
  }
  $n ++;
  if($n > 20)
		$n = 0;

if($nomsg > 50000)
{
	socket_close($socket);
} 
  
  socket_recv($socket, $recieve, 1024, 2);	
  
if($recieve == $buffer)
  {	
     $nomsg ++;		
	continue;
  }		
   $buffer = $recieve;		
	echo "Recieved: $buffer<br>";
    $nomsg = 0;		
  

$pingpos = strpos($buffer,"PING :");	
  $pingcode;	
if($pingpos !== false)
{	
	for($n = $pingpos + 6; $n < strlen($buffer); $n++)
    {
        if($buffer[$n] == 13 || $buffer[$n] == 10)
				 break;
        $pingcode .= $buffer[$n];

    }
    $data = "PONG :" . $pingcode . chr(13);		
    socket_sendto($socket,$data, strlen($data), 0,$host,$port);
     echo "Sent: $data<br>";	
    if($join == false)		
    {		
       $data = "JOIN #oz" . chr(13);		
		  socket_sendto($socket,$data, strlen($data), 0,$host,$port);
        echo "Sent: $data<br>";
        $join = true;		 
	}		 
}	
}
?>

 

Looks like include will no longer work because of a 30 second execution time. So I'm hoping I can use Javascript to submit future messages.

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.