php simple socket_listen - php

I have the following :
$host = "192.168.0.117";
$port = 777;
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not createsocket\n");
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
The variable result gives me an error:
unable to listen on socket [0]: An invalid argument was supplied

You need to bind a socket to (address, port) pair first and only then call listen on it.

$host = '192.168.0.117';
$port = '777';
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, 0);
socket_bind($socket, $adddress, $port) or die("Could not connect");
socket_listen($socket);
$client_connection = socket_accept($socket);
$input = socket_read($client_connection, 1024);
$output = ereg_replace('[\t\n\r]','', $input).chr(0);
//display output
socket_write($client_connection, $output);
socket_close($client_connection);
socket_close($socket);

Related

Send php array to a socket

How to send a php array to a socket server?
I have a webservice and i want to send a php array to a socket webservices.
How i can do it?
I have tried this:
$host = "170.51.249.101";
$port = 8080;
// No Timeout
set_time_limit(0);
// Create a Data to send
$data = "";
$data .= "ID_PRESTADOR: ".$service_id."\n";
$data .= "ID_TERMINAL: ".$atm_id."\n";
$data .= "NRO_TRANSACCION: 123456\n";
// Create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// Connect to the server
$result = socket_connect($socket, $host, $port) or die("Could not connect toserver\n");
// Write to server socket
socket_write($socket, $data, strlen($data)) or die("Could not send data to server\n");
// Read the response from the server
$result = socket_read ($socket, 1024) or die("Could not read server response\n");
echo "Reply From Server :".$result;
// Close the socket
socket_close($socket);
die;

How to access to socket object from a different php file

I want to send something to my connected clients over another php script. But when I use the function "send", clientlist is null.
Can you help me about this please?
Socket.php:
$clientlist = array();
function run() {
global $clientlist;
set_time_limit(0);
$address = '127.0.0.1';
$port = 80;
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
socket_bind($sock, 0, $port) or die('Could not bind to address');
socket_listen($sock);
printf("Listening...\r\n");
while (true) {
$client = socket_accept($sock);
$input = socket_read($client, 1024000);
$clientlist[] = $client;
}
}
function send($msg) {
global $clientlist;
printf("Count: " . count($clientlist) . "\r\n");
socket_write($clientlist[0], "Hey");
}
Msg.php:
include("socket.php");
send($_GET['msg']);

Different PHPSESSID in WebSocket handshake

When the WebSocket handshake is done I receive a PHPSESSID different from the one in the browser, why is this?
Client code for connecting:
websocket = new WebSocket("ws://192.168.0.109:9000/php_servers/socketserver1/socketserver.php");
Server code for reading header:
<?php
include "../serverfunctions.php";
include "eventfunctions.php";
$host = '192.168.0.109';
$port = '9000';
$null = NULL;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($socket, 0, $port);
socket_listen($socket);
$clients = array($socket);
while (true)
{
$changed = $clients;
socket_select($changed, $null, $null, 0, 10);
if (in_array($socket, $changed))
{
$socket_new = socket_accept($socket);
$clients[] = $socket_new;
$header = socket_read($socket_new, 1024);

PHP: Socket server interrupted by socket_recv()

I am writing PHP socket server, displayed here is error causing portion. I find out socket_recv() is causing problem, it only lets one computer connect. However, if I comment out socket_recv then its working fine. But I have to receive data also in socket server. Help me find out solution. Please also point out any wrong with code. JQuery part is working fine, hence didn't print it here.
<?php
set_time_limit(0);
$host = '172.28.4.5';
$port = 10000;
$null = NULL;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($socket, $host, $port);
socket_listen($socket);
$clients = array($socket);
while(true) {
$new_socket = socket_accept($socket);//Accepting new connection/socket/client if any
$clients[] = $new_socket;//Adding the new client/socket/connection to client array
$header = socket_read($new_socket, 1024);
perform_handshaking($header, $new_socket, $host, $port);
/* If I want to notify if new connection is established**/
socket_getpeername($new_socket, $ip);
$message = "Welcome to WebSocket $ip";
$array = array(
'message' => $message
);
$message = mask(json_encode($array));
write_to_socket($message);
$found = array_search($socket, $clients);
unset($clients["$found"]);
//Going through each client
foreach($clients as $client) {
//Getting messages with loop how many packages for each client has
while(socket_recv($client, $buf, 1024, 0) >= 1) {
$array = json_decode(unmask($buf));
print_r($array);
}
}
}
}
?>
it seems that
the code is wrong
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($socket, $host, $port);
socket_listen($socket);
//$clients = array($socket); <---- here
while(true) {
it seems that you add the listened socket to array , you should not receive and data from the listened socket.

How can i run socket server in PHP?

How can i run socket server for each www client?
I have simple socket_server:
<?php
// set ip and port
include('socket.php');
// don't timeout!
set_time_limit(0);
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "-1";
exit;
}
// bind socket to port
$result = socket_bind($socket, $host, $port) or die(socket_strerror(socket_last_error($socket)));
// start listening for connections
$result = socket_listen($socket, 3);
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
echo "Server: Ready.";
while(1) {
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");
// clean up input string
$input = trim($input);
echo "Client Message : ".$input."<br />";
// reverse client input and send back
//$output = strrev($input) ."<br />";
$output = "output message";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
}
// close sockets
socket_close($spawn);
socket_close($socket);
?>
Now when i try run server in this way:
<?php
//include the server.php script to start the server
include_once('server.php');
include('socket.php');
$message = "Hello Server";
echo "Message To server :".$message;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// connect to server
$result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");
// send string to server
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
// get server response
$result = socket_read ($socket, 1024) or die("Could not read server response\n");
echo "Reply From Server :".$result;
// close socket
socket_close($socket);
?>
I get the message:
Only one usage of each socket address (protocol/network address/port) is normally permitted.
I do not have any other communications on this port. I checked the netstat.

Categories