How to write socket codes via php - 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)

Related

PHP - Allow client to send a message to a TLS server

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..

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.

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: socket_connect is working, but not socket_bind

I am trying to connect via TCP Socket to a third party product. I am able to do so (and send a message successfully) with the following code:
if (isset($port) and ($socket=socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) and (socket_connect($socket, $address , $port)))
{
$text="Connection successful on IP $address, port $port";
$data = "<msg id=1>text</msg>" . $newline;
socket_send($socket, $data, strlen($data), MSG_DONTROUTE);
$text .= socket_strerror(socket_last_error());
socket_close($socket);
}
The down side is that I also need to listen on this socket. As I understand it, I first need to bind, rather than using connect. When I run the code below, I get this error when calling socket_bind
Only one usage of each socket address (protocol/network address/port) is normally permitted.
code (snippet from a TCP Class, I have verified that the IP address, port, etc are the same in both snippets):
$this->Socket = socket_create(AF_INET, SOCK_STREAM, 0);
$result = socket_bind($this->Socket, $this->Host, $this->Port);
$result = socket_listen($this->Socket, 3) or die("Could not set up socket listener\n");
$spawn = socket_accept($this->Socket) or die("Could not accept incoming connection\n");
How is it that I can connect using socket_connect but not socket_bind? Is it correct that I need to use socket_bind if I want to listen on this socket?
I am running PHP on a Windows XP Apache installation (Zend). I should also mention that the only other application running when I try to execute this code is the third party program to which I am trying to connect.
Thank you for any insight.

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