Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make client socket wait for server socket with Python

Tags:

python

sockets

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 :)

like image 870
Val Avatar asked Jan 09 '23 06:01

Val


1 Answers

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)

like image 75
Yurippenet Avatar answered Jan 15 '23 13:01

Yurippenet