Advertisement
Guest User

Untitled

a guest
Mar 26th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <pthread.h>
  4. #include <stdio.h>
  5. #include <netinet/in.h>
  6. #include <signal.h>
  7. #include <unistd.h>
  8. #define MAX_CLIENT 10
  9. #include <sys/ioctl.h>
  10. int totcltes=0;
  11. int client_sockfd[MAX_CLIENT];
  12. char client_name[MAX_CLIENT][20];
  13. void *coneccion_clte(void *arg);
  14.  
  15. int main()
  16. {
  17.  
  18. int server_sockfd;
  19.  
  20. int server_len, client_len;
  21.  
  22. struct sockaddr_in server_address;
  23.  
  24. struct sockaddr_in client_address;
  25.  
  26. int parametro[MAX_CLIENT];
  27.  
  28. pthread_t tid[MAX_CLIENT];
  29.  
  30. int i;
  31.  
  32. server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
  33.  
  34. //recv blocking
  35. //int iMode = 0;
  36.  
  37. //ioctl(server_sockfd, FIONBIO, &iMode);
  38.  
  39. server_address.sin_family = AF_INET;
  40.  
  41. server_address.sin_addr.s_addr = htonl(INADDR_ANY);
  42.  
  43. server_address.sin_port = htons(9734);
  44.  
  45. server_len = sizeof(server_address);
  46.  
  47. bind(server_sockfd,(struct sockaddr *)&server_address,server_len);
  48. /* Crear una cola de conexiones */
  49. listen(server_sockfd, MAX_CLIENT);
  50.  
  51. while(totcltes<MAX_CLIENT) //
  52. {
  53. printf("server esperando, clientes atendidos %d\n",totcltes);
  54. /* Aceptar conexión */
  55. client_sockfd[totcltes] = accept(server_sockfd,(struct sockaddr *)&client_address, &client_len);
  56. read(client_sockfd[totcltes], client_name[totcltes], 20);
  57. /* Crea un hilo que atiende al nuevo cliente */
  58. parametro[totcltes]=totcltes;
  59. pthread_create(&tid[totcltes],NULL,coneccion_clte,(void *)&parametro[totcltes]);
  60. }
  61. for(i=0;i<MAX_CLIENT;i++)
  62. pthread_join(tid[i],NULL);
  63. }
  64.  
  65. void *coneccion_clte(void *arg)
  66. {
  67. totcltes++;
  68. char message[500];
  69. int *n=(int *) arg;
  70. int mynum=*n;
  71. while(1){
  72. read(client_sockfd[mynum], message, 500);
  73. sleep(5);
  74. int i;
  75. printf("Message %s\n", message);
  76. for (i = 0; i < totcltes; i++){
  77. //if (i != mynum){
  78. printf("write");
  79. write(client_sockfd[i], client_name[mynum], 20);
  80. write(client_sockfd[i], message, 500);
  81. //}
  82. }
  83. }
  84. close(client_sockfd[mynum]);
  85. return;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement