5

Do you know why this loop doesn't break?

#!/usr/bin/env python

from socket import *
import os
import sys

if __name__ == '__main__':
 HOST = '127.0.0.1'
 PORT = 55554

 print 'Creating socket'
 socketProxy = socket(AF_INET, SOCK_STREAM)

 print 'bind()'
 socketProxy.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
 socketProxy.bind((HOST, PORT))

 print 'Waiting for connection request'
 socketProxy.listen(1)
 conn, addr = socketProxy.accept()

 print 'Connected to ', addr

 request = ''

 while True:
    data = conn.recv(16);
    if not data: break
    request = request+data


print request
sys.stdout.flush()

I am writing a little Server-Proxy getting requests that can be arbitrary long so I must wait until I have received all the request. Anyway this loop (when len(data) == 0) doesn't stop and it keeps on waiting. How Can I stop it? Thanks

6
  • 3
    The client doesn't shutdown/close the socket, maybe? Commented Feb 13, 2013 at 13:12
  • 1
    Are you sure it doesn't simply hangs at conn.recv(16) because socket does not recieve any data? Commented Feb 13, 2013 at 13:12
  • Yes, the client doesn't close the socket because then it waits for the answer... Commented Feb 13, 2013 at 13:17
  • 2
    Remember that sockets are blocking by default, so if there is nothing to receive, then recv will block and wait. Commented Feb 13, 2013 at 13:39
  • I know...the problem is that client and server are given by our teacher, then we must write a proxy. when the client sends the request (an http similar request), it doesn't close the socket...and waits for answers. the proxy must get the html page from the server and send it to the client. So, to get the request from the client without blocking, Shall I perform a recv(n) and decide for a n quite big? Commented Feb 13, 2013 at 13:50

2 Answers 2

3

One solution is to make the socket non-blocking. Then you receive in a loop until no more data is received. If no data has been received (i.e. request is empty) then the connection has been closed, otherwise you have your request.

When you have your request, send it on to the actual server, and do the same as above when waiting for the reply. Send the reply on to the client.

Sign up to request clarification or add additional context in comments.

3 Comments

Shall I use MSG_DONTWAIT to make the socket not blocking?
@user1576208 Or use the socket.setblocking method.
The client can take some time to send any data to the socket, which means the connection isn't closed but no data is received at the time recv() is called. You should make sure the protocol have some way of detecting via a message whenever the client is terminating connection.
1

You can try setting a timeout, see Python socket recv data in while loop not stopping

Comments

Your Answer

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.