Can't connect to remote server on specific port - php

I need to make a TCP/IP socket server in PHP.
So I downloaded an example from php.net and tried it on my own machine (changed default script socket ip to 127.0.0.1). I opened it from web-browser (because I use XAMMP system, so I can't do a background working shell script), connected to 127.0.0.1:8175 by Telnet and it worked fine!
But now I have a problem:
I uploaded that script to remote server, opened port 8175 on the router and could not connect to the remote port from my computer. But I still can connect to the web and ssh ports on that server.
Any ideas?
If I asked that question in a wrong place, just say it. And... Sorry for my bad English.

If you set socket default IP to 127.0.0.1 you will be able to connect only from the machine running the script. So you need to change it something like your server's IP or 0.0.0.0 to accept all connections but take care of security measures.

Related

How to connect to remote DB server with IP address and mysqli_connect [duplicate]

I am trying to connect mysql database using php. But it is running fine on local machine but not when hosted online and error appeared as
mysqli_connect(): (HY000/2002): Connection timed out
<?php
$link = mysqli_connect('sql6.freesqldatabase.com','user_name','password','database_name');
if(mysqli_connect_error())
die("couldn't connect to database");
?>
Your MySQL server is probably not configured to accept requests from the host you're trying to access it from.
Check the configuration for the server.
Since you say the local machine can access it check where you permitted this and do the same for the ip of the online host.
In my case, AWS firewall was blocking my web server.
Adding the web servers IP address to the firewall exceptions fixed
this for me.
There might firewall in the host which is blocking the request to MySQL server. In my case adding the firewall rule helped me out.
Ubuntu
To allow one device to access the MySQL server,
sudo ufw allow from remote_IP_address to any port 3306
To allow any device to connect to MySQL server,
sudo ufw allow 3306
It worked!
If your website and MySQL database are hosted on separate servers, make sure that you've mentioned correct MySQL hostname (resolvable) in connection string.
In case you've specified the correct username, most probably your MySQL server isn't allowing remote MySQL connections from web server where you site resides.
Contact your web hosting provider for the exact MySQL hostname and to allow your web server IP address for remote MySQL connections.
I think you have to white list your hosting server's ip address to your DB server.
So your DB server will accept requests from your hosting server.
Specify the port (3306?) to the connection string as well.

mysqli connect via IP address but not using host name

I do test run with mysqli on remote host, Working fine with IP address but its not working with fully qualified domain name.
DNS is correctly mapped,I can browse the page using browser and ping also success
$mysqli = new mysqli("www.testdomain.com", "uname", "passwd", "database");
Any suggestions ?
mysql is running on Centos 6
First, unless you have a particular reason why you have to add www. before testdomain.com(for example, testdomain.com and www.testdomain.com are pointing to different address), please try again without www. in your host name.
Try nslookup testdomain.com on the machine that hosts your php application(or write a php script and call gethostbyname), see if php can find the correct IP address from given host name.
If the IP address you got from last step is different from the one you tried, try to troubleshoot DNS resolution problem, or edit /etc/hosts file on the machine that hosts your php application, and point testdomain.com to correct IP address. Some servers may have multiple IP addresses bind to them, but MySQL server only listens to one of them, check /etc/mysql/my.cnf on the machine hosts your MySQL server and look for bind 1.2.3.4(or other host name/IP address). In case you don't already know, MySQL server only accepts connections from localhost by default.
If your MySQL server if configured to listen to a particular host name/IP address, try bind to 0.0.0.0 so your MySQL server allows connections from all sources. Also there could be a firewall blocking connections from other sources. Here's how to check firewall configuration on CentOS.
If your php script can resolve testdomain.com, try using mysql command-line client to connect to your database from the machine that hosts your php applicaiton. mysql -h testdomain.com -u uname -p. The command-line client may give you more information on why the connection failed.
I'm not sure if you're using hosted VPS services, such as Amazon EC2, or you have your own server machine, but Amazon EC2 does provide out-of-box firewall, it's called "Security Group". Login to your EC2 console, and click "Security Group" on left-hand-side panel, then you can edit your firewall settings on right-hand-side panel.

Connect websocket server by LAN IP address

I have set up a websockets chat with the purpose of learning. Everything is working but I can't figure this issue out.
When I supply 127.0.0.1 as the address of the connection on the client side then I can access the server from the computer that's hosting it, but when I change the address to the actual LAN address of the hosting computer I can't connect the server even from the host itself. See:
Server = new FancyWebSocket('ws://127.0.0.1:9300'); Appears to work but only the computer that's hosting the server can connect ( for obvious reasons )
Server = new FancyWebSocket('ws://192.168.1.3:9300'); No computers can connect. I confirm 192.168.1.3 is the LAN address of the hosting computer.
What address do I need to put in there so that other computers from my local network can connect?
I solved the problem. Since it was a combination of two answers I thought the only fair thing to do was add another answer with an explanation.
As #Mehran suggested, I have had the server address set up as 127.0.0.1 instead of the network address. After changing that to 192.186.1.3 I was able to connect from the server itself, but other machines were unable to connect. Then I did the steps from the guide provided in #vtortola's answer to add a new inbound rule into the server's firewall in order to allow that port to be used.
So finally it all works now, thank you very much for helping me. +rep to everyone!
I'm pretty sure this is due to the configuration of your WebSocket server. It must be listening to localhost (127.0.0.1) to accept incoming connections in which case it won't answer to those aiming 192.168.1.3.
Since you didn't mention which server you are using I can not be specific but in general there are two ways to instantiate a listening socket, binding it to a specific IP address or * to bind whatever addresses system has. You need to configure the later if you intend to answer server connections coming from any computer within your LAN.
It looks like a Firewall/Policies issue to me.
IIS 7 Windows 2008, Localhost work but not local ip or external ip
Your TCP 80 could be allowed because the IIS installation will open it, that will explain why normal web browsing works. But you are trying to connect to the TCP 9300, that is very unlikely that is allowed by default.
Give a try to this: How to Open a Port in the Windows 7 Firewall , and allow that port.
Here are some things that you can safely assume while troubleshooting this issue:
If the service is able to work on 127.0.0.1 on the same machine, you can assume that the problem is not in the code or the PHP configuration
If you are not receiving an error when the server tries to bind to 192.168.1.3:9003 you can safely presume that the service is working. Try opening the Resource Monitor to see if it is actually listening on this port to confirm. To do so, go to 'Start Menu' in Windows and type 'Resource Monitor' in the 'Search programs and files' box. After opening the Resource Monitor, click the 'Overview' tab and find the name of the server process (typically 'php' if your using a CLI). With your process selected, switch over to the 'Network' tab and you will be able to see if it is listening on any ports within the 'TCP Connections' panel. This will show you what address and port it is listing on, as well as the remote address and port of any clients connected to the service.
If you know the server is running, and you know that it is actively listening on the expected address and port, it is very likely a firewall issue within Windows or your router. Note that even though 192.168.1.3 is the IP assigned to your interface, this is not a local IP and all communication to and from 192.168.1.3 will still go through the Windows firewall, including if being sent on the same machine. If your already at this point, I would strongly suggest checking your windows firewall first. If it is not the Windows firewall, check your router to see if it is blocking the port, and also check port forwarding and other setting to make sure that the router isn't otherwise interfering. We can likely help you with router issues here, but have your router's manual handy.
HTTP is a common service port so it is very possible that the router is not blocking the port, and windows may have automatically opened it if you are using IIS. 9300 is not a common port so it is unlikely to be open by default under any situation, unless your default is "all in", which effectively means your not using a firewall.
Another thing you might try (if possible) is closing your existing HTTP service and bind to port 80 using your Websocket service, or if possible (and while exercising caution) turn off your windows firewall completely to see if it works long enough to connect.
In general, don't try to reach your local network IP address from your own machine. There are very confusing things that happen at the socket layer here that I'll try not to delve too far into. The OS goes out of its way to make this work. Sometimes. I would expect that you cannot reach 192.168.1.3 (the server I'm assuming) from itself. There's a translation between local endpoint addresses when you do that which complicates everything.
A network switch will typically not send a frame back down a port it just received it from, so what you're seeing when you ping your local IP in cmd prompt is a loopback shortcut the OS is taking.
Not being able to reach it from another machine causes me suspect that the socket is not bound correctly on the server. Double check that you are explicitly declaring the socket on the server (address and port), and that your're binding your listener to that socket. Also ensure that the address you're binding to is for the correct network adapter. I see this all the time with laptops or machines that have multiple connected adapters.
Unfortunately I cannot be more targeted with my response as I am unfamiliar with what a FancyWebSocket is or how it is constructed.
I can help you if its a linux system.
If there is no name server on the local network, it is still possible to establish a small table mapping IP addresses and machine hostnames in the /etc/hosts file, usually reserved for local network stations.
This file is available even during network outages or when DNS servers are unreachable, but will only really be useful when duplicated on all the machines on the network. The slightest alteration in correspondence will require the file to be updated everywhere. This is why /etc/hosts generally only contains the most important entries.
This file will be sufficient for a small network not connected to the Internet, but with 5 machines or more, it is recommended to install a proper DNS server.
Try adding all the 'ip:port' along with a hostname and copy the template in file /etc/hosts in all the system.
Hope it resolves the issue!

Cannot connect to database by remote server

i have this problem:
I've created a Cms on my server hosted in Aruba (i call this My-server), i need to connect to a remote server (i call this external-server) to update its Database (Mysql) and ftp uploads via php.
I opened ports and connection in the external-server but it seems that Aruba blocks connection to external servers, even if tunnelling by port 80.
When i use my cms from virtual server (APache) on my laptop all works but when i moved to the server i got problems.
Any advice to solve the problem?
I cannot move my cms to external-server for politicy reason.
In your MySQL config add this
skip-external-locking
and in the config also remove this line below as if this is in place then no outsider can login.
bind-address = 127.0.0.1
restart the MYSQL after this.
You can asked Aruba to provide you access to port 80, 3306 etc. Just ask them I think they will remove it through their firewalls or iptables.

Connecting to localhost from a remote site

I have two sites. One is remote (online) and another localhost (on my developer machine).
The remote site in on dedicated IP address XXX.XXX.XXX.XXX and the localhost machine is public IP YYY.YYY.YYY.YYY. The remote site is a wesite running online like any other internet site.
Both remote and local machine are running mysql db and PHP.
What I need is to have the remote site (on the internet, IP address XXX.XXX.XXX.XXX) connect to my local machine (with the public IP YYY.YYY.YYY.YYY) when some specific webscripts are run by visiters to my website.
How do I establish a connection to my localhost from the remote website using PHP?
Please note that I'am asking for the opposite of normal practice of having a local machine connect to a remote machine.
You will need to configure port forwarding or a DMZ on your router. Port forwarding on port 80 will probably be easiest and more secure (it'll stop web traffic on other ports connecting to your PC which a DMZ would otherwise allow).
The process for setting up port forwarding differs from router-to-router, but it should be relatively easy to find in your router's admin panel. Find the private IP of your PC and, in your router's config, set up so all requests on port 80 to forward to the PC's internal IP.

Categories