PRÁCTICA 3 : PROGRAMACIÓN CON SOCKETS Sockets no orientados a conexión Fichero definiciones.h

Anuncio
PRÁCTICA 3 :
PROGRAMACIÓN CON SOCKETS
Sockets no orientados a conexión
Fichero definiciones.h
#ifndef DEFINICIONES_H
#define DEFINICIONES_H
/*Struct que manda el cliente al servidor con los datos*/
typedef struct
{
float interes;
int tiempo; /*en años*/
} Credito;
#define PORT_UDP_SERV 6500
#define DIR_SERV "127.0.0.1"
#endif
Fichero servidor.h
#ifndef SERVIDOR_H
#define SERVIDOR_H
#include "definiciones.h"
#include <netinet/in.h>
struct sockaddr recibir ( void *datos , int long_datos );
void enviar ( void *datos , int long_datos , struct sockaddr *dir_cliente ,
int long_dir_cliente );
void trataSenial( int senal , int );
void error( char *mesg ); /*Funcion de error */
1
void procesar_peticion( Credito *peticion_cliente ,
struct sockaddr *dir_cliente );
float cuota_mensual( float interes , int tiempo );
#endif
Fichero servidor.c
/***************
Julian Chaves Caceres
David Suarez Fuentes
Lab. Redes de Computadores
Ingeneria Electronica
****************/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <math.h>
#include "servidor.h"
#include "definiciones.h"
int mi_socket;
void main(void)
{
struct sockaddr_in ser_addr; /*Direccion servidor*/
Credito peticion_cliente; /*struct con la peticion del cliente*/
2
struct sockaddr dir_cliente; /*direccion del cliente*/
int long_dir_cliente = sizeof( dir_cliente );
signal( 15 , trataSenial ); /*Senial TERM para terminar programa*/
/*Abrimos socket */
mi_socket = socket( AF_INET , SOCK_DGRAM , 0 );
if ( mi_socket == −1 )
{
error(" Error al crear el socket");
}
printf("\nSERVIDOR : %s\n" , DIR_SERV );
printf("\nPUERTO : %u\n" , PORT_UDP_SERV );
ser_addr.sin_family = AF_INET;
ser_addr.sin_addr.s_addr = inet_addr( DIR_SERV );
ser_addr.sin_port = htons( PORT_UDP_SERV );
/*Hacemos el bind */
if ( bind( mi_socket ,
(struct sockaddr*) &ser_addr ,
sizeof( ser_addr ) ) == −1 )
{
error("Error al hacer el bind");
}
while ( 1 ) /*bucle infinito, */
{
/*esperamos a recibir la informacion del cliente*/
dir_cliente = recibir( &peticion_cliente , sizeof( peticion_cliente ) );
/*procesamos la peticion y enviamos respuesta */
3
procesar_peticion( &peticion_cliente , &dir_cliente );
}
}
struct sockaddr recibir( void *datos , int long_datos )
{
int n_bytes;
struct sockaddr dir_cliente;
int long_dir_cliente = sizeof( dir_cliente );
n_bytes = recvfrom( mi_socket ,
datos ,
long_datos ,
0,
&dir_cliente ,
&long_dir_cliente );
if ( n_bytes == −1 )
{
error("Error en recvfrom");
}
return dir_cliente;
}
void enviar( void *datos , int long_datos , struct sockaddr *dir_cliente ,
int long_dir_cliente )
{
int bytes;
bytes = sendto( mi_socket ,
datos ,
4
long_datos ,
0,
dir_cliente ,
long_dir_cliente );
if ( bytes != long_datos )
{
error( "Error en el sendto" );
}
}
void procesar_peticion( Credito *peticion_cliente ,
struct sockaddr *dir_cliente )
{
float respuesta;
respuesta = cuota_mensual( peticion_cliente−>interes ,
peticion_cliente−>tiempo );
enviar( &respuesta , sizeof( respuesta ) ,
dir_cliente , sizeof( *dir_cliente ) );
}
void error( char *mesg)
{
printf("\n%s\n" , mesg );
perror( "" );
exit(1);
}
void trataSenial( int senial , int algo )
{
5
printf( "\nTrata senial TERM \n" );
if ( senial == 15 )
close ( mi_socket );
printf("\nSocket cerrado \n");
exit( 0 );
}
float cuota_mensual( float interes , int tiempo )
{
float a = 1 + ( interes / 1200 );
float respuesta ;
a = pow( a , (double)(12*tiempo) );
respuesta = (1000000 * a * interes / 1200 ) / ( a − 1 );
printf("\nInteres : %f%% ; ", interes );
printf("Tiempo : %d años ; ", tiempo );
printf("Cuota : %f ptas/mes \n\n", respuesta );
fflush( stdout );
return respuesta ;
}
Fichero cliente.h
#ifndef _CLIENTE_H
#define _CLIENTE_H
#include "definiciones.h"
void error( char *mesg );
struct sockaddr recibir ( void *datos , int long_datos );
void enviar ( void *datos , int long_datos , struct sockaddr *dir_servidor ,
int long_dir_servidor );
6
void obtener_datos( Credito *mi_peticion );
#endif
Fichero cliente.c
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include "cliente.h"
#include "definiciones.h"
int mi_socket;
void main( int argc , char *argv[] )
{
struct sockaddr_in dir_cliente , dir_servidor;
Credito mi_peticion;
float respuesta ;
dir_servidor.sin_family = AF_INET;
dir_servidor.sin_addr.s_addr = inet_addr( DIR_SERV );
dir_servidor.sin_port = htons( PORT_UDP_SERV );
dir_cliente.sin_family = AF_INET;
dir_cliente.sin_addr.s_addr = htonl( INADDR_ANY );
dir_cliente.sin_port = htons( 0 );
mi_socket = socket( AF_INET , SOCK_DGRAM , 0 );
if ( mi_socket == −1 )
{
7
error("Error al abrit socket");
}
if ( bind( mi_socket , (struct sockaddr*) &dir_cliente ,
sizeof( dir_cliente ) ) == −1 )
{
error( "CLIENTE: Error al hacer el bind ");
}
/*Obtenemos los datos a enviar */
obtener_datos ( &mi_peticion );
enviar( &mi_peticion , sizeof( mi_peticion ) ,
(struct sockaddr*)&dir_servidor , sizeof( dir_servidor ) );
recibir( &respuesta , sizeof( respuesta ) );
printf ( "\nRESPUESTA : %f \n" , respuesta );
close ( mi_socket );
}
struct sockaddr recibir( void *datos , int long_datos )
{
int n_bytes;
struct sockaddr dir_servidor;
int long_dir_servidor = sizeof( dir_servidor );
n_bytes = recvfrom( mi_socket ,
datos ,
long_datos ,
0,
&dir_servidor ,
&long_dir_servidor );
8
if ( n_bytes == −1 )
{
error("Error en recvfrom");
}
return dir_servidor;
}
void enviar( void *datos , int long_datos , struct sockaddr *dir_servidor ,
int long_dir_servidor )
{
int bytes;
bytes = sendto( mi_socket ,
datos ,
long_datos ,
0,
dir_servidor ,
long_dir_servidor );
if ( bytes != long_datos )
{
error( "Error en el sendto" );
}
}
void error( char *mesg)
{
printf("\n%s\n" , mesg );
perror( "\n%s\n" , mesg );
exit(1);
9
}
void obtener_datos ( Credito *mi_peticion )
{
printf("\n\nInteres : " );
scanf("%f" , &(mi_peticion−>interes) );
printf("Tiempo ( en años ) : ");
scanf("%d" , &(mi_peticion−>tiempo) );
}
Socket orientados a conexión
Fichero definiciones.h
#ifndef DEFINICIONES_H
#define DEFINICIONES_H
/*Struct que manda el cliente al servidor con los datos*/
typedef struct
{
float interes;
int tiempo; /*en años*/
} Credito;
#define PORT_TCP_SERV 6500
#define DIR_SERV "127.0.0.1"
#endif
Fichero servidor.h
#ifndef SERVIDOR_H
#define SERVIDOR_H
#include "definiciones.h"
#include <netinet/in.h>
10
int recibir ( int nuevo_socket , void *datos , int long_datos );
void enviar ( int nuevo_socket , void *datos , int long_datos );
void error( char *mesg ); /*Funcion de error */
void procesar_peticion( int nuevo_socket , Credito *peticion_cliente );
float cuota_mensual( float interes , int tiempo );
#endif
Fichero servidor.c
/***************
Julian Chaves Caceres
David Suarez Fuentes
Lab. Redes de Computadores
Ingeneria Electronica
****************/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <math.h>
#include "servidor.h"
void main(void)
{
struct sockaddr_in ser_addr; /*Direccion servidor*/
int mi_socket;
11
int nuevo_socket; /*nuevo socket que se recibi al aceptar conexion*/
int n_bytes; /*numero de bytes leidos del cliente */
Credito peticion_cliente; /*struct con la peticion del cliente*/
struct sockaddr dir_cliente; /*direccion del cliente*/
int long_dir_cliente = sizeof( dir_cliente );
/*Abrimos el socket*/
mi_socket = socket( AF_INET , SOCK_STREAM , 0 );
if ( mi_socket == −1 )
{
error(" Error al crear el socket");
}
printf("\nSERVIDOR : %s\n" , DIR_SERV );
printf("\nPUERTO : %u\n" , PORT_TCP_SERV );
ser_addr.sin_family = AF_INET;
ser_addr.sin_addr.s_addr = inet_addr( DIR_SERV );
ser_addr.sin_port = htons( PORT_TCP_SERV );
/*Hacemos el bind */
if ( bind( mi_socket ,
(struct sockaddr*) &ser_addr ,
sizeof( ser_addr ) ) == −1 )
{
error("Error al hacer el bind");
}
if ( listen( mi_socket , 5 ) == −1 )
{
error("Error al hacer listen");
12
}
nuevo_socket = accept( mi_socket , &dir_cliente , &long_dir_cliente);
if ( nuevo_socket == −1 )
{
error( "Error al aceptar conexion ");
}
printf("\nConexion aceptada\n" );
do
{
/*Recibimos los datos del cliente */
n_bytes = recibir( nuevo_socket ,
&peticion_cliente ,
sizeof( peticion_cliente ) );
/*Procesamos y enviamos respuesta */
procesar_peticion( nuevo_socket , &peticion_cliente );
} while ( n_bytes );
close ( nuevo_socket );
close ( mi_socket );
printf("\nCerrada conexion con cliente \n");
exit( 0 );
}
int recibir( int nuevo_socket , void *datos , int long_datos )
{
int n_bytes;
n_bytes = recv( nuevo_socket ,
datos ,
13
long_datos ,
0 );
if ( n_bytes == −1 )
{
error("Error en recv");
}
return n_bytes;
}
void enviar( int nuevo_socket , void *datos , int long_datos )
{
int bytes;
bytes = send( nuevo_socket ,
datos ,
long_datos ,
0 );
if ( bytes != long_datos )
{
error( "Error en el send" );
}
}
void procesar_peticion( int nuevo_socket ,
Credito *peticion_cliente )
{
float respuesta;
respuesta = cuota_mensual( peticion_cliente−>interes ,
peticion_cliente−>tiempo );
14
enviar( nuevo_socket , &respuesta , sizeof( respuesta ) );
}
void error( char *mesg)
{
printf("\n%s\n" , mesg );
perror( "" );
exit(1);
}
float cuota_mensual( float interes , int tiempo )
{
float a = 1 + ( interes / 1200 );
float respuesta ;
a = pow( a , (double)(12*tiempo) );
respuesta = (1000000 * a * interes / 1200 ) / ( a − 1 );
printf("\nInteres : %f%% ; ", interes );
printf("Tiempo : %d años ; ", tiempo );
printf("Cuota : %f ptas/mes \n\n", respuesta );
fflush( stdout );
return respuesta ;
}
Fichero cliente.h
#ifndef _CLIENTE_H
#define _CLIENTE_H
#include "definiciones.h"
void error( char *mesg );
void recibir ( void *datos , int long_datos );
15
void enviar ( void *datos , int long_datos );
void obtener_datos( Credito *mi_peticion );
#endif
Fichero cliente.c
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include "cliente.h"
#include "definiciones.h"
int mi_socket;
void main( int argc , char *argv[] )
{
struct sockaddr_in dir_cliente , dir_servidor;
Credito mi_peticion;
float respuesta ;
dir_servidor.sin_family = AF_INET;
dir_servidor.sin_addr.s_addr = inet_addr( DIR_SERV );
dir_servidor.sin_port = htons( PORT_TCP_SERV );
dir_cliente.sin_family = AF_INET;
dir_cliente.sin_addr.s_addr = htonl( INADDR_ANY );
dir_cliente.sin_port = htons( 0 );
mi_socket = socket( AF_INET , SOCK_STREAM , 0 );
if ( mi_socket == −1 )
16
{
error("Error al abrit socket");
}
if ( connect( mi_socket , ( struct sockaddr* )&dir_servidor ,
sizeof( dir_servidor ) ) == −1 )
{
error("Error al hacer connect");
}
while ( 1 )
{
/*Obtenemos los datos a enviar */
obtener_datos ( &mi_peticion );
if ( mi_peticion.interes == 0 )
/*Salimos*/
break;
enviar( &mi_peticion , sizeof( mi_peticion ) );
recibir( &respuesta , sizeof( respuesta ) );
printf ( "\nRESPUESTA : %f \n" , respuesta );
}
close ( mi_socket );
}
void recibir( void *datos , int long_datos )
{
int n_bytes;
n_bytes = recv( mi_socket ,
datos ,
17
long_datos ,
0);
if ( n_bytes == −1 )
{
error("Error en recvfrom");
}
}
void enviar( void *datos , int long_datos )
{
int bytes;
bytes = send( mi_socket ,
datos ,
long_datos ,
0 );
if ( bytes != long_datos )
{
error( "Error en el sendto" );
}
}
void error( char *mesg)
{
printf("\n%s\n" , mesg );
perror( "\n%s\n" , mesg );
exit(1);
}
void obtener_datos ( Credito *mi_peticion )
18
{
printf("\n\nInteres : " );
scanf("%f" , &(mi_peticion−>interes) );
printf("Tiempo ( en años ) : ");
scanf("%d" , &(mi_peticion−>tiempo) );
}
19
Descargar