I am trying to send a XML to a server in a TCP\IP (socket) connection.
My connection is okay. The sending part is the issue.
See below;
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$host = "xx.xxx.xx.xxx";
$port = xxxx;
// 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");
if($result === true){
echo 'connected';
}
$sendVendRequest='
<ipayMsg client="SAFEPAY" term="00001" seqNum="0" time=" '.date('Y-m-d H: i: s').' +0200">
<elecMsg ver="2.44">
<vendReq>
<ref>319155500001</ref>
<amt cur="KSh">1000</amt>
<numTokens>1</numTokens>
<meter>A12C3456789</meter>
<payType>cash</payType>
</vendReq >
</elecMsg>
</ipayMsg>';
$vendRequestXml=simplexml_load_string($sendVendRequest) or die("Error: could not create an object");
// print_r($vendRequestXml);
socket_write($socket, $sendVendRequest, strlen($sendVendRequest)) or die("Could not send data to server\n");
The sending part fails. It loads until it times out. I suspect I am sending the request wrongly.. Someone please direct me on how to achieve this.
This code works for me:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$host = "example.com";
$port = 80;
// 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");
if($result === true){
echo 'connected';
}ยท
$st='
<ipayMsg client="SAFEPAY" term="00001" seqNum="0" time=" '.date('Y-m-d H: i: s').' +0200">
<elecMsg ver="2.44">
<vendReq>
<ref>319155500001</ref>
<amt cur="KSh">1000</amt>
<numTokens>1</numTokens>
<meter>A12C3456789</meter>
<payType>cash</payType>
</vendReq >
</elecMsg>
</ipayMsg>';
$length = strlen($st);
while(true) {
$sent = socket_write($socket, $st, $length);
if($sent === false) {
break;
}
// Was the entire msg sent?
if($sent < $length) {
// If not, handle the leftover data.
$st = substr($st, $sent);
$length -= $sent;
} else {
break;
}
}
echo socket_read($socket, 8192);
Can you telenet to that port?
Related
I have a device which sends data in every 5 seconds using tcp and i am using php socket programming
and my code isn't working
this code print nothing and when i use this code without loop(while loop) then it works
i want to continue listen the data that my client sends in every 5 seconds
please help me out....thanks in advance.
<?php
error_reporting(E_ALL);
set_time_limit (0);
$host = '103.21.XX.XX';
$port = 60000;
$con = 1;
$word = "";
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die(socket_strerror(socket_last_error()));
$result = socket_bind($socket, $host, $port) or die(socket_strerror(socket_last_error($socket)));
$result = socket_listen($socket) or die(socket_strerror(socket_last_error($socket)));
while ($con==1) {
# code...
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
$input = socket_read($spawn, 2048) or die("Could not read input\n");
$input=(String)$input;
echo $input;
if ($input == 'exit')
{
$close = socket_close($sock);
$con = 0;
}
if($con == 1)
{
$word .= $input;
}
}
echo $word;
?>
I have following scenario:
PHP persistent socket server made it run through console in infinite loop
It is my server.php and running it once by using ' php server.php ' command from command line.
<?php
// set some variables
$host = "127.0.0.1";
$port = 3000;
// don't timeout!
set_time_limit(0);
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
while(true) {
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
// 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;
// reverse client input and send back
$output = strrev($input) . "\n";
echo socket_send($spawn, $output, strlen ($output), 0) or die("Could not write output\n");
// close sockets
}
socket_close($spawn);
socket_close($socket);
?>
Client.php here from this file I am sending message to socket server: At this point I am successful to send message to my socket server.
<?php
$host = "127.0.0.1";
$port = 3000;
$message = "Abhi ye sab kaise karen ahm asdasdasdasd";
//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");
// get server response
//Send the message to the server
if( ! socket_send ( $socket , $message , strlen($message) , 0))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not send data: [$errorcode] $errormsg \n");
} else {
echo "Message send successfully \n".$message;
}
//socket_close($socket);
?>
Receiver.php another file where I want to receive response my socket server is sending which is not working and Its keep on loading with out giving any out put.
<?php
$host = "127.0.0.1";
$port = 3000;
$message = "Abhi ye sab kaise karen ahm ";
//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");
// get server response
//Send the message to the server
while(true){
$from = '';
$port = 3000;
echo socket_recvfrom($socket, $buf, 12, 0, $from, $port);
echo "Received $buf from remote address $from and remote port $port" . PHP_EOL;
}
//socket_close($socket);
?>
My receiver end is not receiving any out put. I am trying to do this way:
Send message from client to socket server and as soon as socket server dispatch it my receiver end display it, I am successful only up to client where I client.php sending message to server.php but receiver.php is not receiving any thing.
Thanks
I am running a client-server code in php for chatting. I am getting this error:
socket_connect(): unable to connect [10061]: No connection could be made because the target machine actively refused it
I have gone through many questions and answers on this problem but still unable to solve this.
Here is my code :
for user-1:
$host = "127.0.0.1";
$port_1 = 5001;
$port_2 = 50002;
// create socket
$socket_2 = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$socket_1 = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_connect($socket_2, $host, $port_2) or die("Could not connect to server\n");
socket_bind($socket_1, $host, $port_1) or die("Could not bind to socket\n");
socket_listen($socket_1, 10) or die("Could not set up socket listener\n");
$new_socket= socket_accept($socket_1) or die("Could not accept incoming connection\n");
$result = socket_read ($socket_1, 1024);
if (isset($_POST['submit']))
{
$message = $_POST['client_message'];
socket_write($socket_2, $message, strlen($message)) or die("Could not send data to server\n");
}
// or die("Could not read server response\n");
if(!empty($result))
{
echo "Reply From Server :".$result;
}
socket_close($socket_1);
socket_close($socket_2);
socket_close($new_socket);
?
This is for user-2
$host = "127.0.0.1";
$port_1 = 5001;
$port_2 = 50002;
// create socket
$socket_2 = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$socket_1 = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_connect($socket_2, $host, $port_2) or die("Could not connect to server\n");
socket_bind($socket_1, $host, $port_1) or die("Could not bind to socket\n");
socket_listen($socket_1, 10) or die("Could not set up socket listener\n");
$new_socket= socket_accept($socket_1) or die("Could not accept incoming connection\n");
$result = socket_read ($socket_1, 1024);
if (isset($_POST['submit']))
{
$message = $_POST['client_message'];
socket_write($socket_2, $message, strlen($message)) or die("Could not send data to server\n");
}
// or die("Could not read server response\n");
if(!empty($result))
{
echo "Reply From Server :".$result;
}
socket_close($socket_1);
socket_close($socket_2);
socket_close($new_socket);
?>
please help me figuring out the errors.
Thank you
I have two files:
client.php
<?php
$host = "127.0.0.1";
$port = 25003;
$message = "Hello Server";
echo "Message To server :".$message;
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
$result = socket_read ($socket, 1024) or die("Could not read server response\n");
echo "Reply From Server :".$result;
socket_close($socket);
?>
and server.php
<?php
$host = "127.0.0.1";
$port = 25003;
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
$input = socket_read($spawn, 1024) or die("Could not read input\n");
$input = trim($input);
echo "Client Message : ".$input;
$output = strrev($input) . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
socket_close($spawn);
socket_close($socket);
?>
I am running http://www.domain.com/php-socket/client.php from browser. But I don't know how to run server.php on server side using terminal. My php-socket root directory is /var/www/php-socket/
please open terminal and type bellow command
cd /var/www/php-socket
php server.php
I'm using the following code as the server for sockets while i'm trying to learn sockets, but the client code will work once, i have to run the server script every time before i run the clint code.
And so when do i ran this server.php to keep listening for client requests?
SERVER.PHP
$host = "127.0.0.1";
$port = 25003;
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
$input = socket_read($spawn, 1024) or die("Could not read input\n");
if ($input == "Hey"){
$input = "Hey you don't shout me. Talk properly...";
} else if ($input == "Vetra"){
$input = "Hello how are you there, whats your name?";
} else { $input = "Well, what can i say, you must be a human being.";
}
echo $input;
$output = $input . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
//socket_close($spawn);
//socket_close($socket);
CLIENT.PHP
$host = "127.0.0.1";
$port = 25003;
$message = $_POST['data'];
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
$result = socket_read ($socket, 1024) or die("Could not read server response\n");
echo $result;
//socket_close($socket);
You should waiting for new connections in infinite loop:
$host = "127.0.0.1";
$port = 25003;
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
while(true) {
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
$input = socket_read($spawn, 1024) or die("Could not read input\n");
if ($input == "Hey"){
$input = "Hey you don't shout me. Talk properly...";
} else if ($input == "Vetra"){
$input = "Hello how are you there, whats your name?";
} else {
$input = "Well, what can i say, you must be a human being.";
}
echo $input.PHP_EOL;
$output = $input . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
socket_close($spawn);
}