Skip to content
Advertisement

Python socket not delivering packet

I am creating a simple messaging server and client in Python using the socket library. First I am doing some validation and a key exchange before I let the user send messages. Every once and a while I get an issue where the server will send a message and the client won’t receive it and then the server will move on trying to receive a message, but the client is still blocking on the receive call too so they are in a deadlock. I am confused how this is happening because I thought the socket API ran TCP which should be guaranteed delivery.

This is part of the server side:

def serverSocket():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind(('0.0.0.0', 2048))
    s.listen()
    conn, addr = s.accept()
    print("Connected by {}".format(addr))
    with conn:
        ## Start Diffie-Hellman

        # generate a list of primes
        primes = primesSieve()

        # pick g
        g = pickPrime(500, primes)

        # send g
        printDebug("Sending g")
        conn.send(bytes(str(g), 'utf-8'))
        printDebug("Sent g")

        # pick p
        p = pickPrime(500, primes, g)

        # send p
        printDebug("Sending p")
        conn.send(bytes(str(p), 'utf-8'))
        printDebug("Sent p")

        # pick a
        a = random.randint(500, 2000)

        # calculate A
        A = (g**a)%p

        # send A
        printDebug("Sending A")
        conn.send(bytes(str(A), 'utf-8'))
        printDebug("Sent A")

        # receive B
        printDebug("Receiving B")
        data = conn.recv(1024)  #### This is where the server will stop
        printDebug("Received B")

        # convert B TODO error checking
        B = int(data.decode('utf-8'))

        # evaluate key
        key = (B**a)%p

This is part of the client side:

def clientSocket():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect(('0.0.0.0', 2048))

    ## Start Diffie-Hellman
    # receive g
    printDebug("Receiving g")
    g = s.recv(1024).decode('utf-8')
    printDebug("Received g")

    g = int(g)
    # receive p
    printDebug("Receiving p")
    p = s.recv(1024).decode('utf-8')
    printDebug("Received p")

    p = int(p)
    # receive A
    printDebug("Receiving A")
    data = s.recv(1024)  #### This is where the client will stop
    printDebug("Received A")

    # convert A TODO error checking
    A = int(data.decode('utf-8'))
    # pick b
    b = random.randint(500, 2000)
    printDebug(b)
    B = (g ** b) % p
    printDebug(B)
    # send B
    printDebug("Sending B")
    s.send(bytes(str(B), 'utf-8'))
    printDebug("Sent B")

    # evaluate key
    key = (A ** b) % p

Most of the time this works flawlessly. However sometimes when the client side is trying to receive A the server sends it then moves on to receiving B, but the client never receives A and doesn’t move on.

Thank you for any help. Also I know this really isn’t the best way to do Diffie Hellman and that the numbers are not large enough.

Advertisement

Answer

As @user207421 said in his comment I was already reading them because it was getting more than one message.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement