do {
listen (descrServer, longCola)
descrClient = accept
(descrServer,PuntSockClient,longClient)
result = connect (descrClient,
PuntSockServer,longserver)
[ close (descrServer) ]
} while ( result == -1 )
< DIALOGO >
< DIALOGO >
close (descrClient)
close (descrClient)
COMPARACION SOCKETS-PIPES COMO MECANISMOS DE COMUNICACION
ENTRE PROCESOS
SOCKETS PIPES
refenciado por
descriptores
referenciado por
array de
descriptores
admite
comunicación entre
procesos de
distintas máquinas
sólo admite
comunicación entre
procesos de la
misma máquina
comunicación
bidireccional
comunicación
unidireccional
filosofía cliente-
servidor
simple intercambio
de información
EJEMPLO DE COMUNICACION MEDIANTE SOCKETS TIPO UNIX ( EN LA MISMA
MAQUINA )
/************************************************************/
/************** servidor.c **********************/
/************************************************************/
/********* proceso servidor 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 dfServer, dfClient, longServer, longClient;
struct sockaddr_un dirUNIXServer;
struct sockaddr_un dirUNIXClient;
struct sockaddr* puntSockServer;
struct sockaddr* puntSockClient;
signal ( SIGCHLD, SIG_IGN ); /* para no crear zombies */
puntSockServer = ( struct sockaddr* ) &dirUNIXServer;
longServer = sizeof ( dirUNIXServer );
puntSockClient = ( struct sockaddr* ) &dirUNIXClient;
longClient = sizeof ( dirUNIXClient );
dfServer = socket ( AF_UNIX, SOCK_STREAM, PROTOCOLO_DEFECTO );
/* se crea un socket UNIX, bidireccional */
dirUNIXServer.sun_family = AF_UNIX; /* tipo de dominio */
strcpy ( dirUNIXServer.sun_path, "fichero" ); /* nombre */
unlink ( "fichero" );
bind ( dfServer, puntSockServer, longServer ); /* crea el fichero */
/* o sea, nombra el socket */
printf ("\n estoy a la espera \n");
listen ( dfServer, 5 );
while (1)
{
dfClient = accept ( dfServer, puntSockClient, &longClient );
/* acepta la conexion cliente */
printf ("\n acepto la conexion \n");
if ( fork() == 0 ) /* crea hijo y envia fichero */
{
escribeFichero ( dfClient );
close ( dfClient ); /* cierra el socket */
exit ( 0 );
}
else
close ( dfClient ); /* cierra el descriptor cliente */
} /* en el padre */
}
/******** funcion escribeFichero( df ) ***************/
escribeFichero ( int df )
{
static char* linea1 = "esta es la linea 1, ";
static char* linea2 = "y esta la linea 2. ";
write ( df, linea1, strlen (linea1) + 1 );
write ( df, linea2, strlen (linea2) + 1 );
}
/****************** fin de servidor.c ***********************/