I am trying to create an android app which connects to a socket created on the desktop. I did a lot of testing and figured out that my routing was giving me a hard time.
When I tried to connect over a mobile hotspot, it was working perfectly fine. But when I connected over the router, my connection was getting rejected or something. I tried connecting both ways, one with my android as serversocket and one with my desktop as server socket.
I think the issue is because of port forwarding or something, but since I'm new to networking, I don't know how to configure it.
I looked up online and tried some stuff myself, however nothing worked.
This is how my D-Link router port forwarding setup is.
D-Link router
I have a socket running at port 8080 hosted by my android device with ip 192.168.0.122 and my laptop is at ip 192.168.0.123
This is client code which i have on my laptop(client.py)
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = input("IP: ")
port = int(input("Port: "))
s.connect((ip,port))
print(s)
s.send(bytes(str("Hello!"), "utf-8"))
I am using python(spyder) to connect as client by enterring the socket ip address and the port number.
The main problem I am facing is the weird behaviour of it working sometimes and then after like a few hours, it just stops working without doing anything. Then I try the same method I did last time to make it work, but it still doesn't work. I feel like my router is moody and only works when it wants to, and that is the weird behaviour.
Related
I’m trying to create a server on my raspberry pi using python and then i want to test the server by accessing it from another device using the IP address of the raspberry pi, but the problem is that everytime i type the IP address of my raspberry pi into my webpage, it doesn’t open and i don’t know if there’s a problem in my code or not, i will write below so that anyone could check
import socket
import sys
my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
try:
my_socket.bind((host, 1234))
except socket.error:
print(‘failed’)
sys.exit()
my_socket.listen(5)
While True:
conn, addr = my_socket.accept()
data = conn.recv(1000)
if data:
print(‘got a request’)
my_socket.send(‘Thank you’)
my_socket.close()
conn.close()
after that i tried typing the raspberry pi’s IP address on my laptop’s webpage but it was no use, first i typed ifconfig in the terminal of the raspberry pi to get the IP address and i tried it but it didn’t work, then i added another line in the code which is gethostbyname to the variable host and printed it and it showed different IP address than the one in ifconfig which was confusing to me, but i even tried this another IP address on my webpage and it didn’t work too
Did you check with netstat to see if the code is actually listening? Should list the <IP>:<Port> as LISTENING, i always check that when i do server code.
Sometimes when you try out code and don't terminate it properly, there can be a orphan process still listening to the Interface:Port hogging the port. Been there, done that, got the T-shirt.
Also, try using 0.0.0.0 instead, it tells the socket listener to listen on all interfaces, including loopback.
Got any firewall denying the connection ? Check that.
Also, check try using curl as a debug tool and see if 1) Curl can connect and 2) you get send some HTTP data to the server:
Curl 127.0.0.1:1234/HelloWorld
Is there a way to connect to another computer via their public IP using python sockets, so that you can send data?
In a similar way to which you can connect to a webpage, etc can you do something like this:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip, 6000))
I have seen examples of people using this in DOS scripts but can't seem to recreate it. All I get is timeout errors. The only time it worked was when using port 5000 and connecting to my own IP (but all other ports failed), any idea why this happened as well?
I'll try be concise, but please let me know if I can provide any more helpful pieces of information.
I have client and server Python programs, and they work fine when ran on the same machine, and when the client connects to my machine's local IP (not 127.0.0.1, but the IP assigned to my machine). I have not been able to get this to work with my public IP.
I get a [Errno 61] Connection refused error when I try to get the client to connect to my router's public IP address. My server binds to all interfaces using bind(("0.0.0.0", 50000)), and I already set up port forwarding for my router. I verified that the program is listening on that port by running netstat -an | grep LISTEN and finding the following line:
tcp4 0 0 *.50000 *.* LISTEN
I can also seemingly reach the port through an online port checking tool, which shows that the port is open when I am running my program, and closed when I close that program. My program also registers the connection from this tool.
The fact that my program accepts the connection from the port checking tool gives me the impression that my client code is missing something, but I can't find any answers. It might be worth noting that I am still running my server and client code on the same machine, but I'm not sure why that would derail things. Here's the code I use to connect on the client side:
tcp_client = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
tcp_client.connect(('my_public_ip', 50000))
Are there any diagnostic steps that I can follow to narrow down my issue?
Before you spend any more time on this, try connecting to your public ip from a computer outside your home network. Spend a couple of dollars on an AWS instance for an hour if you have to, or try connecting from a friend's machine, whatever. It will probably work just fine.
I suspect the problem is simply that you cannot, from inside your home network, connect to your router's public ip address. I tried the same thing with my local network and ran into the same behavior.
If you really need to your public ip during development, you can just assign that as an alias to one of your local interfaces (ip addr add 1.2.3.4/32 dev eth0)...but it's probably easier just to use your an address on your local network, or just arrange for regular access to a remote system for testing.
I have a python socket that is supposed to be listening to all incoming sockets on port 2022 and I have port forwarding enabled as well as a dynamic DNS service. When I am connected to the same network and I try to connect to my pc using the pc's IP address it works like a charm but when I try to use the public IP of my network it won't work although I have done proper port forwarding. Here is my code, it stucks on the last line and doesn't accept any connections outside the network:
IPC= '0.0.0.0'
PORTC =2022
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.bind((IPC,PORTC))
client_socket.listen(5)
connection =client_socket.accept()[0].makefile('wb')
If you can connect from a different PC inside your private network, then there's nothing wrong with your Python code. It's almost certain that there's something wrong with your router or the port forwarding config.
Make sure that you've re-reviewed your port forwarding settings and perhaps try restarting the router (be careful to verify your internally assigned IP addresses haven't changed if you go this route).
For a class project I'm trying to do some socket programming Python but running into a very basic issue. I can't create a TCP connection from my laptop to a lab machine. (Which I'm hoping to use as the "server") Without even getting into the scripts I have written, I've been simply trying interpreter line commands with no success. On the lab machine (kh4250-39.cselabs.umn.edu) I type the following into Python:
from socket import *
sock = socket()
sock.bind(('', 8353))
sock.listen(5)
sock.accept()
And then on my laptop I type:
from socket import *
sock = socket()
sock.connect(('kh4250-39.cselabs.umn.edu', 8353))
At which point both machines block and don't do anything until the client times out or I send a SIGINT. This code is pretty much exactly copied from examples I've found online and from Mark Lutz's book Programming Python (using '' for the server host name apparently uses the OS default and is fairly common). If I run both ends in my computer and use 'localhost' for the hostname it works fine, so I suspect it's some problem with the hostnames I'm using on one or both ends. I'm really not sure what could be going wrong on such a simple example. Does anyone have an idea?
A good way to confirm whether it's a firewall issue or not is to perform a telnet from the command-line to the destination host in question:
% telnet kh4250-39.cselabs.umn.edu 8353
Trying 128.101.38.44...
And then sometime later:
telnet: connect to address 128.101.38.44: Connection timed out
If it just hangs there at Trying and then eventually times out, chances are the connection to the remote host on that specific port is being blocked by a firewall. It could either be at the network layer (e.g. a real firewall or a router access-list) or at the host, such as iptables or other host-based filtering mechanisms.
Access to this lab host might only be available from within the lab or the campus network. Talk with your professor or a network administrator or someone "in the know" on the network to find out for sure.
Try to bind the server to 'kh4250-39.cselabs.umn.edu' instead of '':
sock.bind(('kh4250-39.cselabs.umn.edu', 8353))
If this does not work: Another reason could be a firewall blocking the port 8353....