I have posted this question elsewhere and gotten zero helpful responses.

Specifically on an Apple, with Python 3.9.10, I am using the socket sendall method to send a data buffer on a network connection that has successfully opened to a server. The data that I am sending does not reach the server until I use the socket's close method when I give up on waiting for data to be returned.

The intent is for the Python script to act as a network client to contact a server and do some minimal data transactions. The server is running as a service on a Linux machine. The client-side script spawns a thread to do the network communication with

import threading, sys

t = threading.Thread(target=social_init, daemon=True)
t.start()

The client-side spawned code in question (in part) is:

import socket               # Import socket module and JSON parser module
import json
def social_init():
    global social1_resps, social2_resps, social3_resps
    host = "test.mydomain.com"  # Get server name
    port = 6942                 # this is where the server is listening
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.settimeout(30.0)    # note: 30 second timeout
        s.connect((host, port))
        sbuf = json.dumps({"rmv":"1","gameID":9,"function":1})
        bbuf = sbuf.encode('utf-8')
        nbytes = len(bbuf)
        s.sendall(nbytes.to_bytes(4, 'little'))
        s.sendall(bbuf)

        nnbuf = s.recv(4)
        nbytes = int.from_bytes(nnbuf, 'little')
        bbuf = s.recv(nbytes)
        sbuf = str(bbuf, 'utf-8')
        obj = json.loads(sbuf)
        while (obj != None):
            # actual processing of returned data elided

        s.close()                   # Close the socket when done

This code works as expected on Windows and Linux hosts, but on an Apple host the server sees the connection happen, but no data. When the client times out the receive, or when the script terminates (which forces the connection closed), we see the data being sent - I've checked timing by running Wireshark on the server.

I'm aware of the existence of the flush() function, but because we're using sockets rather than the file handle abstraction, flush apparently does not apply. I have tried the file handle abstraction (socket.makefile) with no better success - in that case flush apparently flushes the file object buffers to the network subsystem, but it still does not hit the wire. Is there an equivalent method that can be used on a socket to force flushing of the network buffer?

How long have you been asking this question? I see priors at over 5 months and the second question I have for you is what do you consider helpful?

While I'd rewrite this another way I won't rewrite it for you. You are the author and must support your app for as long as you see fit. I see nothing wrong with the usual open socket, send data, close socket method we've used for decades. But then some want magic().

I asked it once on one other forum, five months ago. Yes, this is a very low priority issue; if I can't get it working, I'll simply leave it as is and Apple users will be SOL. And what I would consider helpful is specifically what I'm asking: how do I get the data to actually leave the Apple machine without closing the connection?

commented: You may have to restate the question. +17

We ran into this on other platforms and worked around it. Sorry, no magic() call I know of, just had to rewrite.

TCP turned out to be the incorrect choice for our needs but hey you can google "tcp force send" to see if any of those ideas work.
Again, I don't know of any magic() here. Only that we had to rewrite.

Read https://stackoverflow.com/questions/31826762/python-socket-send-immediately to see how they used no_delay magic().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.