From 61439324b734aee09f82ac109bd16ee1b21af8c7 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Fri, 24 Mar 2023 18:46:20 +0000 Subject: [PATCH] [LABI] tema03 src added --- 1ano/2semestre/labi/tema03/src/tcp_client.py | 19 ++++++++++++ 1ano/2semestre/labi/tema03/src/tcp_server.py | 22 +++++++++++++ .../labi/tema03/src/udp_chat_client.py | 25 +++++++++++++++ .../labi/tema03/src/udp_chat_server.py | 31 +++++++++++++++++++ 1ano/2semestre/labi/tema03/src/udp_client.py | 23 ++++++++++++++ 1ano/2semestre/labi/tema03/src/udp_server.py | 23 ++++++++++++++ 6 files changed, 143 insertions(+) create mode 100644 1ano/2semestre/labi/tema03/src/tcp_client.py create mode 100644 1ano/2semestre/labi/tema03/src/tcp_server.py create mode 100644 1ano/2semestre/labi/tema03/src/udp_chat_client.py create mode 100644 1ano/2semestre/labi/tema03/src/udp_chat_server.py create mode 100644 1ano/2semestre/labi/tema03/src/udp_client.py create mode 100644 1ano/2semestre/labi/tema03/src/udp_server.py diff --git a/1ano/2semestre/labi/tema03/src/tcp_client.py b/1ano/2semestre/labi/tema03/src/tcp_client.py new file mode 100644 index 0000000..0df491d --- /dev/null +++ b/1ano/2semestre/labi/tema03/src/tcp_client.py @@ -0,0 +1,19 @@ +import socket + + +def main(args=None): + tcp_s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + tcp_s.connect(('localhost', 1234)) + + while True: + str_data = input("$ ") + if not str_data or str_data == "stop": + break + tcp_s.send(str_data.encode("utf-8")) + + b_data = tcp_s.recv(4096) + print("-> " + b_data.decode("utf-8")) + + +if __name__ == '__main__': + main() diff --git a/1ano/2semestre/labi/tema03/src/tcp_server.py b/1ano/2semestre/labi/tema03/src/tcp_server.py new file mode 100644 index 0000000..1f0c722 --- /dev/null +++ b/1ano/2semestre/labi/tema03/src/tcp_server.py @@ -0,0 +1,22 @@ +import socket + + +def main(args=None): + tcp_s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + tcp_s.bind(('', 1234)) + + tcp_s.listen(1) + client_s, client_addr = tcp_s.accept() + + while True: + b_data = client_s.recv(4096) + if not b_data or b_data == "stop": + break + client_s.send(b_data.upper()) + + client_s.close() + tcp_s.close() + + +if __name__ == '__main__': + main() diff --git a/1ano/2semestre/labi/tema03/src/udp_chat_client.py b/1ano/2semestre/labi/tema03/src/udp_chat_client.py new file mode 100644 index 0000000..b3bcd63 --- /dev/null +++ b/1ano/2semestre/labi/tema03/src/udp_chat_client.py @@ -0,0 +1,25 @@ +import select +import socket +import sys + + +def main(): + udp_s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + udp_s.bind(("127.0.0.1", 0)) + server_addr = ("127.0.0.1", 1234) + + while True: + rsocks = select.select([udp_s, sys.stdin], [], [])[0] + + for sock in rsocks: + if sock == udp_s: + b_data, addr = udp_s.recvfrom(4096) + sys.stdout.write(f"-> {b_data.decode('utf-8')}") + elif sock == sys.stdin: + print('<- ', end='') + msg = sys.stdin.readline() + udp_s.sendto(msg.encode("utf-8"), server_addr) + + +if __name__ == '__main__': + main() diff --git a/1ano/2semestre/labi/tema03/src/udp_chat_server.py b/1ano/2semestre/labi/tema03/src/udp_chat_server.py new file mode 100644 index 0000000..3b4f46b --- /dev/null +++ b/1ano/2semestre/labi/tema03/src/udp_chat_server.py @@ -0,0 +1,31 @@ +import socket + + +def main(): + udp_c = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + udp_c.bind(("127.0.0.1", 1234)) + + addr_list = [] + + while True: + b_data, addr = udp_c.recvfrom(4096) + print(f"Received from {addr}: {b_data.decode('utf-8')}") + + if addr not in addr_list: + addr_list.append(addr) + + if b_data.decode("utf-8") == "stop": + print("Stopping server...") + break + + for laddr in addr_list: + if laddr == addr: + continue + print(f"Sending to {laddr}: {b_data}") + udp_c.sendto(b_data, laddr) + + udp_c.close() + + +if __name__ == "__main__": + main() diff --git a/1ano/2semestre/labi/tema03/src/udp_client.py b/1ano/2semestre/labi/tema03/src/udp_client.py new file mode 100644 index 0000000..7e6c555 --- /dev/null +++ b/1ano/2semestre/labi/tema03/src/udp_client.py @@ -0,0 +1,23 @@ +import socket + + +def main(): + udp_s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + udp_s.bind(("127.0.0.1", 0)) + server_addr = ("127.0.0.1", 1234) + + while True: + msg = input(f"$ ") + udp_s.sendto(msg.encode("utf-8"), server_addr) + + if msg == "stop": + break + + b_data, addr = udp_s.recvfrom(4096) + print(f"-> {b_data.decode('utf-8')}") + + udp_s.close() + + +if __name__ == '__main__': + main() diff --git a/1ano/2semestre/labi/tema03/src/udp_server.py b/1ano/2semestre/labi/tema03/src/udp_server.py new file mode 100644 index 0000000..443af6f --- /dev/null +++ b/1ano/2semestre/labi/tema03/src/udp_server.py @@ -0,0 +1,23 @@ +import socket + + +def main(): + udp_c = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + udp_c.bind(("127.0.0.1", 1234)) + + while True: + b_data, addr = udp_c.recvfrom(4096) + print(f"Received from {addr}: {b_data.decode('utf-8')}") + + if b_data.decode("utf-8") == "stop": + print("Stopping server...") + break + + print(f"Sending to {addr}: {b_data.upper()}") + udp_c.sendto(b_data.upper(), addr) + + udp_c.close() + + +if __name__ == "__main__": + main()