How to connect to a socket server in my network? - python

After doing port forwarding on port 50000 on my router now i can connect to my server using a computer from another network :
SERVER CODE :
import socket
s = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
s.bind(("0.0.0.0" , 50000))
s.listen(5)
c , a = s.accept()
CLIENT CODE (that is connected to another network) :
import socket
s = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
s.connect(("my_public_ip" , 50000))
but the problem is that if i try to connect from my network to the server that is on my network (after doing port forwarding) i get an error :
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

Related

WinError 10061 and WinError 10022 in socket programming only on Windows

I have a very simple Python code for bind or connect to a port. it works without any error on Ubuntu and CentOs but I have an error on Windows 10. I turned off the firewall and antivirus but it didn't help.
my code:
import socket
port = 9999
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
try:
s.connect((host,port))
except:
s.bind((host, port))
s.listen(1)
print("I'm a server")
clientsocket, address = s.accept()
else:
print("I'm a client")
error on windows 10:
Traceback (most recent call last):
File "win.py", line 11, in <module>
s.connect((host,port))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
During the handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "win.py", line 13, in <module>
s.bind((host, port))
OSError: [WinError 10022] An invalid argument was supplied
Edit:
I found my problem is in Try... Except part, if I put this code in two files my problem will solve. But Why? try except don't work correctly in Windows?
The connect() fails because there is no server socket listening at (host,port).
The bind() fails because you can't bind to a hostname, only to an IP address. Unless you want to listen on just a specific network interface, you should bind to 0.0.0.0 to listen on all interfaces.

UDP Socket using IPV6

Hello everyone, I am implementing a bittorrent client and i have hit a roadblock.
Here is my code to get the peer list from a particular tracker which only deals in ipv6.
tracker = "udp://tracker.birkenwald.de:6969/announce"
parse = urlparse(tracker)
ip, port = (socket.getaddrinfo(parse.hostname, parse.port, 0, 0, socket.SOL_TCP))[0][4][0], parse.port
#ip, port = 2001:1b10:1000:8101:0:242:ac11:2,6969
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
sock.sendto(tracker_connection.bytestringForConnecting(), (ip, port))
#bytestringForConnecting() is a function for getting the byte version as written in the protocol.
Getting the following error
Traceback (most recent call last):
File "main.py", line 17, in <module>
t.get_peer_list()
File "/home/aditya/Desktop/CN/CN PROJECT/tracker.py", line 21, in get_peer_list
self.udp_request(url)
File "/home/aditya/Desktop/CN/CN PROJECT/tracker.py", line 64, in udp_request
sock.sendto(tracker_connection.bytestringForConnecting(), (ip, port))
socket.gaierror: [Errno -9] Address family for hostname not supported
The python socket documentation says that AF_INET6 addresses must be passed as a tuple of 4 values. If you pass a 2-tuple it'll be interpreted as AF_INET (IPv4) address.

I cant make my python server run with my IPV 4 address

import socket
PORT = 5050
SERVER = "Insert my public IPV4 address here"
ADDR = (SERVER, PORT)
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR) #here is the line I get the error for
The error says:
Traceback (most recent call last):
File "The Directory", line 15, in <module>
server.bind(ADDR)
OSError: [WinError 10049] The requested address is not valid in its context
Can I not use my IPV4 to make the server public?
If not then what do I use?

Socket Error: 111 [.connect((host, port))]

I am doing a course and it is teaching me socket right now but this code they are showing is not working for me?
import socket
def main():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "localhost"
port = 5000
s.connect((host, port))
print("It works!")
if __name__ == "__main__":
main()
Error:
Traceback (most recent call last):
File "create_connection.py", line 14, in <module>
main()
File "create_connection.py", line 9, in main
s.connect((host, port))
ConnectionRefusedError: [Errno 111] Connection refused
EDIT:
On this video there is nothing listening but there is no error?
Connect request failed with connection refused.
You need a TCP server which is listening on 5000 port.
There should be a server waiting for connection in port 5000, only then it will work. Otherwise this error is expected

Creating non-blocking socket in python

I was trying to understand how non-blocking sockets work ,so I wrote this simple server in python .
import socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('127.0.0.1',1000))
s.listen(5)
s.setblocking(0)
while True:
try:
conn, addr = s.accept()
print ('connection from',addr)
data=conn.recv(100)
print ('recived: ',data,len(data))
except:
pass
Then I tried to connect to this server from multiple instances of this client
import socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1',1000))
while True:
continue
But for some reason putting blocking to 0 or 1 dose not seem to have an effect and server's recv method always block the execution.
So, dose creating non-blocking socket in python require more than just setting the blocking flag to 0.
setblocking only affects the socket you use it on. So you have to add conn.setblocking(0) to see an effect: The recv will then return immediately if there is no data available.
You just need to call setblocking(0) on the connected socket, i.e. conn.
import socket
s = socket.socket()
s.bind(('127.0.0.1', 12345))
s.listen(5)
s.setblocking(0)
>>> conn, addr = s.accept()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.7/socket.py", line 202, in accept
sock, addr = self._sock.accept()
socket.error: [Errno 11] Resource temporarily unavailable
# start your client...
>>> conn, addr = s.accept()
>>> conn.recv() # this will hang until the client sends some data....
'hi there\n'
>>> conn.setblocking(0) # set non-blocking on the connected socket "conn"
>>> conn.recv()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
socket.error: [Errno 11] Resource temporarily unavailable
https://docs.python.org/3/library/socket.html#socket.setdefaulttimeout
You can use s.setdefaulttimeout(1.0) to apply all connection sockets as default.

Categories

Resources