I'm trying to understand how we can use Php socket to let our client send messages to our Node.js server using TLS...
My problem is that i don't know how that is done in Php. Can someone please show me how that can be done in php and explain the steps please???
I have tried to search on many website to gain understanding about it. To connect to TCP server in Php we can do such:
// IPv4
$sock = #socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
// IPv6
$sock = #socket_create(AF_INET6, SOCK_STREAM, SOL_TCP);
$sock->connect($address);
// now we can send to the server using socket_write
How can the same be done with TLS instead of TCP in PHP???
I'm not looking for help with the Node.js server, just php..
Related
I am using a PHP Ratchet Socket server and I want to send data to this socket server via a php client. My socket server is working well with HTML 5 web sockets but php client isn't working. Here is my code
$host = "localhost";
$port = 9000;
$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);
when I run this code nothing happens... Any help?
You probably aren't looking for a PHP client for websockets but a pushserver.
http://socketo.me/docs/push
On the site of socketo.me everything is explained on how to set this up.
To give you a short summary:
A pushserver is the layer between your application logic and the websocket itself.
This would provide you to send requests to the pushserver which are then sent to the clients of the websocket for which the message was meant.
If you need any further explaination please let me know.
This comes late but can be helpful for someone looking to clarify this concepts.
socket_create() and socket_connect() are PHP core functions written to work with TCP and UDP protocols, but not for webSocket connections (ws://uri:port).
For the project of this question, the best is to use a ready-made websocket client package as #mitchken mentioned (ratchet/pawl, amphp/websocket-client, etc).
I have personally used amphp and it works really well, especially if you need to update the UI of an specific user/group in response to an event/notification that occurred in your backend, by sending the message with the PHP websocket client and redirecting the message to the target users in the onMessage() event of the websocket server adapter class.
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)
I'm not 100% sure this is worded right, but I have a Ratchet WebSocket server working correctly as a chat service. However, I want to, when a user posts a new thread on the forums, have the server automatically post a message into the chat to notify them all of this new post.
I want to do this via a quick TCP connection upon the creation of this thread. I'm still somewhat new to sockets and this area of server coding. Is there an easy way that PHP can ignore HTTP overhead in connecting to this same-server socket and simply sending a message?
Here's the code I've tried to use as a test, but ratchet does not even say it received a connection or message (ADDR and port are correct, socket_connect returns TRUE):
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
$string = "Hello, a new post has been BLAH";
$Socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$Status = socket_connect($Socket, "***", "***");
$Bytes = socket_write($Socket, $string, strlen($string));
socket_strerror(socket_last_error());
socket_close($Socket);
The solution suggested on the Ratchet site is to use ZeroMQ to have your synchronous php (web server that is doing the database work for the new post) push the message to the Ratchet server.
They have some pretty good docs at http://socketo.me/docs/push
Hey guys, I am trying to do some socket programming in PHP.
So I am running a socket "server":
$address = '127.0.0.1';
$port = '9999';
$masterSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($masterSocket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($masterSocket, $address, $port);
socket_listen($masterSocket, 5);
$clientSocket = socket_accept($masterSocket);
So I open up SSH and run this script. It is running, no errors.
Then I have another PHP script which attempts to connect to this:
$fp = fsockopen("me.com", 9999, $errno, $errstr, 30);
fclose($fp);
but it's giving me:
Warning: fsockopen(): unable to connect to me.com:9999 (Connection refused)
How do I begin to fix this?
You haven't finished the listening socket sequence, you need to call socket_accept to accept new connections. There is an example in the comments in the PHP documentation.
$clients = array();
$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
socket_bind($socket,'127.0.0.1',$port);
socket_listen($socket);
socket_set_nonblock($socket);
while(true)
{
if(($newc = socket_accept($socket)) !== false)
{
echo "Client $newc has connected\n";
$clients[] = $newc;
}
}
http://php.net/manual/en/function.socket-accept.php
1) Check if the port is firewalled off. You could use telnet to check this.
2) See if it works when the client and server are on the same machine (I'm guessing from your mention of SSH that the server is remote).
3) If it works locally and you can hit the remote port using other tools then it's going to be tricky. I'd suggest you wail and gnash your teeth for a bit; I'm out of ideas.
EDIT: Heh. Or you could just read Steve-o's answer. Teeth-gnashing is still an option.
I know you said that "me.com" is an example but, just to be sure, socket_bind is expecting an IP address.
From http://php.net/manual/en/function.socket-bind.php :
address
If the socket is of the
AF_INET family, the address is an IP
in dotted-quad notation (e.g.
127.0.0.1).
If the socket is of the AF_UNIX
family, the address is the path of a
Unix-domain socket (e.g.
/tmp/my.sock).
I know the question is very old, but if someone still has this problem, make sure you connect to the SAME address you are listening on,
For example, If you're listening on 127.0.0.1 and your Machine address is me.com, you won't be able to connect to me.com with it, for that you'll have to listen on me.com.
Listening on: localhost:8088
Can only connect via: localhost:8088 // not via me.com:8088
Listening on: me.com:8088
Can only connect via: me.com:8088 // not via localhost:8088
I tryed to create a socket in php and reuse it from other process.
I know this can be done with a daemon script but I want to do this without.
I created a socket and binded it to a specific port.
$sock = socket_create (AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option ($sock, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind ($sock, 'xx.xx.xx.xx', 10000);
socket_connect ($sock, $host, $port);
And from another php file I did the same thing. But the packets that I send from the 2 file are not "validated" by host. I sniffed all ports and I see that it uses same local and destination port. I don't understand where is the problem.
Can you help me with this?
It's ok in any other programming language, or any other solution for this.
Andrew
Sockets are not symmetrical. The server side listens on a specific port for a client to conect - the client does not specify the local port - only the remote port and address. Its nothing to do with the language you implement it in.
There's a very good socket server implementation available at http://www.phpclasses.org/browse/package/5758.html with examples.
C.
You can't really use persistent sockets in php. When you execute a php file, a new process is created which cannot access the variables - or sockets - of a different php process so it won't know if there already exists a socket and just creates it.