Skip to content

Commit

Permalink
final
Browse files Browse the repository at this point in the history
  • Loading branch information
shivankgarg98 committed Nov 15, 2018
1 parent e129706 commit 778b75a
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 16 deletions.
Binary file added CN-08.tar.xz
Binary file not shown.
8 changes: 8 additions & 0 deletions CN-08/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#paint
## Dependencies

## Running the application
1. Run the server by `python3 server.py IP-ADDR PORT`
2. Run the voice server by `python3 voice_ser.py IP-ADDR PORT1`
3. Run the paint application by `python3 paint.py IP-ADDR PORT`
4. Run the Voice application by `python3 voice_cli.py IP-ADDR PORT1`
File renamed without changes.
74 changes: 74 additions & 0 deletions CN-08/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@

# Python program to implement server side of chat room.
import socket
import select
import sys
from _thread import *
import json

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

if len(sys.argv) != 3:
print("Correct usage: script, IP address, port number")
exit()

IP_address = str(sys.argv[1])
Port = int(sys.argv[2])
server.bind((IP_address, Port))
server.listen(100)

list_of_clients = []

def clientthread(conn, addr):
mes= {'type':'message',
'message':'Welcome',
'data':'',
'addr':''}
conn.send(bytes('|'+json.dumps(mes),'utf8'))
while True:
try:
message = conn.recv(2048)
if message:
msg=message.decode()
print(msg)
msg=json.loads(msg)
mes=json.dumps({"type":"draw", "addr":addr[0],"data":msg})
mes='|'+mes
broadcast(mes, conn)
else:
remove(conn)

except:
continue

def broadcast(message, connection):
for clients in list_of_clients:
if clients!=connection:
try:
clients.send(bytes(message,'utf8') )
except:
clients.close()

# if the link is broken, we remove the client
remove(clients)

def remove(connection):
if connection in list_of_clients:
list_of_clients.remove(connection)

while True:
conn, addr = server.accept()

"""Maintains a list of clients for ease of broadcasting
a message to all available people in the chatroom"""
list_of_clients.append(conn)
# prints the address of the user that just connected
print(addr[0] + " connected")

# creates and individual thread for every user
# that connects
start_new_thread(clientthread,(conn,addr))

conn.close()
server.close()
53 changes: 53 additions & 0 deletions CN-08/voice_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import socket
import sys
import time
import select
import pyaudio
from _thread import *

CHUNK=1024
FORMAT=pyaudio.paInt16
CHANNELS=1
RATE=20000

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if len(sys.argv) != 3:
print("Correct usage: script, IP address, port number")
exit()
IP_address = str(sys.argv[1])
Port = int(sys.argv[2])
server.connect((IP_address, Port))
sockets_list = [sys.stdin, server]

read_sockets,write_socket, error_socket = select.select(sockets_list,[],[])
server.settimeout(0.035)

p=pyaudio.PyAudio()
recv_stream=p.open(format=FORMAT,channels=CHANNELS,rate=RATE,output=True,frames_per_buffer=CHUNK)
send_stream=p.open(format=FORMAT,channels=CHANNELS,rate=RATE,input=True,frames_per_buffer=CHUNK)

def recv_data():
while True:
try:
for socks in read_sockets:
if socks==server:
data=socks.recv(1024)
recv_stream.write(data)
else:
pass
except:
continue

def send_data():
while True:
try:
data = send_stream.read(CHUNK)
server.send(data)
except:
pass

start_new_thread(recv_data, ())
start_new_thread(send_data, ())

while True:
pass
66 changes: 66 additions & 0 deletions CN-08/voice_ser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import socket
import select
import sys
from _thread import *

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

if len(sys.argv) != 3:
print("Correct usage: script, IP address, port number")
exit()

IP_address = str(sys.argv[1])
Port = int(sys.argv[2])
server.bind((IP_address, Port))
server.listen(100)

list_of_clients = []

def clientthread(conn, addr):
while True:
try:
message = conn.recv(2048)
if message:
broadcast(message,conn)
else:
remove(conn)

except:
continue



def broadcast(message, connection):
for clients in list_of_clients:
if clients!=connection:
try:
clients.send(message)
except:
clients.close()

# if the link is broken, we remove the client
remove(clients)



def remove(connection):
if connection in list_of_clients:
list_of_clients.remove(connection)


while True:
conn, addr = server.accept()

"""Maintains a list of clients for ease of broadcasting
a message to all available people in the chatroom"""
list_of_clients.append(conn)
# prints the address of the user that just connected
print(addr[0] + " connected")

# creates and individual thread for every user
# that connects
start_new_thread(clientthread,(conn,addr))

conn.close()
server.close()
16 changes: 0 additions & 16 deletions README.md

This file was deleted.

0 comments on commit 778b75a

Please sign in to comment.