0

I am trying to set up a local server so that other PCs on the same local network can connect to it. When trying to do so, on the client side, I get the following error: [Errno 10061] No connection could be made because the target machine actively refused it

I have been searching around for hours and still couldn't resolve this issue. I tried turning off my Firewall too, but nothing.

These are my server and client codes:

Server Code:

import socket import threading import SocketServer import datetime ver_codes = [] class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler): def handle(self): print threading.current_thread().isDaemon() data = self.request.recv(1024) command = data.split()[0] if(command=="login"): if(logged_in(data.split()[1])==False): self.request.sendall(login(data.split()[1], data.split()[2])) else: self.request.sendall("already in") class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): pass def client(ip, port, message): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((ip, port)) try: sock.sendall(message) response = sock.recv(1024) print "Received: {}".format(response) finally: sock.close() def logged_in(id_num): for i in ver_codes: if(i[0]==id_num): return True return False def login(username, password): login_file = open("Login.txt", "r") match = login_file.readline() while(match!="*"): if(match.split()[0]==username): if(match.split()[1]==password): ver_codes.append([match.split()[0], encryption_code(match.split()[2])]) login_file.close() return "{} {}".format(match.split()[2], encryption_code(match.split()[2])) print "And Here" match = login_file.readline() return "Denied" login_file.close() def encryption_code(to_encrypt): now = datetime.datetime.now() return int(str(now.microsecond)) * int(to_encrypt) if __name__ == "__main__": HOST, PORT = "localhost", 7274 server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler) ip, port = server.server_address print server.server_address server_thread = threading.Thread(target=server.serve_forever) server_thread.daemon = False server_thread.start() print "Server loop running in thread:", server_thread.name 

Client Code:

import socket import sys HOST, PORT = "localhost", 7274 data = " ".join(sys.argv[1:]) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.connect((HOST, PORT)) sock.sendall("login mahdiNolan m1373") received = sock.recv(1024) finally: sock.close() 

I really appreciate any help you could give me! Thanks A LOT beforehand!

1
  • Try first to run two terminals, and run in each an interactive python. Run in the first: import socket; t=socket.socket(); t.bind(("localhost", 7274)); t.listen(1); s,_=t.accept() and in the second: import socket; s=socket.socket(); s.connect(("localhost", 7274)). If it works, you get two socket objects and you can play with s.send("hello") and s.recv(100). Does it work? Commented Jan 27, 2013 at 18:24

1 Answer 1

1

Your issue is because you're listening on localhost - this will only accept connections from the local machine.

If you want to accept connections from anywhere, instead of "localhost" just pass the empty string "". This is equivalent to specifying INADDR_ANY to the C sockets API - see the ip man page for more information, or this page also looks like it has some useful explanation. In short, this means "accept connections on any local interface".

Instead of the empty string you can instead specify an IP address of a local interface to only accept connections on that interface - it's unlikely you need to do this unless you machine has multiple network cards inside it (e.g. acting as a gateway) and you only want to serve requests on one of the networks.

Also, on the client side you should use the actual address of the machine - replace "localhost" with the IP address or hostname of the server machine. For example, something like "192.168.0.99". If you want to find the IP address of the server under Windows, open a DOS window and run the ipconfig command, look for the line with IPv4 Address (assuming you've got an IPv4 network which is very likely).

The Windows firewall will also block the server from accepting connections as you've already found, but you shouldn't need to disable it - as soon as you run your server you should see a popup window where you can instruct it to accept connections (that was on Windows 7, it might be different on other versions). In any case, turning the software firewall off should allow everything to work, although whether that's a security risk is a matter outside of the scope of this question.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.