Sindbad~EG File Manager

Current Path : /home/beeson/public_html/WebMathXpert/
Upload File :
Current File : //home/beeson/public_html/WebMathXpert/testSocket.php

<?php
// Direct socket connection test
$serverAddress =  '127.0.0.1';  // 'https://localhost';
$serverPort = 12349;
session_start();
$sessionId = session_id(); 
function my_send($socket, $message)
// returns the number of bytes sent
{
  $sessionId = session_id(); 
  $data = $sessionId . "|" . $message;
$sent = 0;
$messagelength = strlen($data); 

// first send six characters with the messagelength
$messagelength_string = sprintf("%6d",strval($messagelength));
while($sent < 6)
{   
	$more = socket_write($socket,$messagelength_string,6-$sent);	
		if ($more === false || $more < 0)
		  {   echo "Socket write failed: " . socket_strerror(socket_last_error()) . "<br>";
			  die();
		  }
		$sent = $sent + $more;
}
$sent = 0;
// echo("sent 6 bytes, now to send $data of length $messagelength <br>");
while($sent < $messagelength)
{  $resttosend = substr($data, $sent);
	$bytestogo = $messagelength - $sent;
       // echo "About to write $bytestogo bytes<br>";
	   $more = socket_write($socket,$resttosend ,$bytestogo);
       // echo "The moving finger writes  $more bytes out of $messagelength<br>";
   if($more == 0)
	   { 
		   echo("Goodbye, cruel world, server has disconnected.");
		   return 0;
	   }
	if ($more < 0)
	  {   echo "Socket write failed: " . socket_strerror(socket_last_error()) . "<br>";
		  return $more;
	  }
	$sent = $sent + $more;
} 
// echo("Sent $sent bytes");
return $sent;		   
}


function sendMessage($socket, $message, $param)
{   
 // echo "<Sending $message<br>";
 if(! is_string($message) || strlen($message) == 0)
 { echo ("Cannot send an empty message<br>");
   return;
 }
 if(! is_string($param) || strlen($param) == 0)
 {
	 echo("Cannot send a message $message with empty parameter<br>");
	 return;
 } 
 
 my_send($socket, "$message|$param");  
 echo("sent the message");
   // $response = my_receive($socket, 1 << 21);  // 1 << 21 is the buffersize
 // return $response;
}


$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

$result = socket_connect($socket, $serverAddress, $serverPort);
if ($result === false) {
    die("Unable to connect to server: " . socket_strerror(socket_last_error($socket)));
}

$message = "askProblemsText";
$param = "72";
// Send message to the Engine using sendMessage function
try {
    $response = sendMessage($socket, $message, $param);
} catch (Exception $e) {
    // Handle any exceptions from sendMessage
    fclose($socket);
    http_response_code(500);
    echo "Error sending message to Engine: " . $e->getMessage();
    exit;
}

// Close the socket connection
socket_close($socket);

// Respond back to the fetch request with the Engine's response
echo $response;

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists