How can I understand the error when trying to open a socket?
I get error 0, however all SSL certificate works fine.
$fp = fsockopen("ssl://cloud-messaging.bitrix24.com", 443, $errno, $errstr);
if (!$fp) {
echo "ERROR: $errno - $errstr<br />\n: " . $errno;
print_r($errstr);
} else {
fclose($fp);
}
Result:
ERROR: 0 -
: 0
But it works when I try to open the socket with myself.
Maybe there is some script that will allow you to get at least some error?
Related
I am trying to use a PHP to read data from the local server localhost, but it seems no data is returned from it. Here is my full script.
<?php
$address = "localhost";
echo "Attempting to open the socket at ".$address."...\n";
$fp = fsockopen($address, 49801, $errno, $errstr, 10);
echo "Error number is "; echo $errno; echo ".\n";
echo "Error string is ".$errstr.".\n";
echo "Attempt complete.\n";
if ($fp) {
print "Socket in now open.\n";
syslog(LOG_INFO, "socket.php: Reaching the specified address...");
print "Writing requests...\n"; //Hangs for about 1-2 minutes
fwrite($fp, "POST / HTTP/1.0\r\nUser-Agent: PHP XMLRPC 1.0\r\nHost: ".$address."Content-Type: text/xml\r\nContent-Length: ".strlen($payload)."\r\n\r\n");
$msg = "";
while($data = fread($fp, 32768)) {
$msg= $msg.$data;
}
if (strlen($msg) != 0) {
print "Final message: ***".$msg."***\n";
} else {
print "There is no data received from '".$address."'\n";
}
fclose($fp);
} else {
print "Error\n";
}
?>
Here is the output I am getting in the terminal:
Attempting to open the socket at localhost...
Error number is 0.
Error string is .
Attempt complete.
Socket in now open.
Writing requests...
There is no data received from 'localhost'
As mentioned in the script above, the second last line Writing requests... hangs for about 1 or 2 minutes, then an empty string is appended.
I think it is rather curious because this script works well on HTTP's port 80 or on SSH's port 22. I have restricted access to localhost:49801's configuration, and thus am not able to make any changes to the server's config.
I was however wondering if something was wrong with the server's config so that I don't have to tear out my hair for another day.
By the way, I am running PHP 5.4 on CentOS 7.
Edit
The '111' in "Content-Length: 111" is an arbitrary number that depends on the payload's string length.
Thanks your your help!
You got no reply from your server because he is waiting 111 bytes of data from you.
if you send the right amount of data to your server he will respond accordingly.
Below is working example where I change the Content-Length to 9
and then I send the 9 bites of data using fwrite:
<?php
$address = "localhost";
$payload = "Some data";
echo "Attempting to open the socket at ".$address."...\n";
$fp = fsockopen($address, 49801, $errno, $errstr, 10);
echo "Error number is "; echo $errno; echo ".\n";
echo "Error string is ".$errstr.".\n";
echo "Attempt complete.\n";
if ($fp) {
print "Socket in now open.\n";
syslog(LOG_INFO, "socket.php: Reaching the specified address...");
print "Writing requests...\n";
fwrite($fp, "POST / HTTP/1.0\r\nUser-Agent: PHP XMLRPC 1.0\r\nHost: ".$address.
"\r\nContent-Type: text/xml\r\nContent-Length: ". strlen($payload) ."\r\n\r\n");
//We send the data to the server
fwrite($fp, $payload);
$msg = "";
while($data = fread($fp, 32768)) {
$msg= $msg.$data;
}
if (strlen($msg) != 0) {
print "Final message: ***".$msg."***\n";
} else {
print "There is no data received from '".$address."'\n";
}
fclose($fp);
} else {
print "Error\n";
}
?>
I am working on to send request to VSP200 device, my device is connected to com port8 of windows machine. I am using fopen() of PHP to open the com port, but I am getting an error
Warning: fopen(COM8:) [function.fopen]: failed to open stream
can you please tell me, what is wrong in my code,
$fp = fopen ("COM8:", "w+");
if (!$fp) {
echo 'not open';
}
else{
echo 'port is open for write<br/>';
$string .= '<STX>C30C10178C10100C103110606C103081000C10100C10101C100<ETX>';
fputs ($fp, $string );
echo $string;
fclose ($fp);
}
$fp = fopen ("COM8:", "r+");
if (!$fp) {
echo 'not open for read';
}
else{
echo '<br/> port is open for read<br/>';
$buffer = fread($fp, 128 );
echo $buffer;
fclose ($fp);
}
You should not include the trailing colon in the port name:
$fp = fopen ("COM8", "w+");
I'm trying to make a simple UDP client server example in PHP but I face an error.
This is the client :
$fp = stream_socket_client("udp://192.168.0.12:12478", $errno, $errstr);
if ($fp)
{
fwrite($fp, "TEST 1 TEST 2 TEST 3");
$buf = fgets($fp);
var_dump($buf);
fclose($fp);
}
This is the server :
$socket = stream_socket_server("udp://192.168.0.12:12478", $errno, $errstr, STREAM_SERVER_BIND);
if ($socket)
{
while ($conn = stream_socket_accept($socket)) {
fwrite($conn, date("D M j H:i:s Y\r\n"));
fclose($conn);
}
fclose($socket);
}
All executions end with :
Warning: stream_socket_accept(): accept failed: Operation not supported
Basically, this is the example given in all PHP documentations but I can't figure what is wrong in it. Any help is greatly appreciated.
Thanks.
Here is the warning on the very same page
Warning
This function should not be used with UDP server sockets. Instead,
use stream_socket_recvfrom() and
stream_socket_sendto().
according to the documentation: "you cannot make a silk piurse from a sow's ear"
stream_socket_connect is intended for STREAMS, not datagram packets. recvfrom would be more likely to work in this scenario.
I am getting problem while using fwrite in php. the following code works in my local computer but gives error in server.
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if(!$fp) {
echo 'Error: '.$errno.', '.$errstr;
} else {
fwrite($fp, 'kool');
}
There is no error with fsockopen. it passes and gives no error. fwrite is not being able to write. it fails and returns no error only false
This is a permissions issue with the Apache/Nobody user accessing a remote file that it doesn't have permission to modify/read/write/execute.
You should also print the error message(s) for debugging
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if(!$fp) {
echo "Error No: ".$errno."<br />\n";
echo "Error Str: ".$errstr."<br />\n";
} else {
fwrite($fp, 'kool');
}
If you're on a shared host, most likely your server does not allow outbound connections on port 80. Usually only inbound connections are allowed.
I have a very simple server php code like this
function listenForClients()
{
$this->serviceConnection = socket_create(AF_UNIX, SOCK_STREAM, 0);
socket_bind($this->serviceConnection, "\tmp\mysock", 0);
socket_listen($this->serviceConnection, 10000000);
while($clientSocket = socket_accept($this->serviceConnection))
{
$clientMessage = socket_read($clientSocket, 1024);
socket_close($clientSocket);
}
}
Then I have a very simple client that does this
for ( $counter = 0; $counter <= 1000; $counter ++) {
$fp = fsockopen("unix///tmp/mysock", 0, $errno, $errstr);
if (!$fp){
echo "Error: Could not open socket connection at " . $counter . "\n";
exit;
}else{
fputs ($fp, "hello", strlen("hello"));
fclose($fp);
}
}
For some reason, after a random number of connections (around 300-500) fsockopen will return with a warning Resource temporarily unavailable. In the beginning I was getting the warning at around 20-30 connections. But once I increased the backlog parameter in socket_listen it got a bit better to around 300-500. How do I overcome this?
What is the way to build a php server socket to accept a lot of incoming connections per second (sustained).
Thanks!
The full error:
PHP Warning: fsockopen(): unable to
connect to unix:///tmp/mysock:0
(Resource temporarily unavailable) in
test.php on line 22
Check your ulimit. Are you overflowing your file descriptor table?
EDIT: the backlog value you have in accept() is bogus. Most OS-es have the max incoming connection queue size on the scale of dozens, not thousands.
I've just been looking at this issue (got here through Google) and I've found that a solution to get rid of the error:
PHP Warning: fsockopen(): unable to connect to unix:///tmp/mysock:0 (Resource temporarily unavailable) in test.php on line 22
..is to not use fsockopen() in the writer thread; try something like this instead:
if (! ($cSock = socket_create(AF_UNIX, SOCK_STREAM, 0))) {
exit("Failed to create socket");
continue;
} else if (! socket_connect($cSock, IPC_SOCK)) {
exit("Failed to connect socket");
} else {
$bw = socket_write($cSock, $msg);
if ($bw === false) {
exit("Socket write failed, %s", array(socket_strerror(socket_last_error())));
} else {
exit("Wrote $bw bytes to socket");
}
}
socket_shutdown($cSock);
socket_close($cSock);
Better late than never ? ;-)