I am trying to send data from PHP Client to Python Server.
CLIENT
<?php
$host = "127.0.0.1";
$port = 2000;
$output="datatatatatatta" ;
$socket1 = socket_create(AF_INET, SOCK_STREAM,0) or die("Could not create socket\n");
socket_connect ($socket1 , $host,$port ) ;
socket_write($socket1, $output, strlen ($output)) or die("Could not write output\n");
socket_close($socket1) ;
?>
SERVER
import socket
import sys
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host= 'VAC01.VACLab.com'
port=int(2000)
s.bind((host,port))
s.listen(1)
conn,addr =s.accept()
print (conn,addr)
data=conn.recv(100000)
data=data.decode("utf-8")
s.close
FILE = open("c:/vinod/vin.txt","w")
FILE.write(str(data))
FILE.close()
On the client side I get the following error ,.
Warning: socket_connect() [function.socket-connect]: unable to connect [0]: No connection could be made because the target machine actively refused it. in C:\xampp\htdocs\xampp\socket.php on line 10
Warning: socket_write() [function.socket-write]: unable to write to socket [0]: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied. in C:\xampp\htdocs\xampp\socket.php on line 12
Could not write output
Is this possible to connect this way ?
You bind to a host that most likely does not resolve to 127.0.0.1 but connect to 127.0.0.1. That cannot work. You have two options:
Use 127.0.0.1 (or localhost) on both sides
Use the hostname VAC01.VACLab.com on both sides
Related
I am having a hard time getting data from the server. Basically server waits for a connection from anyone using correct username and password. So when I use that using local explorer it shows the data on the browser.
Now what I was trying to do is, get this data using socket and forward it to another server address. But I could not even connect to the server to get the data like I get on the browser. Here is what I have tried:
$host = "192.168.1.4/online?user=dneb&pass=mella88";
$port = 1850;
// don't timeout!
set_time_limit(0);
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) 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");
// 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 "server Message : ".$input;
When I run this code I get warning message:
Warning: socket_bind(): Host lookup failed [11004]: The requested name is valid, but no data of the requested type was found. in C:\xampp\htdocs\socket\client.php on line 10
Could not bind to socket
Or suggest other methods to use.
When you create a socket server, you don't specify the host as the full URL to your script; that's why the bind is failing.
Take a look at this sample: http://www.php.net/manual/en/sockets.examples.php
please try changing
$host = "192.168.1.4";
I wrote the follwing code in PHP
<?php
$mysocket = socket_create(AF_INET, SOCK_STREAM , 0);
socket_bind($mysocket, '127.0.0.1',1024);
socket_listen($mysocket) or die("unable to listen!");
socket_connect($mysocket , '127.0.0.1' , 1024);?>
and an error showed up says"
Warning: socket_connect(): unable to connect [102]: Operation not supported on socket in /Applications/XAMPP/xamppfiles/htdocs/SOCKTEST.php on line 5"
Where is the problem?
I don't know the goal of your code. But here is a great tutorial for socket programming in PHP.
https://www.christophh.net/2012/07/24/php-socket-programming/
I have tested your code. The error comes if your bind your socket to and address
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($sock, '127.0.0.1');
socket_connect($sock, '127.0.0.1', 1337);
socket_close($sock);
http://php.net/manual/de/function.socket-bind.php
Example from the PHP documentation. Perhaps its better if you use different instances to test your problem that you can connect through your network to another instance or computer. For this you can use vagrant for example.
Servers listen and accept, clients connect. The same socket endpoint cannot be both a server (listen) and a client (connect)
Using the example from http://www.codeproject.com/Tips/418814/Socket-Programming-in-PHP
SERVER.PHP:
// set some variables
$host = "127.0.0.1";
$port = 25003;
// 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");
// 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";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
// close sockets
socket_close($spawn);
socket_close($socket);
CLIENT.PHP:
$host = "127.0.0.1";
$port = 25003;
$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);
As you can see, the server is listening on 127.0.0.1:port and client is connecting to 127.0.0.1:port
However, I want my client and server to be on two different servers.
For example:
My server will listen be on IP 11.22.33.44:1234 and my client will be connecting from IP 12.23.45.66:1234
I am going to be using linux servers, but right now I am doing this on my windows machine.
So I want to listen on my external IP.
When I enter my external IP as host, I get errors thrown.
Warning: socket_bind(): unable to bind address [10049]: The requested address is not valid in its context. in C:\xampp\htdocs\server.php on line 12
Warning: socket_listen(): unable to listen on socket [10022]: An invalid argument was supplied. in C:\xampp\htdocs\server.php on line 14
Warning: socket_accept(): unable to accept incoming connection [10022]: An invalid argument was supplied. in C:\xampp\htdocs\server.php on line 17
Warning: socket_read() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\server.php on line 18
Warning: socket_write() expects paramet
er 1 to be resource, boolean given in C:\xampp\htdocs\server.php on line 20
How can I do this?
Thanks.
Since you're (speculation!) probably hosting this service behind a proxy, router, firewall, or other perimeter device, your box won't have direct access to bind to the external IP. Therefore, leave it bound to localhost and forward (open) the applicable ports from the perimeter device to your server.
If, for example, you're using AWS to host, you would set the firewall ACL to permit your socket ports to both servers and then the connections should flow through.
This is akin to binding your Apache web services to the localhost IP address of the server.
Alternatively, if you're not hosting this and communication is happening on a local network, then you should be able to at least bind to the assigned/internal IP address of each local network card.
open cmd and write that
php -f C:\xampp\htdocs\server.php
your code
A have problem retrieving remote socket IP and port number.
During following procedure:
$master_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
//socket_bind(), socket_listen(), socket_accept()
socket_getpeername($client_socket, $client_address, $client_port);
This works fine on localhost(WAMP), but in production it generates warning:
PHP Warning: socket_getpeername(): unable to retrieve peer name [107]: Transport endpoint is not connected in ...
This is strange, because $client_socket is a proper socket resource, other socket functions such as socket_read() perform as they should; socket_getsockname() also works, binding local IP and port number to assigned variables.
Searching the web gives nothing. Does anybody know what could be the reason of such warning?
First of all, thanks for taking the time to read this. I have a strange problem with PHP sockets. I working on a php socket daemon which works via localhost, but when I try to connect from outside the LAN or another PC, it doesn't work. I've simplified my daemon to a very basic socket connection to replicate the issue for you to see.
Basically, here's the senario. I start the socket daemon on my server on port 6667. I can connect to the daemon via telnet and from the browser on the local machine running the daemon, but I cannot from any other machine - the daemon doesn't even see a connection attempt being made.
To further complicate the issue (which is why I think it's a port forwarding issue), my ISP blocks port 80, so I've setup dyndns and my router to use port 8000. I've also setup my router to forward port 6667 to my server.
To access my daemon from a browser, I enter the following (seudo) url:
http://mydomain.com:8000/client.php
This works from the local machine and will connect, but from any other machine, the daemon doesn't even see a connection attempt being made. However, if I specify the port like this:
http://mydomain.com:6667
my daemon does see a connection being made, but of course then the browser doesn't have a client page loaded that the user can use to interact with the daemon.
My client uses a flash file to create the socket connection (jsocket), but I know it isn't the cross-domain policy file because the policy is correct, and when connecting via localhost, it serves the policy file correctly.
Here's the simplified daemon code:
<?
// set some variables
$host = '0.0.0.0';
$port = 6667;
// 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");
// 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 input back
$output = $input . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
// close sockets
socket_close($spawn);
socket_close($socket);
?>
Summary:
I CAN connect from localhost via telnet and browser... I CAN connect from other machines via telnet, but I CAN NOT connection from the browser from other machines using the ip or domain name when port 8000 is specified. The daemon doesn't see any connection attempt. If I specify port 6667, then the daemon see's a connection attempt, but that is useless to the user. :(
Any help in this matter would be greatly appreciated! Thanks!
You're binding the socket (using socket_bind) onto localhost. Supplying localhost there will have PHP bind the socket to the 127.0.0.1.
socket_bind is used to bind a socket to a specific interface. Per example:
socket_bind($socket, '127.0.0.1', 80);
This allows you to connect to 127.0.0.1:80, but not 192.168.1.100:80, even if they are the same machine. The socket is bound to the 127.0.0.1 interface only:
$ telnet localhost 80
Trying 127.0.0.1...
Connected to 127.0.0.1.
$ telnet 192.168.1.100 80
Trying 192.168.1.100...
telnet: Unable to connect to remote host: Connection refused
If you want to bind the socket on all available interfaces, use:
socket_bind($socket, '0.0.0.0', 80);
Then this works:
$ telnet localhost 80
Trying 127.0.0.1...
Connected to 127.0.0.1.
$ telnet 192.168.1.100 80
Trying 192.168.1.100...
Connected to 192.168.1.100.
I use this "port_forwarding.php" on server to open port 3000 and forward remote mysql client connection to unix socket file / port 3306 of mysql server:
<?
set_time_limit(0);
function shutdown()
{global $ipsock, $rmsock;
if ($ipsock) fclose($ipsock);
if ($rmsock) fclose($rmsock);
}
register_shutdown_function('shutdown');
$target_socket='unix:///tmp/mysql.sock';//or 'tcp://192.168.0.2:3306'
$ipsock=stream_socket_server('tcp://192.168.0.2:3000', $errno2, $errstr2);
stream_set_blocking($ipsock, 0);
while (true)
{usleep(5000);//0.005s, to reduce cpu consumption
$c_ipsock=stream_socket_accept($ipsock); //even add '-1', it won't wait
$rmsock=stream_socket_client($target_socket, $errno, $errstr);
#stream_set_blocking($rmsock, 1);
while (($c_ipsock && !feof($c_ipsock)) && ($rmsock && !feof($rmsock)))
{$swrite=$except=null;
$sread=array($c_ipsock, $rmsock);
stream_select($sread, $swrite, $except, 5);
//print_r($sread);echo " \n";
if ($sread[0]===$rmsock)
{if ($data=fread($rmsock, 65536))
{//echo 'rmsock:'.strlen($data).' '.$data." \n";
myfwrite($c_ipsock, $data);
}
}
else if ($sread[0]===$c_ipsock)
{if ($data=fread($c_ipsock, 65536))
{//echo 'ipsock:'.strlen($data).' '.$data." \n";
myfwrite($rmsock, $data);
}
}
//var_export(array(feof($c_ipsock), feof($rmsock)));echo " \n";
}
#fclose($c_ipsock);
#fclose($rmsock);
}
function myfwrite($fd,$buf) {
$i=0;
while ($buf != "") {
$i=fwrite ($fd,$buf,strlen($buf));
if ($i==false) {
if (!feof($fd)) continue;
break;
}
$buf=substr($buf,$i);
}
return $i;
}
?>