DEMKS
DEMKS

Reputation: 77

Python ConnectionRefusedError: [WinError 10061]

I am new at python and I want to make a server-client application but every time I am attempting a connection I see this error. I have tried changing sequency of send() and receive() functions but that didn't worked out

msg = client_socket.recv (BUFSIZ) .decode ("utf8")
OSError: [WinError 10038] An attempt was made to perform an operation on an object that is not a socket

Here is the code Client.py

    """Handles receiving of messages."""
    #client_socket.bind((HOST, PORT))
    #client_socket.connect((HOST, PORT))
    msg = client_socket.recv(BUFSIZ).decode("utf8")
    print(msg)
    client_socket.close()


def send(event=None):  # event is passed by binders.
    """Handles sending of messages."""
    client_socket.connect((HOST, PORT))
    msg = input("MSG: ")
    client_socket.send(bytes(msg, "utf8"))
    client_socket.close()

#----Now comes the sockets part----
HOST = '127.0.0.1'
PORT = 7557
if not PORT:
    PORT = 33000
else:
    PORT = int(PORT)

BUFSIZ = 1024
ADDR = (HOST, PORT)

client_socket = socket(AF_INET, SOCK_STREAM)
client_socket.connect(ADDR)

receive_thread = Thread(target=receive)
receive_thread.start()


if __name__ == '__main__':
    client_socket.close()
    receive()
    send()
    receive()

Ps: 99% of code is from internet

Upvotes: 0

Views: 1477

Answers (1)

moe asal
moe asal

Reputation: 582

Don't close your socket after receiving and sending information.

from socket import AF_INET, socket, SOCK_STREAM 
from threading import Thread
def receive() :
    """Handles receiving of messages."""
    while True :
        msg = client_socket.recv(BUFSIZ).decode("utf8")
        print(msg)


def send():
    """Handles sending of messages."""
    while True:
        msg = input("MSG: ")
        client_socket.send(bytes(msg, "utf8"))

#----Now comes the sockets part----
HOST = '127.0.0.1'
PORT = 7557
BUFSIZ = 1024
ADDR = (HOST, PORT)

client_socket = socket(AF_INET, SOCK_STREAM)
client_socket.connect(ADDR)

receive_thread = Thread(target=receive, args=() )
send_thread = Thread(target=send, args=()) 



if __name__ == '__main__':
    receive_thread.start()
    send_thread.start()

This is a modified version of your code.

I made a similar application by myself using the same logic, take a look:

https://github.com/moe-assal/Chatting_Server

Upvotes: 1

Related Questions