I have hosting domain godaddy and i enabled socket module, but dose not work
this code works only in localhost when i upload it in server it does not work.
code server.php
<?php
// set some variables
$host = '150.113.178.20';
$port = 5000;
if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0)))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg \n");
}
echo "Socket created \n";
// Bind the source address
if( !socket_bind($sock, $host , $port) )
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not bind socket : [$errorcode] $errormsg \n");
}
echo "Socket bind OK \n";
if(!socket_listen ($sock , 10))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not listen on socket : [$errorcode] $errormsg \n");
}
echo "Socket listen OK \n";
echo "Waiting for incoming connections... \n";
//start loop to listen for incoming connections
while (true)
{
//Accept incoming connection - This is a blocking call
$client = socket_accept($sock);
//display information about the client who is connected
if(socket_getpeername($client , $address , $port))
{
echo "Client $address : $port is now connected to us. \n";
}
//read data from the incoming socket
$input = socket_read($client, 1024000);
$response = "OK .. $input";
// Display output back to client
socket_write($client, $response);
}
when i execute server.php script in ssh no problem
but when i write from CMD :
Error log file
After some research i found that The code is correct
Godaddy does not open custom ports.
The code does not work because have a different security policy localhost than GoDaddy (or other hosting website).
Related
I want to make udp listener on windows but I failed to do so, I tried couple of codes which available online but it didn't work for me, it seems that my windows php configuration is not done properly so code is not able to connect to socket, the code keeps waiting for messages but it receive nothing, I tested my network using two PCs with ready UDP sender/receiver app, and it works fine,
please check my code below and the output, please let me know if I should do something in php.ini
<?php
//Reduce errors
error_reporting(~E_WARNING);
//Create a UDP socket
if(!($sock = socket_create(AF_INET, SOCK_DGRAM, 0)))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg \n");
}
echo "Socket created \n";
// Bind the source address
if( !socket_bind($sock, "127.0.0.1" , 1235) )
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not bind socket : [$errorcode] $errormsg \n");
}
echo "Socket bind OK \n";
//Do some communication, this loop can handle multiple clients
while(1)
{
echo "Waiting for data ... \n";
//Receive some data
$r = socket_recvfrom($sock, $buf, 512, 0, $remote_ip, $remote_port);
echo "$remote_ip : $remote_port -- " . $buf;
//Send back the data to the client
socket_sendto($sock, "OK " . $buf , 100 , 0 , $remote_ip , $remote_port);
}
socket_close($sock);
cmd php code with output
Im trying to create a simple UDP server on php which initially will be receiving some dummy data. I've tried following some tutorials and now i can create and bind a socket. However when im using socket_recvfrom for some reason the page just keeps loading and when i refresh it again, the port is taken and i cant bind the socket in that port anymore. below is my code and any answer is welcomed.
`
if(!($sock=socket_create(AF_INET, SOCK_DGRAM, 0)))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldnt create socket: [$errorcode] $errormsg \n");
}
echo "socket create OK";
if (!socket_bind($sock,"localhost",8880))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldnt bind socket: [$errorcode] $errormsg \n");
}
echo "socket bind OK";
// while(1)
// {
$remote_ip ='';
$remote_port=0;
echo "waiting for data ... \n";
$r=socket_recvfrom($sock, $buf, 512 , 0 , $remote_ip , $remote_port);
echo "$remote_ip : $remote_port --". $buf;
// }
// socket_close($sock);
I ended up using this function:
if (false ===($tcpsock = stream_socket_server("tcp://127.0.0.1:8876",$errorno,$errstr)))
echo"tcp socket failed : $errstr($errorno)\n";
if(false ===($connection = stream_socket_accept($tcpsock)))
echo "tcp accept failed!\n";
else echo "tcp accepted";
And if you want a UDP socket, you just have to :stream_socket_server("udp://127.0.0.1:8876",$errorno,$errstr)))
<?php
if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0)))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg \n");
}
echo "Socket created \n";
// Bind the source address
if( !socket_bind($sock, "127.0.0.1" , 5000) )
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not bind socket : [$errorcode] $errormsg \n");
}
echo "Socket bind OK \n";
if(!socket_listen ($sock , 10))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not listen on socket : [$errorcode] $errormsg \n");
}
echo "Socket listen OK \n";
echo "Waiting for incoming connections... \n";
//Accept incoming connection - This is a blocking call
$client = socket_accept($sock);
//display information about the client who is connected
if(socket_getpeername($client , $address , $port))
{
echo "Client $address : $port is now connected to us.";
}
socket_close($client);
socket_close($sock);
?>
This code not working with command line.
No output is display in commend line after enter
But when i try in browser it works in infinite loop.
May be this is fine or another way to run a service in php
These codes build a tcp server,You can use telnet or netcat to access the server.
You also can use browser to access the server,like "http://127.0.0.1:5000/".Now you will see the command line output.
socket_accept blocks the proccess,until a request reach.You can visit this page for more information.
I'm developing a new socket based application for interacting with my clients and I want to achieve that using socket programming.
Let's say my server socket is running on 127.0.0.1 on 8888 port. when a client connected to my server & if he trying to send a message to admin where admin is already connected from a different socket client.
But I'm facing a problem exchanging data between admin & client sockets. However the server socket is accepting the data from the admin & client as well. When I'm trying to send client data to admin its not passing over.
server.php
<?
set_time_limit(0);
// Set the ip and port we will listen on
$address = '127.0.0.1';
$port = 8888;
// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0) or die('Could not bind to address'); // 0 for SQL_TCP
// Bind the socket to an address/port
socket_bind($sock, 0, $port) or die('Could not bind to address'); //0 for localhost
// Start listening for connections
socket_listen($sock);
$admin = null;
$clients = array();
while(true) {
/* Accept incoming requests and handle them as child processes */
$client = socket_accept($sock);
if(socket_getpeername($client, $address, $port)) {
echo "Client $address : $port is now connected to us. \n";
}
// Read the input from the client – 2MB bytes
$input = socket_read($client, 2097152);
$data = json_decode($input);
print_r($data);
if($data->connection === "admin"){
echo "Admin connection\n";
$admin = $client;
}else if($data->connection === "client"){
echo "Got data from client\n";
$message = array('client_id' => $data->id, 'message' => $data->message);
$clients[$data->id] = $client;
if($admin !== null){
echo "Sending data to admin\n";
socket_write($admin, json_encode($message), strlen(json_encode($message)));
}else{
echo "Admin not available, sending response to client\n";
$message = 'Admin not available!';
socket_write($client, $message, strlen($message));
}
}
print_r($clients);
// socket_write($client, $response);
// socket_close($client);
}
// Close the master sockets
socket_close($sock);
?>
admin.php
<?
set_time_limit(0);
$server = '128.199.XXX.XXX';
$port = '8888';
if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0))) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg \n");
}
echo "Socket created \n";
//Connect socket to remote server
if(!socket_connect($sock, $server, $port)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not connect: [$errorcode] $errormsg \n");
}
echo "Connection established \n";
$message = array('connection' => 'admin');
//Send the message to the server
if(!socket_send($sock, json_encode($message), strlen(json_encode($message)), 0)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not send data: [$errorcode] $errormsg \n");
}
echo "Message send successfully \n";
if(socket_recv($sock, $buf, 10, MSG_WAITALL) === FALSE) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not receive data: [$errorcode] $errormsg \n");
}
print_r(json_decode($buf));
?>
client.php
<?
set_time_limit(0);
$server = '128.199.XXX.XXX';
$port = '8888';
if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0))) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg \n");
}
echo "Socket created \n";
//Connect socket to remote server
if(!socket_connect($sock, $server, $port)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not connect: [$errorcode] $errormsg \n");
}
echo "Connection established \n";
$message = array('connection' => 'client', 'id' => time(), 'message' => 'Hi');
//Send the message to the server
if(!socket_send($sock, json_encode($message), strlen(json_encode($message)), 0)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not send data: [$errorcode] $errormsg \n");
}
echo "Message send successfully \n";
if(socket_recv($sock, $buf, 10, MSG_WAITALL) === FALSE) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not receive data: [$errorcode] $errormsg \n");
}
print_r(json_decode($buf));
?>
Can anybody done this before? lease help me to achieve it.
Thanks in advance :-)
I have a google compute engine instance running. I installed php5, apache2 and mysql on the compute engine instance.
I have opened up the port in the firewall settings as well "tcp:3000"
I have then put this code up in /var/www/ folder to open a php server and do some custom replies.
However Its not able to open and bind socket. Can you please help?
This is my code:
<?php
error_reporting(0);
set_time_limit(0);
date_default_timezone_set('Asia/Calcutta');
$address = 'localhost';
$port = 3000;
$max_clients = 10;
$datetime = date("Y-m-d H:i:s");
if(!($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg \n");
writeToFile('edmerrorlog.txt', $datetime." :: ".'Couldnt create socket: ['.$errorcode.']'. $errormsg);
}
echo "New Socket created \n";
/*if (!socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1)) {
echo socket_strerror(socket_last_error($sock));
writeToFile('edmerrorlog.txt', $datetime." :: ".socket_strerror(socket_last_error($sock)));
exit;
}
*/
// Bind the source address
if( !socket_bind($sock, $address , $port) )
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not bind socket : [$errorcode] $errormsg \n");
writeToFile('edmerrorlog.txt', $datetime." :: ".'Could not bind socket : ['.$errorcode.']'. $errormsg);
}
echo "Socket bind OK \n";
if(!socket_listen ($sock , 10))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not listen on socket : [$errorcode] $errormsg \n");
writeToFile('edmerrorlog.txt', $datetime." :: ".'Could not listen on socket1 : ['.$errorcode.']'. $errormsg);
}