Skip to content

Commit

Permalink
Updated homework 6
Browse files Browse the repository at this point in the history
  • Loading branch information
RodolfoFerro committed Dec 15, 2017
1 parent 9f26c14 commit 3b141b6
Show file tree
Hide file tree
Showing 9 changed files with 393 additions and 134 deletions.
77 changes: 77 additions & 0 deletions Proyecto Final/Python/video_capture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# ===============================================================
# Author: Rodolfo Ferro Pérez
# Email: [email protected]
# Twitter: @FerroRodolfo
#
# Script: Video display script with two resolution sizes.
#
# ABOUT COPYING OR USING PARTIAL INFORMATION:
# This script was originally created by Rodolfo Ferro. Any
# explicit usage of this script or its contents is granted
# according to the license provided and its conditions.
# ===============================================================

"""
# ===============================================================
# Author: Rodolfo Ferro Pérez
# Email: [email protected]
# Twitter: @FerroRodolfo
#
# Script: Video display script with two resolution sizes.
#
# ABOUT COPYING OR USING PARTIAL INFORMATION:
# This script was originally created by Rodolfo Ferro. Any
# explicit usage of this script or its contents is granted
# according to the license provided and its conditions.
# ===============================================================
INSTRUCTIONS:
Over de cam screen...
1. Press <SPACEBAR> to change resolution.
2. Press <ESC> to close the camera.
"""

import numpy as np
import argparse
import imutils
import cv2


def resolution(cap, res=0):
if res:
cap.set(3, 640)
cap.set(4, 480)
return
cap.set(3, 320)
cap.set(4, 240)
return


print(__doc__)
res, size = 0, ["320x240", "640x480"]
cap = cv2.VideoCapture(0)
resolution(cap, res)

while(True):
# Capture frame-by-frame:
ret, frame = cap.read()

# Display the resulting frame:
cv2.imshow('Camera w/resolution {}'.format(size[res]), frame)

# Look for pressed key:
key = cv2.waitKey(10)
if key == 27:
break
if key == 32:
res += 1
res = res % 2
cv2.destroyWindow('Camera w/resolution {}'.format(size[(res + 1) % 2]))
resolution(cap, res)

# When everything done, release the capture:
cap.release()
cv2.destroyAllWindows()
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ Repositorio personal de la materia de Redes Computacionales 2017, impartida por
| 1 | *Chat bidireccional usando memoria compartida* | a) [Monodireccional](https://github.com/RodolfoFerro/ComputerNetworks17/tree/master/Tarea%201/mono) <br>b) [**Bidireccional**](https://github.com/RodolfoFerro/ComputerNetworks17/tree/master/Tarea%201/bi) | [Video](https://drive.google.com/file/d/0B_UdIDqyTHWGaWd0aTFjZDI2SmM/view?usp=sharing)|
| 2 | *Chat bidireccional usando sockets* | a) [Monodireccional](https://github.com/RodolfoFerro/ComputerNetworks17/tree/master/Tarea%202/mono) <br>b) [**Bidireccional**](https://github.com/RodolfoFerro/ComputerNetworks17/tree/master/Tarea%202/bi) | [Video](https://drive.google.com/file/d/0B_UdIDqyTHWGMGVKZEdiUk56ckk/view?usp=sharing)|
| 3 | *Chat multicliente usando* `fork` *y sockets* | a) [**Multicliente**](https://github.com/RodolfoFerro/ComputerNetworks17/tree/master/Tarea%203) | [Video](https://drive.google.com/file/d/0B_UdIDqyTHWGbFhVRlRYeTkwdVk/view?usp=sharing) |
| 4 | *Chatroom multicliente robusto* |
| 4 | *Chatroom multicliente robusto* | | |
| 5 | *Comparativa* `TCP`/`UDP` *en transferencia de paquetes* | a) [**TCP**](https://github.com/RodolfoFerro/ComputerNetworks17/tree/master/Tarea%205/TCP) <br>b) [**UDP**](https://github.com/RodolfoFerro/ComputerNetworks17/tree/master/Tarea%205/UDP) | [Tabla de resultados](https://github.com/RodolfoFerro/ComputerNetworks17/blob/master/Tarea%205/Resultados.md) |
| 6 | *Transferencia de archivos robusta* |
| 6 | *Transferencia de archivos robusta* | a) [Códigos](https://github.com/RodolfoFerro/ComputerNetworks17/tree/master/Tarea%206) | |

# Proyecto final: Internet of Things (IoT) 📡📲

Expand Down
Binary file added Tarea 6/client
Binary file not shown.
133 changes: 76 additions & 57 deletions Tarea 6/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,74 +8,93 @@
#include <unistd.h>
#include <errno.h>

#define MAXLINE 255 /*Longitud máxima de mensaje*/
#define BUFFER_SZ 256 /*Tamaño de buffer*/

int sock_client(){
struct sockaddr_in serv_addr;
int s, rc;

serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(7500);
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

// Si sock < 0 hay un error en la creación del socket:
s = socket( AF_INET, SOCK_STREAM, 0 );
if (s < 0){
perror("Problema creando el socket");
exit(1);
}

// Conexión del cliente al servidor:
rc = connect(s, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
if (rc){
perror("Problema al intentar la conexión con el servidor");
exit(1);
}

return s;
}

void recv_file(int sock, char *filename){
// Recibimos paquetes:
char buffer[BUFFER_SZ];
int rc;
FILE *f;
f = fopen(filename, "w");

rc = recv(sock, buffer, sizeof(buffer), 0);
while (rc){
fprintf(f, "%s", buffer);
bzero(buffer, BUFFER_SZ);
rc = read(sock, buffer, BUFFER_SZ);
}
fclose(f);

printf("The file was received successfully!\n\n");
}

int main(int argc, char **argv)
{
int sock = 0, connfd = 0;
struct sockaddr_in serv_addr;
int rc, n;
char buffer[MAXLINE];
int sock = 0, rc, n = 0;
char opt;
char buffer[BUFFER_SZ];
char filename[BUFFER_SZ];
FILE *f;

// Inicio
printf("=== CLIENT ===\n");
printf("Creador de mensajes.\n");
printf("Receptor de archivos.\n");

// Obtenemos tty para crear usuario
char *tty = ttyname(STDIN_FILENO);
char *user;
user = &tty[9];
// int user = 100*(tty[9] - '0') + 10*(tty[10] - '0') + (tty[11] - '0');
printf("Usuario %s.\n\n", user);

// Si sock < 0 hay un error en la creación del socket:
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) <0) {
perror("Problema creando el Socket");
exit(2);
}
// Creamos el socket:
sock = sock_client();
printf("Menu: \n");
printf("1. Desargar archivo \n");
printf("2. Introducir comando del sistema \n");
printf("3. Salir \n\n");

// Creación del socket
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
serv_addr.sin_port = htons(7500);
// Ciclo de control:
while (1) {
printf("Opción: ");
scanf("%c", &opt);
rc = send(sock, &opt, 1, 0);

// Conexión del clinte al servidor:
if (connect(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr))<0){
perror("Problema al intentar la conexión con el servidor");
exit(3);
opt = (int) opt;
if (opt == 49) {
recv_file(sock, "new.txt");
break;
}
if (opt == 50) {
printf("Inroduce comando: \n");
scanf("%s", &buffer);
// printf("%s\n", buffer);
send(sock, buffer, BUFFER_SZ, 0);
system(buffer);
}
if (opt == 51) {
break;
}
}

// Ciclo para leer texto:
while (1) {
// Limpiamos y escribimos un mensaje:
bzero(buffer, MAXLINE);
printf("Introduce un mensaje: ");
fgets(buffer, MAXLINE, stdin);

// Enviamos usuario y mensaje:
rc = send(sock, user, MAXLINE, 0);
rc = send(sock, buffer, MAXLINE, 0);
if (rc <= 0) perror( "Error en send" );

// Si escribimos 'EXIT', salimos:
if (strcmp(buffer, "EXIT\n") == 0) {
printf("¡Hasta luego!\n");
break;
}

// Recibimos respuesta del servidor:
bzero(buffer, MAXLINE);
rc = recv(sock, buffer, MAXLINE, 0);
if (rc < 0){
printf("Error leyendo buffer.\n");
exit(1);
}
if (rc > 0){
// Imprimimos mensaje recibido:
printf("ECHO: %s\n", buffer);
}
}

// Cerramos socket:
close(sock);
Expand Down
Empty file added Tarea 6/new.txt
Empty file.
130 changes: 130 additions & 0 deletions Tarea 6/new_serv.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#define BUFFER_SZ 256 /*Tamaño de buffer*/

int sock_server(){
struct sockaddr_in serv_addr;
int s, rc;

serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(7500);
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);

// Si sock < 0 hay un error en la creación del socket:
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0){
perror("Problema creando el socket");
exit(1);
}

// Bind del servidor al cliente:
rc = bind(s, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
if (rc < 0){
perror("Problema en bind");
exit(1);
}

// Listen al cliente:
rc = listen(s, 5);
if (rc){
perror("Problema en listen");
exit(1);
}

return s;
}

pid_t create_child(){
pid_t pid;
pid = fork();
if (pid == (pid_t)-1){
perror("Error en child");
exit(1);
}
return pid;
}

void send_file(int s, FILE *fp, char * filename){
// Enviamos archivo:
char buffer[BUFFER_SZ];
int rc;
fp = fopen(filename, "r");

while (!feof(fp)){
fscanf(fp, "%s", buffer);
rc = write(s, buffer, BUFFER_SZ);
bzero(buffer, BUFFER_SZ);
}

printf("The file was sent successfully!\n\n");
return;
}

int main(int argc, char **argv)
{
int sock = 0, s, rc;
char opt;
char buffer[BUFFER_SZ];
char filename[BUFFER_SZ];
FILE *fp;
pid_t child;

// Inicio:
printf("=== SERVER ===\n");
printf("Encargado del envío de archivos.\n");

// Creamos el socket:
sock = sock_server();

// Ciclo de control:
while (1){
// Aceptamos conexiones:
s = accept(sock, NULL, NULL);
if (s < 0){
perror("Problema aceptando conexión");
exit(1);
}

// Hacemos fork:
child = create_child();

if (child == 0){
while (1) {
rc = recv(s, &buffer, 1, 0);
if(rc < 0) {
perror("Error recibiendo opción");
exit(1);
}

opt = buffer[0];
printf("Opción recibida: %c\n", opt);
switch (opt) {
case '1':
send_file(s, fp, "test.txt");
break;

default:
printf("HERE!\n");
break;
}
}
}
}



// Cerramos socket:
fclose(fp);
close(sock);
printf("Gracias por usar este servicio.\n");
// system("ls");
return 0;
}
Binary file added Tarea 6/server
Binary file not shown.
Loading

0 comments on commit 3b141b6

Please sign in to comment.