Ir a inicioFavoritosRecomiéndanos El Prisma.com - Ingeniería de Sistemas
Inicio > Regresar a Ingeniería de Sistemas 936 visitantes activos
Sockets - Comunicación entre Procesos Distribuidos



Navigation bar
  Start Previous page
 7 of 14 
Next page End 2 3 4 5 6 7 8 9 10 11 12  

/***********************************/  
main ()  
{
 
int clientFd;  
int serverLen;  
int result;  
struct sockaddr_in serverINETAddress;  
struct sockaddr* serverSockAddrPtr;  
unsigned long inetAddress;  
serverSockAddrPtr = (struct sockaddr *) &serverINETAddress;  
serverLen = sizeof (serverINETAddress);  
while (1)  
{  
  inetAddress = promptForINETAddress ();  
  if (inetAddress == 0) break;  
  bzero ((char *)&serverINETAddress , sizeof(serverINETAddress));  
  serverINETAddress.sin_family = AF_INET;  
  serverINETAddress.sin_addr.s_addr = inetAddress;  
  serverINETAddress.sin_port = htons ( PUERTO_HORA );  
  clientFd = socket ( AF_INET,  SOCK_STREAM,  PROTOCOLO_DEFECTO );  
  do  
   {  
    result = connect( clientFd, serverSockAddrPtr, serverLen );  
    if (result == -1) sleep(1);  
   }  
  while (result == -1);  
  readTime(clientFd);  
  close(clientFd);  
}  
exit(0);   
}  
/******************  promptForINETAddress () **************************/  
unsigned long promptForINETAddress ()  
{
 
char hostName [100];  
unsigned long inetAddress;  
   
  do  
  {  
   printf ("Nombre maquina (q = salir, s = maquina propia): ");  
   scanf("%s",hostName);  
   if ( strcmp (hostName,"q") == 0 ) return(0);  
   inetAddress = nameToAddr (hostName);  
   if (inetAddress == 0 ) printf ("\n Maquina no encontrada\n");  
  }  
while ( inetAddress == 0 );  
}
 
/*******************  nameToAddr ( name ) *************************/  
unsigned long nameToAddr (char *name)  
{
 
char hostName[100];  
struct hostent* hostStruct;  
struct in_addr* hostNode;  
if (isdigit (name[0])) return (inet_addr (name));  
if (strcmp (name,"s") == 0)  
  {  
   gethostname (hostName,100);  
   printf("Nombre de la propia maquina es %s\n",hostName);  
  }  
else  
  strcpy(hostName,name);  
hostStruct = gethostbyname (hostName);  
if (hostStruct == NULL) return (0);                         /** maquina no encontrada **/  
hostNode = (struct in_addr*) hostStruct->h_addr;   /* saca la dir. IP de la struct */  
printf("Direccion IP = %s\n", inet_ntoa (*hostNode));  
return (hostNode->s_addr);                                     /* devuelve la dir IP */  
}
 
/******************** readTime ( fd ) **********************/  
readTime (int fd)  
{
 
char str[200];  
printf("La hora en el puerto destino es ");  
while (readLine (fd,str))  
  printf("%s\n",str);  
}
 
/************   readLine ( fd, str ) ******************************/  
readLine (int fd,char* str)  
{
 
int n;  
do  
  {  
   n = read(fd,str,1);  
}  
while (n>0 && *str++ != '\n');  
return (n>0);  
Previous page Top Next page
Inicio Última Actualización 09/09/2010 Sugerencias
 
Guía MBA y Masters
Conoce los mejores programas de MBA
Executive MBA y Global MBA