Old-fashioned-dev
Old-fashioned-dev

Reputation: 425

socket recv not returning

I can't understand why socket recv doesn't return. I have a client application that send string to a server. The client is executed in a terminal. This is the code

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

print "Enter client sender"
try:
    client.connect((target, port))
    if len(buffer):
        client.send(buffer)

    while True:
        print "Enter loop"
        recv_len = 1
        response = ""

        while recv_len:
            print "Enter loop 2"
            data = client.recv(4096)
            print "Afer recv"
            recv_len = len(data)
            print recv_len
            response += data

            if recv_len < 4096:
                break

        print response,

        buffer = raw_input("")
        buffer += "\n"

        client.send(buffer)

except:
    print "[*] Exception Exiting"
    client.close()

But the "After recv" label is never reached. Why? Can you help me? What is wrong?

Upvotes: 3

Views: 6297

Answers (1)

szym
szym

Reputation: 5846

Your code is generally correct, although bufferis a built-in type and you should avoid using it as a variable name (but it will still work if you do).

On a blocking socket (the default state), recv will block until there is some data available for reading. Make sure the other endpoint is sending (and flushing to the socket) a response.

You might find tcpdump or Wireshark useful to see the packets actually being transmitted. Also, I suggest you use nc or a similar generic TCP socket tool to debug this. E.g., nc -4 -l localhost PORT (-4 because your AF_INET implies IPv4).

Upvotes: 3

Related Questions