Create PHP WebSocket with SSL - php

I have a websocket chat on my website and I recently added SSL certificate to my website. When I open my website with http, chat works. When I open it with https it's not working. I am using javascript as client for my chat with ws:// protocol. I changed that protocol to wss:// because of SSL encryption on my page, but it won't work that way. Here's part of my PHP server code that creates the socket:
$host = 'localhost';
$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);

how do you add a certificate to a php socket?
using a different function call.
http://php.net/manual/en/function.stream-socket-client.php
and setting the correct socket options
http://php.net/manual/en/context.ssl.php

Related

PHP code (running in Compute Engine) for creating a listening socket on a specific port

I have written some code in PHP to create a socket and listen for incoming connections. I am trying to do this using a specific port I'm interested in.
Everything is going well, except it opens the listening TCP socket on some random port, instead of the one chosen by me.
I'm running this code on Google Compute Engine (GCE).
Here is the exact php code that I tried:
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
$address = '35.190.179.17';
//bind socket to specified host
socket_bind($socket, $address,54468);
//listen to port
socket_listen($socket);
I got the issue resolved by using the internal IP instead of external IP address and opening the port for external connections.
Compute engine was not allowing socket bind using external IP address.

Cannot connect to IP using sockets

I am trying to connect to my public IP address but to no success.
The port is open as checked using - http://www.yougetsignal.com/tools/open-ports/
and a request from this site (http://www.rexswain.com/httpview.html) works.
My problem is with this PHP code below. This used to work fine when used on my local network. Now I am using it on a web server.
Could the problem be with the web server to allowing the request to be sent or am I doing something wrong?
$ip = "MYIP";
$port = "600";
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
//socket_bind($sock, $ip, $port) or die ("cannot bind to address.");
echo($ip);
$state = socket_connect($sock,$ip, $port);
$error = socket_last_error();
$errorMsg = socket_strerror($error);
echo($errorMsg);
EDIT : The error in my comment below was derived with the socket_bind commented (Code edited to output the error)
Error : Connection timed out
This is wrong:
socket_bind($sock, $ip, $port) or ...
$state = socket_connect($sock,$ip, $port);
Bind binds the local address of the socket, it doesn't make sense to bind and connect to the same port. In general you do not want to bind in client mode. Remove the line completely, then you may get real error.

How to write socket codes via php

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)

Reusing address and port in PHP websockets

I'm implementing websockets for a chat system. The server.php is being invoked via ssh with php /www/server.php, and the first time it executes fine; but if the process is stopped (ctrl+z), this error is displayed after trying to invoke php /www/server.php again:
Warning: socket_bind(): unable to bind address [48]: Address already in use in /www/server.php
These are the contents of the /www/server.php file:
<?
$host = '10.10.0.103';
$port = '1337';
//Create TCP/IP sream socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
//reuseable port
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
//bind socket to specified host
socket_bind($socket, 0, $port);
//listen to port
socket_listen($socket);
// ... etc
So, I have two questions:
Can I reuse the same address / port after stopping the php /www/server.php job with a FLAG? Isn't SO_REUSEADDR supposed to reuse the same address/port?
What are the best practices for this issue? Restarting the websockets server could be an everyday task, e.g.: after updating the contents of /www/server.php
Thanks for any tips!

PHP sockets problem

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

Categories