commit de5e3a31d7d3f9057c0025d3222a89683bfdf4da Author: vicsash Date: Fri Jan 19 17:44:32 2024 +0100 Ejercicio TCP diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/TCP.iml b/.idea/TCP.iml new file mode 100644 index 0000000..7307346 --- /dev/null +++ b/.idea/TCP.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..bf2e8ba --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..8a4eed5 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/cliente.py b/cliente.py new file mode 100644 index 0000000..c004a35 --- /dev/null +++ b/cliente.py @@ -0,0 +1,20 @@ +import socket + +HOST = '127.0.0.1' +PORT = 2000 + +with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.connect((HOST, PORT)) + print('Conectado con éxito') + + while True: + message = input("Enter a message (or 'exit' to quit): ") + if message.lower() == 'exit': + break + + s.sendall(message.encode('utf-8')) + num_bytes_sent = len(message.encode('utf-8')) + print(f"Sent {num_bytes_sent} bytes") + + data = s.recv(1024) # línea bloqueante + print('Recibido:', repr(data.decode('utf-8'))) \ No newline at end of file diff --git a/servidor.py b/servidor.py new file mode 100644 index 0000000..4c239cc --- /dev/null +++ b/servidor.py @@ -0,0 +1,15 @@ +import socket +HOST = '' # todas las interfaces locales a la escucha +PORT = 2000 # Puerto de escucha +with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.bind((HOST, PORT)) + s.listen() + conn, addr = s.accept() #línea bloqueante + with conn: + print(f"Conexión exitosa con el cliente. IP ({addr[0]}) Puerto ({addr[1]})") + while True: + data = conn.recv(1024) #línea bloqueante + print (data) + if data==b"0": + break + conn.sendall(b"mensaje recibido") \ No newline at end of file