Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <pthread.h>
- #include <stdio.h>
- #include <netinet/in.h>
- #include <signal.h>
- #include <unistd.h>
- #define MAX_CLIENT 10
- #include <sys/ioctl.h>
- int totcltes=0;
- int client_sockfd[MAX_CLIENT];
- char client_name[MAX_CLIENT][20];
- void *coneccion_clte(void *arg);
- int main()
- {
- int server_sockfd;
- int server_len, client_len;
- struct sockaddr_in server_address;
- struct sockaddr_in client_address;
- int parametro[MAX_CLIENT];
- pthread_t tid[MAX_CLIENT];
- int i;
- server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
- //recv blocking
- //int iMode = 0;
- //ioctl(server_sockfd, FIONBIO, &iMode);
- server_address.sin_family = AF_INET;
- server_address.sin_addr.s_addr = htonl(INADDR_ANY);
- server_address.sin_port = htons(9734);
- server_len = sizeof(server_address);
- bind(server_sockfd,(struct sockaddr *)&server_address,server_len);
- /* Crear una cola de conexiones */
- listen(server_sockfd, MAX_CLIENT);
- while(totcltes<MAX_CLIENT) //
- {
- printf("server esperando, clientes atendidos %d\n",totcltes);
- /* Aceptar conexión */
- client_sockfd[totcltes] = accept(server_sockfd,(struct sockaddr *)&client_address, &client_len);
- read(client_sockfd[totcltes], client_name[totcltes], 20);
- /* Crea un hilo que atiende al nuevo cliente */
- parametro[totcltes]=totcltes;
- pthread_create(&tid[totcltes],NULL,coneccion_clte,(void *)¶metro[totcltes]);
- }
- for(i=0;i<MAX_CLIENT;i++)
- pthread_join(tid[i],NULL);
- }
- void *coneccion_clte(void *arg)
- {
- totcltes++;
- char message[500];
- int *n=(int *) arg;
- int mynum=*n;
- while(1){
- read(client_sockfd[mynum], message, 500);
- sleep(5);
- int i;
- printf("Message %s\n", message);
- for (i = 0; i < totcltes; i++){
- //if (i != mynum){
- printf("write");
- write(client_sockfd[i], client_name[mynum], 20);
- write(client_sockfd[i], message, 500);
- //}
- }
- }
- close(client_sockfd[mynum]);
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement