audio over python tcp error
Question: I am writing a simple python tcp code to send over a wav file however I seem
to be getting stuck. can someone explain why my code is not working correctly?

Server Code

    
    
    import socket, time
    import scipy.io.wavfile
    import numpy as np
    
    def Main():
    host = ''
    port = 3333
    MAX = 65535
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((host,port))
    
    s.listen(1)
    print "Listening on port..." + str(port)
    c, addr = s.accept()
    print "Connection from: " + str(addr)
    wavFile = np.array([],dtype='int16')
    i = 0
    while True:
        data = c.recvfrom(MAX)
        if not data:
            break
        # print ++i
        # wavfile = np.append(wavfile,data)
        print data
    timestr = time.strftime("%y%m%d-%h%m%s")
    print timestr
    # wavF = open(timestr + ".wav", "rw+")
    scipy.io.wavfile.write(timestr + ".wav",44100, data)
    c.close()
    
    if __name__ == '__main__':
        Main()
    

Client Code

    
    
    host, port = "", 3333
    
    import sys , socket
    import scipy.io.wavfile
    
    # create a tcp/ip socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    
    # connect the socket to the port where the server is listening
    server_address = (host, port)
    print >>sys.stderr, 'connecting to %s port %s' % server_address
    
    input_data = scipy.io.wavfile.read('Voice 005.wav',)
    audio = input_data[1]
    sock.connect(server_address)
    print 'have connected'
    try:
        # send data
       sock.sendall(audio)
       print  "sent" + str(audio)
       sock.close()
    except:
        print('something failed sending data')
    finally:
        print >>sys.stderr, 'closing socket'
        print "done sending"
        sock.close()
    

Please help someone, I want to send an audio file to my embedded device with
tcp since it crucial data to be processed on the embedded device.


Answer: Not sure why you go to the trouble of using `scipy` and `numpy` for this,
since you can just use the `array` module to create binary arrays that will
hold the wave file. Can you adapt and use the simple client/server example
below?

(Note: I've copy/pasted a small Windows sound file called 'tada.wav' to the
same folder to use with the test scripts.)

Code for the server script:

    
    
    import socket
    
    HOST = ''                 # Symbolic name meaning all available interfaces
    PORT = 50007              # Arbitrary non-privileged port
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((HOST, PORT))
    s.listen(1)
    print('Listening...')
    conn, addr = s.accept()
    print('Connected by', addr)
    outfile = open("newfile.wav", 'ab')
    while True:
        data = conn.recv(1024)
        if not data: break
        outfile.write(data)
    conn.close()
    outfile.close()
    print ("Completed.")
    

Code for the client:

    
    
    from array import array
    from os import stat
    import socket
    
    arr = array('B') # create binary array to hold the wave file
    result = stat("tada.wav")  # sample file is in the same folder
    f = open("tada.wav", 'rb')
    arr.fromfile(f, result.st_size) # using file size as the array length
    print("Length of data: " + str(len(arr)))
    
    HOST = 'localhost'
    PORT = 50007
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, PORT))
    s.send(arr)
    print('Finished sending...')
    s.close()
    print('done.')
    

This works for me (though only tested by running both on localhost) and I end
up with a second wave file that's an exact copy of the one sent by the client
through the socket.

