I have a simple server-client setup where I want to transfer files back and forth. I have no issue when I use the following code to send a file from the client to the server. My problem is the code hangs without any errors when I try to then subsequently send a file back from the server to the client. Note that here I am trying to send a special type of file (hdf5).
The following is the server code:
import socket
import h5py
import io
HOST = 'localhost'
PORT = 3512 # Port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen()
conn, address = s.accept()
#Receive the HDF5 file in chunks
chunks = []
while True:
chunk = conn.recv(1024)
if not chunk:
break
chunks.append(chunk)
#Read the hdf5 file
with h5py.File(io.BytesIO(b''.join(chunks)), 'r') as f:
print(f.keys())
#If we stop up to here it works.
#But when I add the following, we never get past the conn.recv() part.
img = f.id.get_file_image()
conn.send(img)
Client side:
import socket
import h5py
import io
HOST = 'localhost'
PORT = 3512
sc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sc.connect((HOST, PORT)) # Connect to the port and host
#create hdf5 file with some dummy content
h5_f = h5py.File("test_file.h5", 'a')
grp = h5_f.create_group("Something")
grp.create_dataset("test", shape = (50,50))
h5_f.flush()
#Send the file
img = h5_f.id.get_file_image()
sc.send(img) # Send file image in chunks
#Receive a hdf5 file
chunks = []
while True:
chunk = sc.recv(1024)
if not chunk:
break
chunks.append(chunk)
# Load the HDF5 file from the received chunks
with h5py.File(io.BytesIO(b''.join(chunks)), 'r') as f:
print(f.keys())
Running this code, it simply hangs. Why is receiving data on the client side an issue here?
I expected the client to receive the data, but it did not. It hangs without any error messages.