0

UPDATE: I found the source of the program hanging. In my pdthread it calls mysocket1.sendall(mydata). It doesn't seem to be able to get past this. Does anyone know why this might be an issue?

I have the following code:

mysocket1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysocket1.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
mysocket1.connect(('127.0.0.1', 10001))

while mysocket1:
    try:
        msg = mysocket1.recv(4092)
        msg = pickle.loads(msg)
        print(msg)
        for pd in pd_objects:
            if msg[0] == pd.cam_ip and str(msg[1]) == 'ON' and pd.pd_active == False:
                pd.pd_active = True
                pdthread = Thread(target=get_pd_data(pd.cam_ip))    
                pdthread.daemon = True
                pdthread.start()
                print("testing1")
            elif msg[0] == pd.cam_ip and msg[1] == 'OFF':
                pd.pd_active = False
                print("testing else")
            print("testing2")
    except:
        traceback.print_exc(file=sys.stdout)
        break
    print("reached end")

I have another python program connected on the other end. This connects and runs perfectly the first time I press a button to activate it. I have my data sending over in the pdthread and all works swell. It's what happens afterwards that's the problem. Future button presses are NOT picked up by this side. The data is being sent over the socket just fine from the other end, it's just not being received here.

I've put in some prints for testing and I found a few interesting things that I cannot explain which is why I'm asking here:

The print("testing1") is never printed. Ever. Not even after the first successful click. Which makes me think that pdthread.start() is behaving like pdthread.join()... which confuses me.

The print("testing else") is never printed. This is expected given my testing but I wanted to rule it out.

Here's the weirdest one. I have two items in my pd_objects list. When I click the button that sends the pd.cam_ip of the first item, print("testing2") does not print, but it DOES print if I click the one for the second item in the list.

Can anyone help explain this bizarre behaviour?

EDIT: the final print is also never printed

Cm1602
  • 91
  • 11
  • https://stackoverflow.com/a/43420503/238704 – President James K. Polk Dec 03 '20 at 15:08
  • Does this answer your question? [Python socket not receiving without sending](https://stackoverflow.com/questions/43420075/python-socket-not-receiving-without-sending) – President James K. Polk Dec 03 '20 at 15:12
  • @PresidentJamesK.Polk I think it might be. But I'm fairly new to socket programming and this is all very confusing. Could you help me understand how I would integrate this into my code? Would I need to shut down the socket after every read? – Cm1602 Dec 03 '20 at 16:29
  • No, you don't shutdown after every read. That's not the gist of the accepted answer. – President James K. Polk Dec 03 '20 at 18:03
  • @PresidentJamesK.Polk Then I'm not sure I understand the gist of the accepted answer. Could you help explain that to me? – Cm1602 Dec 04 '20 at 08:45
  • 1
    Take a step back and learn how sockets work. Get a simple client/server working that sends messages. `recv` is NOT guaranteed to have a complete valid message in it. TCP is a byte stream and `recv(4092)` will receive 1-4092 bytes or 0 if the socket is closed. You must check the return value of `recv` and make sure you have a complete message. Send the message size as part of the byte stream. – Mark Tolonen Dec 05 '20 at 00:27
  • Here's an answer for exchanging files over a socket to study https://stackoverflow.com/a/53145147/235698 – Mark Tolonen Dec 05 '20 at 00:32

0 Answers0