How connect socket in different port in PHP?
This code is working fine. I am successfully tested socket connection in my system in port:80 but not able to connect in different port .
<?php
echo "<pre>";
error_reporting(E_ALL);
echo "<h2>TCP/IP Connection</h2>\n";
/* Get the port for the WWW service. */
$service_port = 8000;
/* Get the IP address for the target host. */
$address = gethostbyname('localhost');
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
echo "OK.\n";
}
echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
} else {
echo "OK.\n";
}
echo "Closing socket...";
socket_close($socket);
echo "OK.\n\n";
?>
Output:
Attempting to connect to '127.0.0.1' on port '8000'...socket_connect() failed.
Reason: () Con开发者_运维技巧nection refused
Closing socket...OK.
I need to connect in port different than 80, so please any Idea? Thank You
You can't connect to a generic port if there isn't a server socket opened there.
精彩评论