Michael L
Michael L

Reputation: 303

Having a disconnect button and a connect button?

I have a disconnect button and a connect button on my client. When I click the disconnect button and then the connect button I get this: OSError: [WinError 10038] An operation was attempted on something that is not a socket

The disconnect button is coded like this:

def Disconnect():
    s.shutdown(socket.SHUT_RDWR)
    s.close()

And the connect button is this:

def Join1():
    print("CONNECTING TO: " + host + "...")
    try:
        s.connect((host, port))
        print("CONNECTING TO: " + host + " ESTABLISHED!")

        statusbar_status = "Connected"
        startrecv = Thread(target=returnrecv)
        startrecv.start()

Why can't I connect again after I click the disconnect button? Is it impossible to reopen the socket? I've been stuck on this problem for like a month now and I can't understand why..

Upvotes: 2

Views: 393

Answers (1)

Stevo Mitric
Stevo Mitric

Reputation: 1619

After closing a socket, you cannot reuse it to share other data between Server and Client. From Python Docs, about close() method:

Close the socket. All future operations on the socket object will fail. The remote end will receive no more data (after queued data is flushed). Sockets are automatically closed when they are garbage-collected.

So you would need to create a new socket object every time you try to connect (in your join1() function which would look something like this:

def Join1():
    global s     # i would recommend using classes instead
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)      # your socket object
    print("CONNECTING TO: " + host + "...")
    try:
        s.connect((host, port))
        ...

Upvotes: 2

Related Questions