11

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))
Ulgen
  • 126
  • 1
  • 2
  • 6
Stan Theo Hettinga
  • 119
  • 1
  • 1
  • 3

7 Answers7

13

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.

Dan Kowalczyk
  • 4,103
  • 2
  • 18
  • 29
5

'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.

furas
  • 134,197
  • 12
  • 106
  • 148
1

You need simply to start server at first, and then run the client_code. In VS Code i've opened 2 terminals. One for the server_code to be running While True, and the other one for the client_code

0

So this may not fix your question specifically but it fixed mine and it can help someone else I work with vscode and I use some extension that runs my code so when you want to run your server run it on your CMD or Terminal and run your client in vscode it helped me (maybe importat I work on mac so maybe spesific OS problem)

xfajk X
  • 151
  • 1
  • 1
  • 7
0

If you are connecting to a host:port that is open but there is no service bound to it you may see this IIRC. Eg with ssh you sometimes see this while attempting to connect to a server that is booting but sshd is not running.

psorenson
  • 259
  • 2
  • 5
0

this happened to me too. try checking your Ip address in your settings and change your ip address in your code.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 21 '23 at 11:59
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/34247003) – valentinmk Apr 25 '23 at 14:11
-3

This Code Not Valid For Chatting, you have to use unblocking sockets and select module or other async modules

Mohannad_
  • 63
  • 5