Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. #include "../compumundo.c"
  2. #include <pthread.h>
  3.  
  4. #define PARAMETROS {"PUERTO","MARCOS","MARCO_SIZE","ENTRADAS_CACHE","CACHE_X_PROC","REEMPLAZO_CACHE","RETARDO_MEMORIA"}
  5.  
  6. // ESTRUCTURAS DE SOCKET
  7.  
  8. int addrLen; // Variable para guardar un sizeof de estructura
  9. struct sockaddr_in direccionCliente; // Direccion de clientes genericos
  10. int SCKTNuevo; // Variable para descriptor de fichero nuevo
  11.  
  12. typedef struct asd {
  13. int socket;
  14. struct asd * siguiente;
  15. } nodoSocket;
  16.  
  17. nodoSocket * listaSockets = NULL;
  18.  
  19. // Inserta un socket en la lista de sockets, y si no existe, la crea
  20. nodoSocket * insertarSocket(nodoSocket * lista, int SCKTNuevo){
  21.  
  22. nodoSocket * puntero;
  23. nodoSocket * nodoNuevo = (nodoSocket *) malloc(sizeof(nodoSocket));
  24.  
  25. if (lista == NULL){
  26.  
  27. nodoSocket * listaNueva = (nodoSocket *) malloc(sizeof(nodoSocket));
  28.  
  29. listaNueva->socket = SCKTNuevo;
  30. listaNueva->siguiente = NULL;
  31.  
  32. return listaNueva;
  33.  
  34. } else {
  35.  
  36. puntero = lista;
  37. while(puntero->siguiente != NULL){
  38. puntero = puntero->siguiente;
  39. }
  40.  
  41. nodoNuevo->socket = SCKTNuevo;
  42. nodoNuevo->siguiente = NULL;
  43.  
  44. puntero->siguiente = nodoNuevo;
  45.  
  46. return lista;
  47.  
  48. }
  49.  
  50. }
  51.  
  52. // Recorre una lista de sockets
  53. void imprimirSockets(nodoSocket * lista){
  54.  
  55. nodoSocket * p = lista;
  56.  
  57. while((p->siguiente) != NULL){
  58. printf("%d", (p->socket));
  59. p = p->siguiente;
  60. }
  61.  
  62. printf("%d", (p->socket));
  63. }
  64.  
  65.  
  66. void * crearConexion(int SCKTListener){
  67.  
  68. if((SCKTNuevo = accept(SCKTListener, (void *) &direccionCliente, &addrLen)) == -1){
  69. printf("Error: Fallo el accept() \n");
  70. }
  71.  
  72. printf("%d \n", SCKTNuevo);
  73.  
  74. listaSockets = insertarSocket(listaSockets, SCKTNuevo);
  75. imprimirSockets(listaSockets);
  76. }
  77.  
  78. // Funcion main pide: Archivo de configuracion .ini
  79. int main(int argc, char **argv) {
  80.  
  81. // Variable que se usa para absorber el codigo de error de una funcion
  82. int codigo;
  83.  
  84. // Compruebo que no falten argumentos del main
  85. if(argc != 2){
  86. codigo = -10;
  87. printf("Error con los parametros. \n"
  88. "Sintaxis: archivo.out 'path de archivo de configuracion' \n");
  89. return codigo;
  90. }
  91.  
  92. // Identifico y separo los argumentos del main
  93. char * path = argv[1];
  94.  
  95. // Compruebo existencia del archivo de configuracion en el path
  96. codigo = existeArchivo(path);
  97. if(codigo == -11){
  98. printf("El archivo de configuracion no existe. \n");
  99. return codigo;
  100. }
  101.  
  102. // Crep un puntero a archivo de configuracion
  103. t_config * configuracion = config_create(path);
  104.  
  105. //Imprimo los valores de la configuracion
  106. char * parametros[] = PARAMETROS;
  107. codigo = imprimirConfiguracion(configuracion,parametros,sizeof(parametros)/sizeof(parametros[0]));
  108. switch (codigo){
  109. case -12:
  110. printf("Error en la cantidad de parámetros en el archivo de configuración. \n");
  111. break;
  112. case -13:
  113. printf("Parametro definido en archivo de configuración inexistente. \n");
  114. break;
  115. }
  116.  
  117. // ESTRUCTURAS Y FUNCIONES DE SOCKETS -------------------------------------------------------
  118.  
  119. struct sockaddr_in direccionServidor; // Direccion de Memoria como servidor
  120.  
  121. int SCKTListener = socket(AF_INET,SOCK_STREAM,0); // Socket Listener
  122.  
  123. direccionServidor.sin_family = AF_INET;
  124. direccionServidor.sin_addr.s_addr = INADDR_ANY;
  125. direccionServidor.sin_port = htons(config_get_int_value(configuracion,"PUERTO"));
  126.  
  127. int yes = 1; // Variable para setsockopt
  128. int cantBytes; // Variable que guarda cantidad de bytes recibidos por mensaje
  129. char bufferMsgRec[MAXSIZEMSG]; // Buffer de carga de mensaje recibido
  130. char bufferMsgSend[MAXSIZEMSG]; // Buffer de carga de mensaje a enviar
  131.  
  132. // Reinicia el puerto
  133. if(setsockopt(SCKTListener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1){
  134. printf("Error: Fallo el setsockopt() \n");
  135. return -21;
  136. }
  137.  
  138. // Bindeo a el puerto
  139. if(bind(SCKTListener,(void*) &direccionServidor, sizeof(direccionServidor)) == -1){
  140. printf("Error: Fallo el bind() \n");
  141. return -14;
  142. }
  143.  
  144. // Pongo al socket a escuchar conexiones
  145. // CANTCLIENTES: Cantidad maxima de clientes (definido en compumundo.c)
  146. if (listen(SCKTListener,CANTCLIENTES) == -1) {
  147. printf("Error: Fallo el listen() \n");
  148. return -15;
  149. }
  150.  
  151. pthread_t hiloConexion;
  152. int paramConexion = SCKTListener;
  153.  
  154. while(1){
  155.  
  156. pthread_create(&hiloConexion, NULL, (void *) crearConexion, (void *) paramConexion);
  157. pthread_join(hiloConexion,NULL);
  158.  
  159. }
  160.  
  161.  
  162. return EXIT_SUCCESS;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement