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.
Related
This must be simple and I suspect I have disappeared down the wrong rabbit hole.
I have the apache http server running on a pi serving pages (LAMP server) exposed through our domestic router. I can get php to serve up the material I want, and thought it would be easy to get php (on the server) to talk via sockets to another machine on the local network (ie 192.168.1.73 for example)
I can get data from a web page via the server to the local machine, but cannot get the server to receive messages from the local machine. In php the call to
'''
socket_bind(...)
'''
gives the error
"unable to bind address [99]: Cannot assign requested address"
Don't know where the 99 is coming from; installed and ran ufw to open the port I'm using (would not have thought that was necessary or desirable but tried it anyway)
Any help greatly appreciated.
P
It is a PHP problem.
Implemented "quote of the day" in java, running the client on the same machine as my problematic php, and the quote server on another machine on the local network. That works fine. replace the java client with this php client:
<?php
$server_ip = '192.168.1.78';
$port = 41235;
$message = 'hello world.';
$buf = [];
$skt = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP) or die(..);
socket_sendto($skt,$message,strlen($message),0,$server_ip,$port);
// message is successfully sent and received at other end...
socket_bind($skt,$server_ip,$port)
or die("Could not bind socket\n");
// the bind fails
$bc = socket_recvfrom($skt,$buf,256,0,$server_ip,$port);
echo "Got $bc bytes back";
?>
Fails as above with error [99]
The use-case is to run a server on a machine inside a local network -lets call it insideServer, and have requests from a webpage hosted on a machine, outsideServer, responded to by code running on insideServer. Why? InsideServer's server is written in java because I like it. The right way to do this was, in the old days, to have java running on the user's machine as an applet; The modern way would be to have outsideServer run Apache Tomcat. I don't however have the luxury of doing that so how does one forward requests?
The solution is to have a PHP script in the relevant directory of outsideServer that connects via sockets to insideServer where insideServer's code can be in whatever you want. The PHP has however been problematic. This code does work however:
$insideServerIP = '192.168.1.78';
$insideServerPort = 41234;
$message = 'pretend message from web page form';
$skt = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP) or die("create failed: " . socket_last_error());
socket_connect($skt, $insideServerIP, $insideServerPort) or die("connect failed: " . socket_last_error());
socket_write($skt, $message, strlen($message));
$in = socket_read($skt, 256, PHP_BINARY_READ);
echo "$in\n";
socket_close($skt);
My research over the last few days (Arr!!!) suggests nobody knows how socket_bind() is meant to work, and it turns out you don't need it!
Please do post comments (or a better answer) if there are mistakes, but this solution works I believe.
P
I have some embedded devices which connect to their sever (supplied software for windows) running on port 6000.
I'm confused as to how this works as my understanding is that when a tcp connection is made on a port, that's it, the port is occupied an no other connections can be made.
But I guess this logic is wrong as multiple devices can connect to their server software just as a web server can accept multiple connections on port 80.
When I run netcat on an ubuntu server with the command nc -l -k 6000 it seems to do what I want, I can see the messages coming in one after another from multiple devices.
From the netcat manual:
-k Forces nc to stay listening for another connection after its cur-
rent connection is completed. It is an error to use this option
without the -l option.
I tried to achieve a similar scenario with php but I failed. Here's what I have so far:
<?php
$address = "0.0.0.0";
$port = 6000;
$mysock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($mysock, $address, $port) or die('Could not bind to address');
socket_listen($mysock, 5);
$client = socket_accept($mysock);
while (true) {
$input = socket_read($client, 1);
echo $input;
}
?>
I can see the first message from the first device that connects but after that nothing. Also this seems to be the wrong approach as the while loop causes high cpu usage.
Note I'm running the php script at the command line on an ubuntu server.
I realize that this code will only accept one client, but how can I change it so that it will behave like nc and accept multiple connections. Sorry if the question seems vague, I really don't know where to begin. Any help is appreciated
I am tried to implement a LDAP authentication in my web application developed in ZF2. LDAP authentication is working fine in Windows 7.
But, after moving the application to LINUX machine, LDAP authentication is not working. I am always getting the error as : Warning: ldap_bind(): Unable to bind to server: Can't contact LDAP server in LdapConnect.php on line 20
I have used the scripts as:
$ldaphost = "ldap://xxxx.net";
$ldapport = 389;
$ds = ldap_connect($ldaphost, $ldapport) or die("Could not connect to $ldaphost");
if ($ds)
{
$username = "username#xxxx.net";
$upasswd = "password";
$ldapbind = ldap_bind($ds, $username, $upasswd);
if ($ldapbind)
{
print "Congratulations! you are authenticated successfully.";
}else{
print "Better luck next time!";
}
}
Should I install any software package or should I do any config settings?
Note: If I give the IP adress then it is working fine, but if I give the domain name, then it is not working.
The library may be different between the 2, or a different version. You'd be amazed how many variations of the ldap client there are. In your position I would (if available) use ldap client to make the same kind of connection a few different ways.
e.g. the "-x" on the standard ldapsearch:
-x Use simple authentication instead of SASL.
So you could express the connection like this:
ldapsearch -h xxxx.net -p 389 (etc)
ldapsearch -x -h ldap://xxxx.net:389 (this should actually be -H..)
and so on.
It is also possible for things outside of your code to be an issue. Prod servers often have firewalls and proxies (e.g. F5) that are transparent to the server/client.
Make sure your final code has exception handling for binding and searching. I'm not too familiar with the php implementation, and the doco is a tad thin. Normally you'd use a synchronous bind.
Can you verify that the code above is exactly as you had it on Windows? The reason I ask is that looking here: http://php.net/manual/en/function.ldap-connect.php it seems that you may be mixing 2 types of bind. I definitely wouldn't have done it like that in standard python.
So if using a URI normally you'd do it like this:
ldap_connect("ldap://blah:389")
and if you're connecting via host/port combo:
ldap_connect("blah","389")
With minimal exception info my best guess is that its actually trying to bind to a hostname "ldap://xxxx.net" on port "389".
I'm trying to make a simple listener on port 8195. When I try the following code block in PHP CLI conditions, it only shows 'Test' once, then hangs. If I delete the file 'votifier.run', the file designed to be the on/off switch, it still continues to hang. It never shows 'Client connected'.
Furthermore, if I try to connect to the host via Telnet on port 8195 while the script is running, I simply get a connection failed message. It's like it's looking for one connection and just not giving up.
// Set the IP and port to listen to
$address = 'localhost';
$port = 8195;
// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
// Bind the socket to an address/port
socket_bind($sock, $address, $port);
// Start listening for connections
socket_listen($sock);
// Loop continuously
while ( file_exists('votifier.run') ) {
echo 'Test';
$client = socket_accept($sock);
if( $client ) {
echo 'Client connected';
// Don't hang on slow connections
socket_set_timeout($client, 5);
// Send them our version
socket_write("VOTIFIER MCWEBLINK\n");
// Read the 256 byte block
$block = socket_read($client, 256);
...
The answer:
socket_accept() will usually hang until a connection is made. If a connection attempt was made, the script would continue, but because the socket was being created on localhost, it would only accept connections to it from localhost.
The fix is to use your external IP rather than 'localhost' or '127.0.0.1'. Then you can Telnet to it.
I'm just guessing here, but could it be that the address you are trying to bind to should not be a hostname?
If the socket is of the AF_INET family, the address is an IP in dotted-quad notation (e.g. 127.0.0.1).
EDIT
Ok, I've taken your script and tried to reproduce your error but couldn't. There are a couple of flaws in it but none that would cause a telnet client's connection attempt to fail.
Since none of the aforementioned applies, let's go thru the checklist one by one:
sockets module loaded/compiled
localhost does resolve to 127.0.0.1
the port isn't taken by any other application running
there's no rule of any sort of firewall that would prevent communication between the telnet client and your server
the machine which you connect from is allowed to connect to the server host (try the same host if it isn't)
the file that's being checked in the while-loop does exist
you are sure that there isn't another fatal error within your script that would prevent the snippet you posted from running
These are all the possible error sources I can think of, atm. Try fixing up the minor flaws first, then go thru the checklist.
if( $client ) {
echo 'Client connected';
// Don't hang on slow connections
socket_set_option(
$client,
SOL_SOCKET,
SO_RCVTIMEO | SO_SNDTIMEO,
array('sec' => 5, 'usec' => 0)
);
// Send them our version
socket_write($client, "VOTIFIER MCWEBLINK\n");
^^^^^^^
// Read the 256 byte block
$block = socket_read($client, 256);
You should be using threads. If the client never sends anything your code will block in the read() method. Each accepted socket should be completely handled in a new thread.
You may want to check this:
PHP Votifier example for Minecraft Topsites
It explains how the code works, it's the basic function that makes the encryption, fills up the 256 blank spaces and sends the packet too. You can work a little with it as you may want to improve it.
You can see a live demo of the running php for the plugin here: http://topg.org/test_votifier
I am trying to convert some code from perl to php.
Perl code looks like below:
my $handle = Connect($port, $host);
and I am trying to use socket to do the same thing in php.
I have tried socket_create and socket_connect,
socket_create and socket_bind, and fsocketopen.
As a result, I'm stuck with error messages saying "Connection refused" or "permission denied":
socket_connect() [function.socket-connect]: unable to connect [111]: Connection refused in
I am not sure if this is the problem I need to solve, or the problem of permission because the code in perl works fine (I did not write that code).
my php code looks like below:
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if(!$socket){
die('Error: socket_create()');
}
if(!socket_connect($socket,$host,$port)) {
die('Error: socket_connect()');
}
I'm not the one who manages the server, so I will need to ask someone else for the access if it is a permission issue. What should I ask for specifically?
Or should I use some other function to connect to the server? I am new to perl, so I am not sure if socket_connect is the equivalent function to use or not.
Thanks.
If your perl code is able to establish the connection, no additional permissions should be needed to do the same in php. Connection refused means the remote host doesn't let you in (you probably connect to wrong address/port). Permission denied is more surprising, a lot of people have this kind of problem while running httpd scripts with SELinux enabled. If you're one of them, refer to the SELinux manpage:
SELinux policy can be setup such that httpd scripts are not allowed to connect out to the network. This would prevent a hacker from breaking into you httpd server and attacking other machines. If you need scripts to be able to connect you can set the httpd_can_network_connect boolean on:
setsebool -P httpd_can_network_connect 1
I have a few concers to your examples though. Connect from your Perl snippet doesn't seem to be the standard socket connect; I don't know which module it belongs to, but are you sure there is no magic behind the call? As socket_connect takes address in dotted-quad notation (for IPv4), make sure you're not passing a hostname (you would need to make a DNS lookup first). At the very end check if it's really a TCP socket you need, not UDP.