Python sockets will not connect - sockets

I'm trying to run a server and a client on two separate Windows 7 machines on the same network using sockets in Python 2.7. First I'm just trying to get them to connect before trying to do anything.
Currently my server is:
import socket
host = '0.0.0.0' #Also tried '', 'localhost', gethostname()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, 12345))
s.listen(5)
cs, addr = s.accept()
print "Connected."
My client is:
import socket
host = '127.0.0.1' #Also tried 'localhost', gethostname()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(host, 12345)
print "Connected."
The error I get is:
socket.error: [Errno 10061] No connection could be made because the target machine actively refused it.
I've looked through lots of other questions but none of the answers solved my problem. Any help is appreciated.
When I use the IP address of the server (10.0.63.40) as the host for the client, I get
[Errno 10060] A connection attempt failed because the connected party did not properly
respond after a period of time, or established connection failed because connected host has
failed to respond

You are saying these are two separate machines. You cannot reach the one machine from the other by connecting to 127.0.0.1 or localhost.
Listening on 0.0.0.0 is fine, this means that the listening socket is reachable from all interfaces, including the local network.
However, for connecting to your server, you obviously need to use the IP address (or hostname, if you have properly set up a local name server) of your server machine in the local network.
Per your comment, the local IP address of your server machine is 10.0.63.40. That means you should end up calling s.connect("10.0.63.40", 12345).

I had the same problem when I tried to connect my client code with server one. It got resolved my by using this command:
socket.gethostbyname(socket.gethostname())
: note[ I run it locally not uploaded it into a live server]

Related

psycopg2.OperationalError: could not connect to server: Connection refused - Same code works from one system but not from another

I am trying to connect to redshift and the same code (same host, User, DB name, pwd, port) works from one computer but throws connection refused error from another one . Both the systems first connect to VPN and trying to connect to database. Not sure if any of the config file needs to be updated with IP ? Not sure what could be the reason.
psycopg2.OperationalError: could not connect to server: Connection refused
Is server running on host xxx.xx.xx.xx and accepting TCP/IP connections on port YYYY
"Connection refused" means it couldn't even get ahold of the database to attempt to establish a connection with it. Whatever is going on is happening at the networking level, not within the database.

php: fbird_connect(): Unable to complete network request to host "localhost". Failed to establish a connection [duplicate]

I'm trying to connect to a remote Firebird database "test" (alias already added). It is not an embedded server, and is installed on VM with IP 192.168.1.147.
Here is my connection string:
User=sysdba;Password=masterkey;Database=test;DataSource=192.168.1.147
However I got an error:
FirebirdSql.Data.FirebirdClient.FbException (0x80004005): Unable to complete network request to host "192.168.1.147". ---> Unable to complete network request to host "192.168.1.147".
I've done some research on that but haven't got a clue yet. Some help needed. Thanks
My IP address is 192.168.2.108, and I can ping that server IP successfully
Make sure that
Firebird is running
Firebird is listening on port 3050 on the specified IP (or on 0.0.0.0)
Your firewall allows access to port 3050
You're using the correct host name
For Linux, the Firebird port is closed by default. You need to modify RemoteBindAddress in /etc/firebird/2.5/firebird.conf from
RemoteBindAddress = localhost
to
RemoteBindAddress =
and restart service.
Make sure you have excluded tcp port 3050 in VMs' firewall.
Ok. I think your connection to firebird is faililing because the firebird client looks up the network service file by name and does not find gds_db in the services file.
If your connection string refers to the host by using IP, the Client might fail to identify it because it gethostbyname() and not by IP
Manually include this in the file and you should be fine.
C:\windows\system32\drivers\etc\services

