#include "i_addr.h" #include "str_com.h" int main(int argc, char *argv[]) { int sfd, nsfd, pid; struct sockaddr_in d_servidor, d_cliente; int lon_d_cliente; char nombre_host[256]; struct hostent *host_servidor; if (gethostname(nombre_host, sizeof(nombre_host)) == -1) { printf("Error en la asignacion del nombre del host"); exit(-1); } if ((host_servidor=gethostbyname(nombre_host)) == NULL) { printf("Error en la lectura del archivo /etc/host"); exit(-1); } if ((sfd=socket(AF_INET, SOCK_STREAM, 0)) == -1) { printf("Error en la creacion del socket"); exit(-1); } printf("Servidor de conversacion:%s\n",host_servidor->h_name); d_servidor.sin_family = AF_INET; d_servidor.sin_addr.s_addr = *(long *)host_servidor->h_addr; d_servidor.sin_port = htons(7228); if (bind(sfd,&d_servidor,sizeof(d_servidor)) == -1) { printf("Error en la publicidad del socket"); exit(-1); } listen(sfd,10); for (;;) { lon_d_cliente=sizeof(d_cliente); if ((nsfd=accept(sfd,&d_cliente,&lon_d_cliente)) == -1) { printf("Error en la aceptacion de conversacion"); exit(-1); } pid=fork(); if (pid==-1) { printf("Error en la creacion del proceso hijo"); exit(-1); } else if (pid==0) { close(sfd); recibir_conexion(nsfd,host_servidor->h_name); close(nsfd); exit(0); } close(nsfd); } }