/************************************************************/
/************** cliente.c **********************/
/************************************************************/
/********* proceso cliente con sockets AF_UNIX **********/
/************************************************************/
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h> /* para sockets UNIX */
#define PROTOCOLO_DEFECTO 0
/****************************************************/
main()
{
int dfClient, longServer, resultado;
struct sockaddr_un dirUNIXServer;
struct sockaddr* puntSockServer;
puntSockServer = ( struct sockaddr* ) &dirUNIXServer;
longServer = sizeof ( dirUNIXServer );
dfClient = socket ( AF_UNIX, SOCK_STREAM, PROTOCOLO_DEFECTO );
/* se crea un socket UNIX, bidireccional */
dirUNIXServer.sun_family = AF_UNIX; /* tipo de dominio server */
strcpy ( dirUNIXServer.sun_path, "fichero" ); /* nombre server */
do
{
resultado = connect ( dfClient, puntSockServer, longServer );
if ( resultado == -1 ) sleep (1); /* reintento */
}
while ( resultado == -1 );
leeFichero ( dfClient ); /* lee el fichero */
close ( dfClient ); /* cierra el socket */
exit (0); /* buen fin */
}
/************* leeFichero ( df ) *****************/
leeFichero ( int df )
{
char cad[200];
while ( leeLinea ( df, cad ) ) /* lee hasta fin de la entrada */
printf ("%s\n", cad ); /* e imprime lo leido */
}
/************* leeLinea ( df, cad ) ******************/
leeLinea ( int df, char *cad )
{
int n;
do
{
n = read ( df, cad, 1 ); /* lectura de un caracter */
}
while ( n > 0 && *cad++ != NULL ); /* lee hasta NULL o fin entrada */
return ( n > 0 ); /* devuelve falso si fin de entrada */
}
/************* fin de cliente.c ****************************/
EJEMPLO DE COMUNICACION CON SOCKETS INET
( ENTRE DIFERENTES MAQUINAS )
/*************************************************/
/**************** hora.c ***********************/
/*************************************************/
/* visualiza el dia y la hora de un host */
/*************************************************/
#include <stdio.h>
#include <signal.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h> /* socket INET */
#include <arpa/inet.h>
#include <netdb.h>
#define PUERTO_HORA 13
#define PROTOCOLO_DEFECTO 0
unsigned long promptForINETAddress ();
unsigned long nameToAddr ();