i got an error in http :socketException : OS error : connection refused

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception:
SocketException: OS Error: Connection refused, errno = 111, address =
127.0.0.1, port = 36832
The TCP/IP connection to the host , port has failed. Error: "null. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall."
Test this by running the following command on a Windows Machine:
Test-NetConnection -Port 36832 -InformationLevel "Detailed"
You will get details similar to:
WARNING: TCP connect to (2a01:XXX:XXXX::XX: 36832) failed
THIS INDICATES THE PORT IS NOT OPEN
If you get something like:
AVERTISSEMENT : TCP connect to (13.107.4.52 : 36832) failed ComputerName : internetbeacon.msedge.net RemoteAddress : 13.107.4.52 RemotePort : 36832
THIS INDICATES A DNS ISSUE.
How to open ports in Windows 10 via Elevated Powershell Prompt:
https://winaero.com/blog/open-port-windows-firewall-windows-10/
How to resolve DNS issues in Windows 10 on Local and Remotes Hosts:
https://www.ghacks.net/2016/10/20/how-to-fix-resolving-host-issues-on-windows/
You can also verify DNS Issues with nslookup it runs on Linux, Mac, and Windows. You will have to most likely install the modules with Chocolatey, Brew, Yum, df, aptly, and other package managers that are used by the variety of flavors for Linux. Furthermore, you can look at the following for more information
http://techgenix.com/10-ways-troubleshoot-dns-resolution-issues/
https://devblogs.microsoft.com/scripting/use-powershell-to-troubleshoot-client-dns/
Other things you can try:
traceroute
tcpdump
nmap
Please check that the Firewall Rules are correct on both local and remote machines, furthermore ensure that you have the ability to connect via ipsec/firewall rules and that your Database is configured to accept incoming connection on that port.
netstat -tlnp

Remotely connecting to postgresql server works from my computer does not work from hosting provider's

There is a PostgreSQL server on a Windows computer that I need to access remotely. It's not on the local network. I'm trying to connect to it with pg_pconnect. I have an AMP stack with PHP 7.1.23 on my local Mac computer and I can connect to it just fine. (The connection works, I can make queries, etc.) When I upload the same script to a hosting provider (LAMP stack with PHP 7.1.22) the script does not work anymore.
Warning: pg_pconnect(): Unable to connect to PostgreSQL server: could not connect to server: Connection refused
Is the server running on host "x.x.x.x" and accepting
TCP/IP connections on port 5432? in /path/to/my-script on line 42
I would think that the SQL server does not allow remote connections if it didn't allow the connection from my computer either. But to that server both connections would be remote, right? So how can it allow one and not allow the other?
Turned out that the hosting provider had to whitelist the IP address first. That's why it didn't work.

Bi-directional communication using sockets via ssh tunnel

I have a server host (S) and a bunch of clients (C1, C2, C3, ...). I would like to open connection between S and C1, C2, C3 respectively for bi-directional communication. Ideally using sockets. SSH for authorisation purposes is preferred.
Ideally:
Client C1 creates SSH reverse tunnel to S, forwarding C1's port so it is accessible on S as it's own port.
Client program running on C1 creates a socket and binds to forwarded port.
Server program running on S creates a socket and binds to forwarded port.
Client and server can exchange data.
Is something like that possible? I tried coding up a draft using Python but to no avail:
Firstly, I run on C1: ssh -N -R 9999:localhost:15432 root#example.com - OK
Secondly, on server I run:
import socket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(('localhost', 9999))
serversocket.listen(5)
while True:
connection, address = serversocket.accept()
buf = connection.recv(64)
if len(buf) > 0:
print buf
break
Thirdly, on client I run:
import socket
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect(('localhost', 15432))
clientsocket.send('hello')
But I'm getting socket.error: [Errno 111] Connection refused on client. Also if I set up tunnel first and then launch server program, I'm getting [Errno 98] Address already in use. It only works when I first start the program, then set up a tunnel.
If aforementioned concept does not make sense, what would you suggest to create sort of synchronisation tool so any client listens for queries from the server and can respond with data? (preferrably in Python).
Thanks in advance.
-N doesn't do what you seem to think it does: it's intended for the ssh-destination to be able to connect back to the originator. But that would make the originating side the server.
It sounds like you should be using -L to simply create a connection from the client through the ssh tunnel to the server.
To demonstrate: I have a local server named 'bree'. On that machine, I execute (this could also be your python server listening on port 9999):
nc -l 9999
Now on my client machine, I execute this in one window (or could put it in the background):
ssh -N -L 9999:bree:9999 bree
This says: listen on the local (client) machine to port 9999, and when a connection to it is made, forward the request through the tunnel, and connect to port 9999 on bree.
Now, in a second window on the client machine, I execute:
nc localhost 9999
The two nc instances are connected.