Jump to content

PHP Sockets


j.smith1981

Recommended Posts

I am interested in learning about PHP sockets.

 

I am reading from the O'Reilly book titled 'PHP in a Nutshell' on Chapter 20 'Sockets' and have done my own variation of this code (not that this is complete but just wanted to ask a question about this', used comments to explain gotcha's kind of what I think a function I am not sure about does:

 

<?php

$port = 1024;

$simple_socket = socket_create_listen("$port"); // simply opens a port!

if(!$simple_socket) {
  echo "Failed to open socket: 1024 please make sure it's not in use by another service\n";
  exit; // exit script when socket was not openable

  } else {
  
    // if socket was created:
    echo "Socket was created on Port: $port\n";
    
while(true) {
  
  $client = socket_accept($simple_socket);
  
  $welcome  = "\n Welcome to my test CLI client for hsmedia.co.uk\n";
  $welcome .= "Type '!close' to close this connection, or type '!halt' to halt the server\n";
  
  socket_write($client, $welcome); // writes what ever data is in the socket, so if a client has written output to the socket, this displays that or vice versa, ie sends that data to the client?
}

}

 

socket_write() (in this case only) sends data to the client presumably (though this examples extremely vaige!) of what they have entered?

 

Just out of interest basically, because I spent some time writing client apps in Flash to do with network games and fancy having a bash at using Linux to write my own services to do with multiplayer games in Flash Action Script, just thought it'd be allot of fun to continue on with some of my University work on my own, since I sadly lost the tutors server application.

 

Any feedback will be interesting and much appreciated,

Jeremy.

Link to comment
https://forums.phpfreaks.com/topic/245012-php-sockets/
Share on other sites

Ahh I know how this works now, done a few additions to this just using Linux though!

 

<?php

$simple_socket = socket_create_listen("1024"); // simply opens a port!

if(!$simple_socket) {
  echo "Failed to open socket: 1024 please make sure it's not in use by another service\n";
  exit; // exit script when socket was not openable
}

// if socket was created:
echo "Socket was created on Port: 1024\n";
    
while(true) {

  $client = socket_accept($simple_socket); // what the user goes into when a client connects?
  
  $welcome  = "\n Welcome to my test CLI client for hsmedia.co.uk\n";
  $welcome .= "Type '!close' to close this connection, or type '!halt' to halt the server\n";
  $exit = "You are being exited from this message service, please come back soon!\n";
  $close = "You have closed down this service, goodbye!\n";
  
  socket_write($client, $welcome); // writes what ever data is in the socket, so if a client has written output to the socket, this displays that or vice versa, ie sends that data to the client?

  while(true) { // while reads 1

    $input = trim(socket_read($client, 256)); // save what's in the socket for that user, or the person seeing the server connected rather!
  
    if($input == '!close'){
  socket_write($client, $exit);
      break; // if the user has sent a command then make the loop exit client but won't disconect from service, see below logic!
    }
  
    if($input == '!halt') {
  socket_write($client, $close);
      socket_close ($client);
      break 2; // putting 2 makes this work, how?
    }
  
    // whilst in the loop display all messages:
    echo "Them: $input\n";
  } 
  socket_close($client); // when using '!halt' as a command this seems to bring up an invalid resource, ask!
}

socket_close($simple_socket);

 

Now it reads back results if the user !halts (stops the service) or closes their connection (when leaving the service running on the server), though I am testing on the same machine using telnet localhost then the port no of that!

 

Makes sense now!

Link to comment
https://forums.phpfreaks.com/topic/245012-php-sockets/#findComment-1258594
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.