I am trying to connect to my public IP address but to no success.
The port is open as checked using - http://www.yougetsignal.com/tools/open-ports/
and a request from this site (http://www.rexswain.com/httpview.html) works.
My problem is with this PHP code below. This used to work fine when used on my local network. Now I am using it on a web server.
Could the problem be with the web server to allowing the request to be sent or am I doing something wrong?
$ip = "MYIP";
$port = "600";
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
//socket_bind($sock, $ip, $port) or die ("cannot bind to address.");
echo($ip);
$state = socket_connect($sock,$ip, $port);
$error = socket_last_error();
$errorMsg = socket_strerror($error);
echo($errorMsg);
EDIT : The error in my comment below was derived with the socket_bind commented (Code edited to output the error)
Error : Connection timed out
This is wrong:
socket_bind($sock, $ip, $port) or ...
$state = socket_connect($sock,$ip, $port);
Bind binds the local address of the socket, it doesn't make sense to bind and connect to the same port. In general you do not want to bind in client mode. Remove the line completely, then you may get real error.
Related
I am having a hard time getting data from the server. Basically server waits for a connection from anyone using correct username and password. So when I use that using local explorer it shows the data on the browser.
Now what I was trying to do is, get this data using socket and forward it to another server address. But I could not even connect to the server to get the data like I get on the browser. Here is what I have tried:
$host = "192.168.1.4/online?user=dneb&pass=mella88";
$port = 1850;
// don't timeout!
set_time_limit(0);
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Could not create socket\n");
// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");
// clean up input string
$input = trim($input);
echo "server Message : ".$input;
When I run this code I get warning message:
Warning: socket_bind(): Host lookup failed [11004]: The requested name is valid, but no data of the requested type was found. in C:\xampp\htdocs\socket\client.php on line 10
Could not bind to socket
Or suggest other methods to use.
When you create a socket server, you don't specify the host as the full URL to your script; that's why the bind is failing.
Take a look at this sample: http://www.php.net/manual/en/sockets.examples.php
please try changing
$host = "192.168.1.4";
I have a server service script and client service script written in PHP using PHP's sockets API. They are working fine in localhost, but after uploading those two scripts to my hosting account on ipage, the server script is giving me an error on the socket_bind call. Here is the code I am using on the server:
$host = "my_own_hosting";
$port = 25003;
$message = "Hello Client";
set_time_limit(0);
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0)
or die("Could not create socket\n");
// bind socket to port (this is the call that returns the error)
$result = socket_bind($socket, $host, $port)
or die("Could not bind to socket\n");
// put server into passive state and listen for connections
$result = socket_listen($socket, 3)
or die("Could not set up socket listener\n");
Any ideas on why this call is not working?
First and foremost:
Use socket_strerror and socket_last_error to figure out your problem:
if (!($result = socket_bind($socket, $host, $port)){
die(socket_strerror(socket_last_error($socket)));
}
You are trying to run your server script on a shared PHP hosting provider (ipage).
These kind of providers do not generally allow to use the PHP sockets interface to build your own services, or implement harsh restrictions (Such as allowed ports or protocols for running the service) to avoid abuse and prevent safety issues. Either you comply with them or change your hosting service to one that fits your needs.
It seems your provider allows using the sockets interface though.
(You can check it out by searching for socket on http://www.hostingreviewbox.com/features/ipage-php/ although it's best if you make sure of it by running phpinfo on your account yourself, or even better: Open up a support ticket with your hosting provider and ask them if sockets are in any way restricted on your account.)
Then you should make sure of 2 things:
Are you using the proper ip address on $host?
You can ping my_own_hosting and see what you get or even better, add this code snippet to your server script for figuring out your server's public ip at runtime:
$host = my_ip();
echo "my ip address is $host\n";
function my_ip($dest='8.8.8.8', $port=53)
{
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_connect($socket, $dest, $port);
socket_getsockname($socket, $addr, $port);
socket_close($socket);
return $addr;
}
(from http://php.net/manual/en/function.socket-create.php#49368)
Is the port you are trying to use already in use or restricted?
Try to use a different port or ask your hosting provider about it.
On a side note:
Trust the PHP manual before its comments: You should use getprotobyname or SOL_TCP for determining the third parameter of socket_create instead of just sending 0.
Lastly, you should consider using socket_create_listen instead of socket_create + socket_bind + socket_listen. Or, for this particular case, stream_socket_server and stream_socket_accept. Like the PHP manual shows, they are way more practical:
$socket = stream_socket_server("tcp://{$host}:{$port}", $errno, $errstr);
if (!$socket) {
echo "$errstr ($errno)<br />\n";
} else {
while ($conn = stream_socket_accept($socket)) {
fwrite($conn, 'The local time is ' . date('n/j/Y g:i a') . "\n");
fclose($conn);
}
fclose($socket);
}
I'm implementing websockets for a chat system. The server.php is being invoked via ssh with php /www/server.php, and the first time it executes fine; but if the process is stopped (ctrl+z), this error is displayed after trying to invoke php /www/server.php again:
Warning: socket_bind(): unable to bind address [48]: Address already in use in /www/server.php
These are the contents of the /www/server.php file:
<?
$host = '10.10.0.103';
$port = '1337';
//Create TCP/IP sream socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
//reuseable port
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
//bind socket to specified host
socket_bind($socket, 0, $port);
//listen to port
socket_listen($socket);
// ... etc
So, I have two questions:
Can I reuse the same address / port after stopping the php /www/server.php job with a FLAG? Isn't SO_REUSEADDR supposed to reuse the same address/port?
What are the best practices for this issue? Restarting the websockets server could be an everyday task, e.g.: after updating the contents of /www/server.php
Thanks for any tips!
I am trying to connect via TCP Socket to a third party product. I am able to do so (and send a message successfully) with the following code:
if (isset($port) and ($socket=socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) and (socket_connect($socket, $address , $port)))
{
$text="Connection successful on IP $address, port $port";
$data = "<msg id=1>text</msg>" . $newline;
socket_send($socket, $data, strlen($data), MSG_DONTROUTE);
$text .= socket_strerror(socket_last_error());
socket_close($socket);
}
The down side is that I also need to listen on this socket. As I understand it, I first need to bind, rather than using connect. When I run the code below, I get this error when calling socket_bind
Only one usage of each socket address (protocol/network address/port) is normally permitted.
code (snippet from a TCP Class, I have verified that the IP address, port, etc are the same in both snippets):
$this->Socket = socket_create(AF_INET, SOCK_STREAM, 0);
$result = socket_bind($this->Socket, $this->Host, $this->Port);
$result = socket_listen($this->Socket, 3) or die("Could not set up socket listener\n");
$spawn = socket_accept($this->Socket) or die("Could not accept incoming connection\n");
How is it that I can connect using socket_connect but not socket_bind? Is it correct that I need to use socket_bind if I want to listen on this socket?
I am running PHP on a Windows XP Apache installation (Zend). I should also mention that the only other application running when I try to execute this code is the third party program to which I am trying to connect.
Thank you for any insight.
Hey guys, I am trying to do some socket programming in PHP.
So I am running a socket "server":
$address = '127.0.0.1';
$port = '9999';
$masterSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($masterSocket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($masterSocket, $address, $port);
socket_listen($masterSocket, 5);
$clientSocket = socket_accept($masterSocket);
So I open up SSH and run this script. It is running, no errors.
Then I have another PHP script which attempts to connect to this:
$fp = fsockopen("me.com", 9999, $errno, $errstr, 30);
fclose($fp);
but it's giving me:
Warning: fsockopen(): unable to connect to me.com:9999 (Connection refused)
How do I begin to fix this?
You haven't finished the listening socket sequence, you need to call socket_accept to accept new connections. There is an example in the comments in the PHP documentation.
$clients = array();
$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
socket_bind($socket,'127.0.0.1',$port);
socket_listen($socket);
socket_set_nonblock($socket);
while(true)
{
if(($newc = socket_accept($socket)) !== false)
{
echo "Client $newc has connected\n";
$clients[] = $newc;
}
}
http://php.net/manual/en/function.socket-accept.php
1) Check if the port is firewalled off. You could use telnet to check this.
2) See if it works when the client and server are on the same machine (I'm guessing from your mention of SSH that the server is remote).
3) If it works locally and you can hit the remote port using other tools then it's going to be tricky. I'd suggest you wail and gnash your teeth for a bit; I'm out of ideas.
EDIT: Heh. Or you could just read Steve-o's answer. Teeth-gnashing is still an option.
I know you said that "me.com" is an example but, just to be sure, socket_bind is expecting an IP address.
From http://php.net/manual/en/function.socket-bind.php :
address
If the socket is of the
AF_INET family, the address is an IP
in dotted-quad notation (e.g.
127.0.0.1).
If the socket is of the AF_UNIX
family, the address is the path of a
Unix-domain socket (e.g.
/tmp/my.sock).
I know the question is very old, but if someone still has this problem, make sure you connect to the SAME address you are listening on,
For example, If you're listening on 127.0.0.1 and your Machine address is me.com, you won't be able to connect to me.com with it, for that you'll have to listen on me.com.
Listening on: localhost:8088
Can only connect via: localhost:8088 // not via me.com:8088
Listening on: me.com:8088
Can only connect via: me.com:8088 // not via localhost:8088