Php stream_socket_client - Only one packet is sent - php

I have got a client server application using TLS and C# and I have a requirement to make an extension with PHP.
I am able to establish a connection and receive data once, after that, the server doesn't receive anything else and the php script reaches the end.
My PHP code is as follow:
$fp = stream_socket_client("tls://localhost:1776", $errno, $errstr, 30);
if (!$fp) {
die("Unable to connect: $errstr ($errno)");
}
fwrite($fp, "Test1");
fwrite($fp, "Test2");
fclose($fp);
echo "DONE!";
I have attempted several approaches but the second fwrite never reaches the server no matter what. I am 100% sure the server is not the problem as the code is exactly the same for the C# client and that one works flawless.
Could anyone give me any tips on how to overcome this?
Thanks for reading and best regards.

Related

I'm getting "Unable to find the socket transport 'http'" error suddenly. It was working fine until we switched networks

After a network switch, parts of my program that send data to other servers are no longer working.
I tried the following code:
<?php
fsockopen("www.php.net", 80, &$errno, &$errstr, 30);
if(!$fp) {
echo "Error: $errstr ($errno)<br>\n";
} else {
fputs($fp,"GET / HTTP/1.0\n\n");
while(!feof($fp)) {
echo fgets($fp,128);
}
fclose($fp);
}
?>
After running that code, I am presented with the following error:
Unable to find the socket transport "http" - did you forget to enable it when you configured PHP? (19)
What do I need to check to ensure this works? It's baffling because it was working fine just before switching networks. I'm also getting the "php_network_getaddresses: getaddrinfo" error when I try get_file_contents.
Did you try opening the socket without the protocol part, e.g. just
fsockopen("www.php.net", 80, &$errno, &$errstr, 30);
I found the answer by doing a google search for
Unable to find the socket transport "http"
The same answer is in all of the top 5 results, so it would have saved you 3 days to just spend 5 seconds copying and pasting the error in to google.

speeding up fsockopen

I have a socket which connects to an irc server and sends some commands during the connection.
Seems like this:
$socket = #fsockopen(IRCIP, IRCPORT, $errno, $errstr, IMEOUT);
stream_set_timeout($socket, TIMEOUT);
fputs($socket, "SVSLIST\n");
But it takes a bit long (mostly, 0.5 second but sometimes its up to 1.5 second) Not to mention that both php script and the irc server works on the same machine.
So i would like to ask how can i speed up this process? I was using readfile with different kind of mechanism (building a httpd server as module in that irc server and redirect the readfile to do queries) to do that, it was pretty fast.. Is there a way to boost the speed?
Thanks.
The last parameter of fsockopen() is the timeout, set this to a low value to make the script complete faster, like this:
$socket = #fsockopen(IRCIP, IRCPORT, $errno, $errstr, 0.1);
Also... you have to know that this code:
$socket = fsockopen('www.mysite.com', 80);
Is way slower than:
$socket = fsockopen(gethostbyname('www.mysite.com'=, 80);
One last thing... if your script has to be run locally on the same machine of the IRC server, just use 127.0.0.1 to connect instead of the machine public IP address.

PHP HTTP Request via fsockopen and fwrite not working as expected on linux

We are currently using sockets to open and write to a http connection, requests that we don't necessarily care about the response! Like tracking pings etc
This worked on our old servers and on our windows developments environments but not on our new ubuntu servers.
The code we use is as follows
$aUrlParts = parse_url($sUrl);
$fp = fsockopen(
$aUrlParts['host'],
isset($aUrlParts['port']) ? $aUrlParts['port'] : 80,
$errno, $errstr, 30
);
$sHeader = "GET {$aUrlParts['path']}?{$aUrlParts["query"]} HTTP/1.1\r\n";
$sHeader.= "Host: {$aUrlParts['host']}\r\n";
$sHeader.= "Connection: Close\r\n\r\n";
fwrite($fp, $sHeader);
fclose($fp);
if I do a read after the fwrite i can get it all to work from the servers but this defeats the point of doing the request this way compared to just curling the URL
I have tried flush the socket and setting it to non blocking but non of that works! Just doing a read after is the only thing that works!
Any help is appreciated
Edit: I will mention these new servers are AWS based and I have a feeling the socket implementation on them may be different
I am not sure that this is worthy as an answer but I had the exact same problem and that's how I came here. My only difference was that I was trying to execute a request against the server itself.
My solution was in the access management of the server. I had an htaccess file that was blocking anyone from viewing except for my own network and the hitch was that it was also blocking my server from requesting itself.
So maybe it has something to do with the servers access management. Let me know if this helps.
We were using fsockopen and fwrite combo, then it up and stopped working one day. Or it was kind of intermittent. After a little research and testing, and if you have fopen wrappers enabled, I ended up using file_get_contents and stream_context_create functions with a timeout that is set to 100th of second. The timeout parameter can receive floating values (https://www.php.net/manual/en/context.http.php). I wrapped it in a try...catch block so it would fail silently. It works beautifully for our purposes. You can do logging stuff in the catch if needed.
$context = stream_context_create([
"http" => [
"method"=>"GET",
"timeout" => .01
]
]
);
try {
file_get_contents($url, 0, $context);
}catch( Exception $e ){
// Fail silently
}

fsockopen is returning an error even if the server im trying to reach is online?

im using fsockopen below:
$socket = fsockopen("uberminecraft.com", 25565, $errno, $errstr, 1);
return ($errno === 0);
Now this should return either false or true if the server is up or not. I know this server is definty up yet i still keep getting an error
Warning: fsockopen() [function.fsockopen]: unable to connect to uberminecraft.com:25565 (Connection timed out)
You have set a timeout of 1 second, is this intentional? Do you know whether the server is able to provide a response fast enough? Have you tried setting a higher timeout?
Also you might want to look at php.net for how to check whether the connection was made.
You must change fsockopen function as follow
$socket = fsockopen("uberminecraft.com", 25565, $errno, $errstr, 30);
Still you are getting same error. please tell to your host provider to open 25565 port.
Thanks

PHP fwrite() to socket - operation not permitted?

I'm making a simple utility in PHP to control my Minecraft server via UDP:
$fp = fsockopen('udp://host', 'port', $errno, $errstr);
if (!$fp)
error("Unable to connect!");
else {
fwrite($fp, $data['command'].':user:pass');
stream_set_timeout($fp, 5);
error(fread($fp, 128));
fclose($fp);
}
For some reason fwrite is throwing this error:
Notice: fwrite() [function.fwrite]: send of 20 bytes failed with errno=1 Operation not permitted in /homepages/44/d217581656/htdocs/xenforo/util/remoterestart/interface.php on line 22
Anyone know why?
I'm going to go out on a limb and say you're probably not permitted to use sockets on your server. This is a fairly common thing to disable on most hosts.
Run a phpinfo() and see if there is any socket functions which are disabled, or contact your host and just ask.

Categories