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?
Related
The code I tried:
$address = '192.168.0.201';
$port = 4073;
$timeout = 30;
if (($socket = #socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
exit("socket_create() failed: reason: " . socket_strerror(socket_last_error()));
}
$result = socket_connect($socket, $address, $port);
The socket is created successfully but socket_connect results with:
Warning: socket_connect(): unable to connect [10061]: No connection
could be made because the target machine actively refused it in...
Device is on the same network and subnet, device and card reading works fine with the ZKAccess software..
I also tried existing library - https://github.com/mlrahman/ZKTeco_Attendance_Access_Using_PHP , but it produces identical error. Am I missing some steps or device configuration here?
ZKTeco devices have hardcoded TCP port 4370 but there is 4073 in your code. Try to change that, it might connect.
Despite that, this library doesn't seem to work with ZKTeco C3 devices, at least I was unable to make it work. There are several ZKTeco libraries for pythot that don't work, either. I've tried do trace the communication in Wireshark. After the communication is set, all libraries send the same 16 bit packet (5050827d08000000e80317fc00000000) which is ignored by the C3.
Unfortunately, it seems that the only way to communicate with C3 devices is to use the original ZKTeco SDK and to write a PHP wrapper around it.
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?
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 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