Jump to content

Socket Help


Pro.Luv

Recommended Posts

Hi,

 

I read online documentation but I stil can't get this right I need to create a PHP page that will send data to another PHP page using sockets so far I got this:

 

client.php

function ping($host) {
    $package = "\x08\x00\x19\x2f\x00\x00\x00\x00\x70\x69\x6e\x67";

    /* create the socket, the last '1' denotes ICMP */   
    $socket = socket_create(AF_INET, SOCK_RAW, 1);
   
    /* set socket receive timeout to 1 second */
    socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 1, "usec" => 0));
   
    /* connect to socket */
    socket_connect($socket, $host, null);
   
    /* record start time */
    list($start_usec, $start_sec) = explode(" ", microtime());
    $start_time = ((float) $start_usec + (float) $start_sec);
   
    socket_send($socket, $package, strlen($package), 0);
   
    if(@socket_read($socket, 255)) {
        list($end_usec, $end_sec) = explode(" ", microtime());
        $end_time = ((float) $end_usec + (float) $end_sec);
   
        $total_time = $end_time - $start_time;
        $great = "Working !!";
        
        return $total_time." - ".$great;
        
    } else {
        return false;
    }
   
    socket_close($socket);
}

  ping("127.0.0.1");

 

when I use the function it passes back to the same page I need it to send to this page

 

server.php


    error_reporting(E_ALL | E_STRICT);
    
    $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
    socket_bind($socket, '127.0.0.1', 1223);
    
    $from = "127.0.0.1";
    $port = 80;
    socket_recvfrom($socket, $buf, 12, 0, $from, $port);
    
    echo "Received $buf from remote address $from and remote port $port" . PHP_EOL;
    

 

I really need help with this.

 

Thanks

 

 

Link to comment
https://forums.phpfreaks.com/topic/173863-socket-help/
Share on other sites

Your server code is completely wrong. The socket you have created there is not used to communicate with the client, but to listen for the incoming connections. You need two more functions: socket_listen() and socket_accept(). Moreover, the server must be running all the time.

 

When socket_accept() accepts a connection, it creates a new socket to talk to the connected client. As you finish the connection processing, you close it and get back to listening on the main socket. This will be something like that:

 

$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
socket_bind($socket,'127.0.0.1',$port);
socket_listen($socket);

while(true)
{
   if(is_resource($newSocket = socket_accept($socket)))
   {
      // do something with $newSocket
      socket_close($newSocket);
   }
}
socket_close($socket);

Link to comment
https://forums.phpfreaks.com/topic/173863-socket-help/#findComment-916538
Share on other sites

Thanks alot for the reply, the client.php is right ? is there anything I need to add in there so that I can send data to the server.php ?

 

Really appreciate your help.

 

My server.php now is:

 


    $socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
    socket_bind($socket,'127.0.0.1', $port);
    socket_listen($socket);
    
    while(true)
    {
       if(is_resource($newSocket = socket_accept($socket)))
       {
          // do something with $newSocket
          echo $newSocket;
          socket_close($newSocket);
       }
    }
    socket_close($socket);

Link to comment
https://forums.phpfreaks.com/topic/173863-socket-help/#findComment-916546
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.