5

i'm trying make client stay active and wait for a server connection. For the moment if I open the client before the server, it fails because client doesn't find server connection and stops running. I have seen similar topics, but they where for c# and Java. I'm a newbie, so help me guys :)

client code:

#!/usr/bin/env python
import pyaudio
import socket
import sys
import time


# Pyaudio Initialization
chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 10240

p = pyaudio.PyAudio()

stream = p.open(format = FORMAT,
                channels = CHANNELS,
                rate = RATE,
                input = True,
                frames_per_buffer = chunk)

# Socket Initialization
host = ''
port = 50000
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host,port))

# Main Functionality
while 1:
    data = stream.read(chunk)
    s.send(data)
    s.recv(size)

Server code:

#!/usr/bin/env python
import pyaudio
import socket, ssl
import sys

# Pyaudio Initialization
chunk = 1024
p = pyaudio.PyAudio()

stream = p.open(format = pyaudio.paInt16,
                channels = 1,
                rate = 10240,
                output = True)

# Socket Initialization
host = ''
port = 50000
backlog = 5
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host,port))
s.listen(backlog)

client, address = s.accept()

# Main Functionality
while 1:
    data = client.recv(size)
    if data:
        # Write data to pyaudio stream
        stream.write(data)  # Stream the recieved audio data
        client.send('ACK')  # Send an ACK

client.close()
stream.close()
p.terminate()

And one more question, what kind of protocol-python extensions should I use to run the same but with remote connection instead of local)? Manny thanks :)

1 Answer 1

9

You can try something like this:

#!/usr/bin/env python
import pyaudio
import socket
import sys
import time


# Pyaudio Initialization
chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 10240

p = pyaudio.PyAudio()

stream = p.open(format = FORMAT,
                channels = CHANNELS,
                rate = RATE,
                input = True,
                frames_per_buffer = chunk)

# Socket Initialization
host = ''
port = 50000
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connected = False
while not connected:
    try:
        s.connect((host,port))
        connected = True
    except Exception as e:
        pass #Do nothing, just try again

# Main Functionality
while 1:
    data = stream.read(chunk)
    s.send(data)
    s.recv(size)

As for your second question: This should work on remote connections as well, since you bind to all interfaces with host='' Just make sure you portforward your router (only server needs to be port forwarded)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.