14892

Python ConnectionRefusedError: [Errno 61] Connection refused

<h3>Question</h3>

Ive seen similar questions but they I couldn't fix this error. Me and my friend are making a chat program but we keep getting the error ConnectionRefusedError: [Errno 61] Connection refused We are on different networks by the way. Here is my code for the server

import socket def socket_create(): try: global host global port global s host = '' port = 9999 s = socket.socket() except socket.error as msg: print("Socket creation error" + str(msg)) #Wait for client, Connect socket and port def socket_bind(): try: global host global port global s print("Binding socket to port: " + str(port)) s.bind((host, port)) s.listen(5) except socket.error as msg: print("Socket binding error" + str(msg) + "\n" + "Retrying...") socket_bind #Accept connections (Establishes connection with client) socket has to be listining def socket_accept(): conn, address = s.accept() print("Connection is established |" + " IP:" + str(address[0]) + "| port:" + str(address[1])) chat_send(conn) def chat_send(conn): while True: chat =input() if len(str.encode(chat)) > 0: conn.send(str.encode(chat)) client_response = str(conn.recv(1024), "utf-8") print(client_response) def main(): socket_create() socket_bind() socket_accept() main()

And my client code

import socket #connects to server s = socket.socket() host = '127.0.0.1' port = 9999 s.connect((host, port)) #gets chat while True: data = s.recv(1024) print (data[:].decode("utf-8")) chat = input() s.send(str.encode(chat))
<h3>Answer1:</h3>

'127.0.0.1' means local computer - so client connents with server on the same computer. Client have to use IP from server - like 192.168.0.1.

Check on server:

on Windows (in cmd.exe)

ipconfig

on Linux (in console)

ifconfig

But if you are in different networks then it may not work. ipconfig/ifconfig returns local IP (like 192.168.0.1) which is visible only in local network. Then you may need external IP and setting (redirections) on your and provider routers. External IP can be IP of your router or provider router. You can see your external IP when you visit pages like this http://httpbin.org/ip . But it can still need some work nad it be bigger problem.


<h3>Answer2:</h3>

This may not answer your original question, but I encountered this error and it was simply that I had not starting the server process first to listen to localhost (127.0.0.1) on the port I chose to test on. In order for the client to connect to localhost, a server must be listening on localhost.

来源:https://stackoverflow.com/questions/40814712/python-connectionrefusederror-errno-61-connection-refused

Recommend

  • Boolean to Visibility Converter in Win RT XAML not working first time
  • Django route all non-catched urls to included urls.py
  • protobuf with inheritance?
  • CKEditor 4 - how to add font family and font size controls to the toolbar
  • how to upload the image from iphone and using .NET webservices
  • Using @DECLARE in MYSQL 5.1
  • Google Apps Script: Sheets Forms Data Manipulation and Deleting Rows if Certain Cells are Blank
  • How to deploy a theme in liferay?
  • Share dialog: “Sorry, sharing is unavailable at this time. Please try again later.”
  • mailR Connection refused from linux but not from windows
  • Method resolution when using dynamic and handling of undefined method for specific derived class
  • Use tornado future to fetch url, two different ways get different results
  • Why node_modules is empty after docker build?
  • Boostrap 4 snackbar / toast [closed]
  • how to add part of Face A to Part of face B, most importantly matching color tones
  • How to encrypt and decrypt files with bouncy castle (DES) in netbeans java?
  • celery for different timezones
  • Using downloaded font offline [duplicate]
  • How to validate a JTextField of email ID with a regex in swing code?
  • Version in jar name
  • matching end of string
  • Replacing ui router 0.2 with 1.0.0
  • IOS - Facebook SDK fbDidLogin not called — initialize view controllers.
  • How to bend a cylinder in three.js?
  • How do I replace 'php3' with 'php' inside every file in a directory
  • NHibernate - Paging with ICriteria and optional ICriteria calls
  • How to upload an Image onto localhost server whilst path is stored in mysql database?
  • wxpython passing information, pointers?
  • How to read contents of a directory recursively in Linux Kernel?
  • How do I get the standard Vim FTP command to work?
  • Semi-local Levenshtein distance
  • Generate a runnable jar and include libraries in it with Maven
  • create circular Auto Horizontal Scroll View?
  • Getting/building the SQL (with parameters) from NHibernate 3.2
  • SQL query for game rental website - game delivery report
  • How to redirect into different page by user type in php and mysql
  • jQuery scrollTop if URL has hash