I get this error message, when try connect to tiny UDP server. The source code is minimized and looks like so:
//server.php
$server_host = '127.0.0.1';
$server_port = 21665;
$poll_interval = 0.5;
$socket = socket_create(AF_INET, SOCK_DGRAM, 0);
socket_bind($socket, $server_host, $server_port);
$clients = [$socket];
while(true) {
$read = $clients;
$write = [];
$except = [];
if (socket_select($read, $write, $except, $poll_interval) < 1){
continue;
}
if (in_array($socket, $read)) {
echo "Client submitted request!\n";
//request parsing
}
}
So, when I run the server with $ php server.php it hangs for ever, as it should. When however I try to connect to it through telnet to post a request I get an error message:
$ telnet 127.0.0.1 21665
Trying 127.0.0.1 ...
Telnet: unable to connect to remote host: Connection refused
What I'm doing wrong and how can I fix it?
What I'm doing wrong
You are using telnet (a TCP client) to try to connect to a UDP port.
how can I fix it
Use a client that supports UDP, for example netcat as explained in this answer: telnet counterpart for udp
nc -u <host> <port>
Start typing and press enter.
Related
I have the following php script (tcp server)
<?php
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
// Bind the socket to an address/port
socket_bind($sock, "0.0.0.0", 14010) or die('Could not bind to address');
$i=0;
for(;;) {
// Start listening for connections
socket_listen($sock);
/* Accept incoming requests and handle them as child processes */
$client = socket_accept($sock);
// Read the input from the client – 1024 bytes
$in = socket_read($client, 41984);
socket_write($client, $in);
//socket_close($client);
}
// Close the master sockets
socket_close($sock);
?>
How I can make the server wait the [FIN + ACK] from the client before closing the socket?
You can instead of calling close, do a recv on socket (not sure of php construct). recv system call returns 0 on peer closure. If this is the case you can then close on server side.
I want to get a message back after succesfully connected with an UDP address via fsockopen. Connection with a RCON ModManager (game server).
What to expect?
If I use Telnet:
Telnet 31.204.131.9 15502 , I see:
ModManager Rcon v8.5
Digest seed: iJrrQAkv
Now in PHP:
<?php
$_ip = '31.204.131.9' ;
$_port = '15502';
if (($socket = fsockopen ('udp://'.$_ip, $_port, $errno, $errstr, 30))) {
// till here it works, got connected
// Digest seed?
if(fwrite($socket, "GET / HTTP/1.1\r\n")) { // writing works }
echo fread($socket, 1024); // NOTHING
fclose($socket);
}
?>
You can try it.. IP and Port valid. Thnx in advance!!
When you use telnet you use tcp protocol, not udp. Try
<?php
$_ip = '31.204.131.9' ;
$_port = '15502';
if (($socket = fsockopen ($_ip, $_port, $errno, $errstr, 30))) {
// till here it works, got connected
// Digest seed?
if(fwrite($socket, "GET / HTTP/1.1\r\n")) { echo "writing works\n"; }
echo fread($socket, 1024); // NOTHING
fclose($socket);
}
I am trying to receive a UDP multicast stream in PHP. The receive command never gets anything and waits forever.
I can watch the stream using VLC player so the stream is accessible on my machine. Any help on how to do this using PHP is highly appreciated.
Here is my code.
<?php
error_reporting(E_ALL | E_STRICT);
//create a new socket
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
//i am not sure about this command. I think i have to set this option to start receiving packets.
socket_set_option($socket, SOL_SOCKET, MCAST_JOIN_SOURCE_GROUP, array("group"=>"239.194.0.73","interface"=>"eth0","source"=>"239.194.0.73"));
$binded = socket_bind($socket, '127.0.0.1', 6073);
//receive data
$from = '';
$port = 0;
socket_recvfrom($socket, $buf, 12, MSG_WAITALL, $from, $port);
echo "Received $buf from remote address $from and remote port $port" . PHP_EOL;
?>
$binded = socket_bind($socket, '127.0.0.1', 6073);
Should be
$binded = socket_bind($socket, '0.0.0.0', 6073);
Or else you will only recv packets originating from the local host.
I'm trying since a couple of days to establish a TLS connection to a SMTP server in PHP via fsockopen() on my newly installed Ubuntu server. I#ve tried almost everything and googled for hours but still I didn't get it working.
The PHP code looks as follows:
$fp = fsockopen("tls://smtp.xxxx.com", 25, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
// some other stuff
}
The output is just (0), i.e., $errstr = null and $errno = 0.
OpenSSL is installed and enabled:
OpenSSL support: enabled
OpenSSL Library Version: OpenSSL 0.9.8o 01 Jun 2010
OpenSSL Header Version: OpenSSL 0.9.8o 01 Jun 2010
and the following stream socket transports are registered:
tcp, udp, unix, udg, ssl, sslv3, sslv2, tls.
The port is open as a telnet from the console works.
Any ideas what's wrong or how I could at least get some more debug output?
Thanks,
Markus
Your connection doesn't make much sense. By using the TLS handler, you want TLS to be established BEFORE any data goes. But port 25 is standard SMTP, which can only establish TLS AFTER you've initially connected via an unencrypted regular connection. Once that initial connection is established, then you can enable TLS with the STARTTLS command to tell the SMTP server to switch over.
If you want TLS from the get-go, then use port 465, which is ssl/tls from the start.
With gmail, the ssl connection port had ssl from the get-go, but the tls port, you connected plain, and had to start tls manually with a STARTTLS command. I'm guessing this is the same. Here's and example to gmail so you can see what's going on. The EHLO command shows the STARTTLS command while if you start with ssl from the begining, it goes strait to the AUTH XOAUTH command list.
<?php
function get($socket,$length=1024){
$send = '';
$sr = fgets($socket,$length);
while( $sr ){
$send .= $sr;
if( $sr[3] != '-' ){ break; }
$sr = fgets($socket,$length);
}
return $send;
}
function put($socket,$cmd,$length=1024){
fputs($socket,$cmd."\r\n",$length);
}
if (!($smtp = fsockopen("smtp.gmail.com", 587, $errno, $errstr, 15))) {
die("Unable to connect");
}
echo "<pre>\n";
echo get($smtp); // should return a 220 if you want to check
$cmd = "EHLO ${_SERVER['HTTP_HOST']}";
echo $cmd."\r\n";
put($smtp,$cmd);
echo get($smtp); // 250
$cmd = "STARTTLS";
echo $cmd."\r\n";
put($smtp,$cmd);
echo get($smtp); // 220
if(false == stream_socket_enable_crypto($smtp, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)){
// fclose($smtp); // unsure if you need to close as I haven't run into a security fail at this point
die("unable to start tls encryption");
}
$cmd = "EHLO ".$_SERVER['HTTP_HOST'];
echo $cmd;
put($smtp,$cmd);
echo get($smtp); // 250
$cmd = "QUIT";
echo $cmd."\r\n";
put($smtp,$cmd);
echo get($smtp);
echo "</pre>";
fclose($smtp);
If it works from the command line but not from within Apache then there is probably some difference between the PHP configuration: do a diff between /etc/php/apache2/php.ini and /etc/php/cli/php.ini and see what might have changed.
I want to talk with php server with socket by telnet.
I wrote 'echo' server (i send string to server, server send it to me)
i use chr(0) on end of output string to send information that string is sent
socket_write($client, $output.chr(0));
but telnet haven't see it and i cant send new string
TELNET
telnet 127.0.0.1 9000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
hello
hello_
PHP
<?php
set_time_limit (0);
$address = '127.0.0.1';
$port = 9000;
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
socket_bind($sock, $address, $port) or die('Could not bind');
socket_listen($sock);
while(true) {
$client = socket_accept($sock);
$input = trim(socket_read($client, 1024));
if ($input == 'off') break;
$output = $input.chr(0);
socket_write($client, $output);
}
socket_close($client);
socket_close($sock);
?>
what i'm doing wrong?
You should end the lines with carriage return or "\r\n", instead of just the \0 character. The telnet client watches out for them too I think.
The real problem however is your use of $client = socket_accept(..) within the loop. You must only establish the connection once, before the while. Otherwise you will reset the connected stream.