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
https://forums.phpfreaks.com/topic/215741-working-with-sockets/
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>";
	}		 
}	
}	
?>

Just a file with a single variable.

I have the word include within the loop. I'm thinking it just takes it a while to understand it's updated.

Do you think a submit button would work for submitting data?

How would you script a simple forum with a submit button that uses java script?

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?

<?
$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.

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.

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.