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