0

I have found one benign yet persistent problem with my socket client, it seems to hang and never receive a response from the server. Put differently, there is an indefinite wait for any response.

This only happens for cases where the server socket, has run say a minute or so method/function whose result is supposed to be returned to the client.

Put simply if you try to send plain text back and forth, this works fine, however making calls to a function that may need the client end to wait bit for a response cause it to hang.

Any help will be appreciated. Below is some code...thanks to @Robᵩ for providing an earlier solution that worked with text like input and response

client

import socket
s = socket.socket()
s.connect(("localhost",3000))
f=open ("tranmit.jpg", "rb")
l = f.read(1024)
while (l):
    s.send(l)
    l = f.read(1024)
s.shutdown(socket.SHUT_WR)
reply =s.recv(1024) # Feedback: would like the receive feedback from the server.
print reply
s.close()

server

import socket
import sys
s = socket.socket()
s.bind(("localhost",3000))
s.listen(10)
i=1
# def somefunction(x):
#     ''' does some computationally semi intensive work, say last about 120s and returns a result for the client'''
while True:
    sc, address = s.accept()
    print address
    f = open('transmit.jpg','wb') #open in binary
    l = 1
    while(l):
        l = sc.recv(1024)
        while (l):
            f.write(l)
            l = sc.recv(1024)
        f.close()
        result =somefunction('transmit.jpg') # does something with the image from the client
        sc.send(str(result)) # Would like to send back a response

    sc.close()

s.close()
Community
  • 1
  • 1
Ezra
  • 3
  • 1
  • 2
  • I tried you code with `time.sleep(120)` in `somefunction` and it works without the issue you described. I tried to reproduce the issue under linux, if you are using windows, please specify that fact in question. I am not sure how windows sockets differ from unix, but even there I guess the connection could drop but not 'hang'. Do you have the issue you described with the fragments of the code you've provided? – Grief Mar 03 '16 at 14:02
  • 1
    @Grief- this is on a mac system- yosemite which should in every way work the same as the unit system you tested your code over. But for some reason, the client waits with no end for the response/rely from the server. when I interrupt the process, the system shows to be waiting indefinitely at this point -> reply =s.recv(1024) in the client program. – Ezra Mar 07 '16 at 07:02

0 Answers0