Php supports raw socket on tcp(6)?
$socket = socket_create(AF_INET, SOCK_RAW, getprotobyname("tcp"));
I tried this.But,I got a warning message.
Warning: socket_create(): Unable to create socket [10013]: An attempt was made to
access a socket in a way forbidden by its access permissions.
I tried with ICMP(1).It is working.
$socket = socket_create(AF_INET, SOCK_RAW, getprotobyname("icmp"));
or other way to send a tcp syn packet in php?
Related
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..
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 have a server in PHP which binds to a port and listens to sockets. My server is started in a PHP script with:
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($socket, 0, $port);
It then listens to the port:
socket_listen($socket);
When a HTTP message arrives from the client, the server reads the header:
$header = socket_read($socket_new,1024);
and then stores the connection in a Memcache storage. This works for most browsers including safari, firefox and Chrome's Canary. However, it doesn't work on chrome. The browser throws an error message:
WebSocket connection to 'ws://xyz.com:9001/chat_server.php'
failed: Error during WebSocket handshake: Incorrect
'Sec-WebSocket-Accept' header value
My version of Chrome is: Version 38.0.2125.111 m (64-bit)
We had the same issue and we could solve it by increasing the "maximum number of bytes" parameter in the socket_read() function. You can try
socket_tead($socket_new, 2048);
The reason is that websocket header in chrome sometimes is greater that 1024 bytes. So, when your server reads 1024 bytes, it does not get Sec-Websocket-Key parameter and it can not generate valid Sec-Websocket-Accept value.
You can also use fsockopen() and fread() instead of socket_read() function.
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!
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?