3

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?

martineau
124k29 gold badges181 silver badges319 bronze badges
asked Jun 3, 2016 at 23:03

1 Answer 1

3

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).

answered Jun 3, 2016 at 23:05
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.