1

I'm a beginner in python socket programming and I have to send a message from the server to the client side . I have 2 python IDLES one for the server and one for the client. I have made the server file with no errors but when I create a connection socket in my client file and try to connect to server I get the error:

clientSocket.connect((servername,port))
ConnectionRefusedError: [WinError 10061] no connection could be made because the target machine actively refused it

I don't know how to deal with this error and I would appreciate your help with guiding me. Thank you in advance. My code:

Server:

from socket import *

port = 1234
serverSocket = socket(AF_INET,SOCK_STREAM)
serverSocket.bind(('',port))
serverSocket.listen()
print("Server has started")

data = "Network labs"
while True:
    connectionSocket , addr = serverSocket.accept()
    connectionSocket.send(data)
    connectionSocket.close()

Client:

from socket import *

port = 1234
servername = 'localhost'
clientSocket = socket(AF_INET,SOCK_STREAM)
clientSocket.connect((servername,port)) #this is where the error happens
SkryptX
  • 813
  • 1
  • 9
  • 24
Vasilis Skentos
  • 506
  • 11
  • 22
  • 2
    Could be a rights or a firewall problem. Try a port > 5000 and check into your operating system's firewall rules. – tdelaney May 24 '20 at 18:41
  • Are your client and server really running in the same host? – user207421 May 24 '20 at 22:45
  • @tdelaney Try a port > 5000 why? It's the ports below 1024 that need privilege. And it's hard to see how it can be a firewall problem in localhost. – user207421 May 24 '20 at 22:46
  • @user207421 - I depends on the operating system. 5000+ is a likely range, but no guarantees. – tdelaney May 24 '20 at 22:49
  • @tdelaney I've only been doing this for thirty years but it has been 1024 in every OS I have ever bound a socket on. If you have a counter example where it is 5000, or even 1025, please produce it. – user207421 May 24 '20 at 23:12
  • @user207421 - RFC 1700 defines IPPORT_RESERVED (1024) for root level ports (not a firewall thing) and IPPORT_UNRESERVED (5000+) for unprivileged servers. I don't recall off hand which operating systems have restrictions in the <5000 range. – tdelaney May 24 '20 at 23:23
  • @tdelaney We'll ve never encountered such a one in 30 years, and that's across a lot of platforms. I routinely use ports in the low 1000s above 1024 and never a single problem. Let us know when you manage to 'recall' one. – user207421 May 25 '20 at 02:46

1 Answers1

0

I would gradually try to understand where the problem is coming from. Try to identify where the problem is:

  1. Write an example which is available online and will surely works, and then you will know that the problem is in your code.
  2. Try to use the same code on different computer, or a VM. If it will work there you will know that the problem is with the environment.
  3. Try to find people with similar problems, you will usually won't be the first. - Errno 10061 : No connection could be made because the target machine actively refused it ( client - server ) - this seems nice.
  4. Find out if your server is running correctly, checks if someone is listening on port 1234 before running the client. (Use netstat)

Few things which unrelated to the subject but will improve your coding: 1. Don't import *, it's just an easy way to get name collision. 2. Use conventions, it's just make everything nicer to read. https://www.python.org/dev/peps/pep-0008/

Of course you can ignore all of this, it's just an advice.

user6035109
  • 323
  • 2
  • 7