0

When the disconnect message is received from my client(A django site sending the data), my socket isn't closing and this is preventing the rest of the code in my python file from running. If anybody knows what is causing this, help would be great. My Code:

import threading
import queue
import socket

HEADER = 64
PORT = 6060
SERVER = '0.0.0.0'
ADDR = (SERVER, PORT)
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!DISCONNECT!"
my_queue = queue.Queue()
print(SERVER)
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)


def handle_client(conn, addr):
    print(f"[NEW CONNECTION] {addr} connected")

    connected = True
    while connected:
        msg_length = conn.recv(HEADER).decode(FORMAT)
        if msg_length:
            msg_length = int(msg_length)
            msg = conn.recv(msg_length).decode(FORMAT)
            if msg == DISCONNECT_MESSAGE:
                connected = False
            else:
                my_queue.put(msg)
            print(f"[{ADDR}] {msg}")
            conn.send("MSG Received".encode(FORMAT))
    conn.close()
def start():
    server.listen()
    print(f"[LISTENING] Server Is Listening On {SERVER}")
    while True:
        conn, addr = server.accept()
        thread = threading.Thread(target=handle_client, args=(conn, addr))
        thread.start()
        print(f"[ACTIVE CONNECTIONS] {threading.activeCount() - 1}")

print("[Starting] Server")
start()

msg = my_queue.get()
print(msg)
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
Garberchov
  • 67
  • 1
  • 15
  • https://stackoverflow.com/a/43420503/238704 – President James K. Polk Sep 20 '21 at 00:27
  • 1
    In addition to reading the link posted, have you tried printing `msg` or stepping through the code in a debugger to view the `msg` and see why it doesn't match? TCP isn't message-based, so you can receive less that you expect and this code doesn't account for that. – Mark Tolonen Sep 20 '21 at 00:39
  • @PresidentJamesK.Polk, I have it set so that when connected is not true it stops, however it getting the exact disconnect message that I am sending it and is still not disconnecting as Mark was concerned about so I am unsure why it isn't breaking – Garberchov Sep 20 '21 at 00:58
  • @MarkTolonen I am printing out the messages that I receive and I receive the exact same disconnect message as should be coming in but it isn't breaking, so I am very confused. – Garberchov Sep 20 '21 at 00:59
  • 1
    What behavior is incorrect? I copied your server code and sent a disconnect message and it worked. No message is printed in your current code but when I connect a second client it showed only 1 active connection so it did close. You don't really need to send a disconnect message, though, just close the client side socket and the `recv` on the server size will return 0 bytes indicating the client is done and you can close the server that way. Keep in mind, though, that you should verify you've received a complete message. `recv(n)` can return any number from 0-n bytes. – Mark Tolonen Sep 20 '21 at 02:39
  • first you could use `print()` to see if line `connected = False` is really executed. ie. `print( msg == DISCONNECT_MESSAGE )`. Maybe it sends little different message than you expect - ie. with '\n' at the end or with some spaces - and printing `msg` it may look like you get correct message but `msg == DISCONNECT_MESSAGE:` may gives false. – furas Sep 20 '21 at 03:31
  • @furas I have got it printing both the disconnect message itself and `print(msg == DISCONNECT_MESSAGE` and it prints true. It is just not going on to the rest of the code in the rest of the file? – Garberchov Sep 20 '21 at 10:20
  • @MarkTolonen the behavior that is wrong is that the rest of the code is not running in the file, I am printing `print(msg==DISCONNECT_MESSAGE)` and it is coming out True but the rest of the code in the file is not running – Garberchov Sep 20 '21 at 10:22
  • I used `socket` to create `client` to test code and it works correctly for me. When client sends `!DISCONNECT!` then server closes connection and I can run another client. I can't reproduce your problem. – furas Sep 20 '21 at 12:28

0 Answers0