Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <strings.h>
  4. #include <sys/types.h>
  5. #include <sys/fcntl.h>
  6. #include <sys/socket.h>
  7. #include <sys/select.h>
  8. #include <netinet/in.h>
  9. #include <netdb.h>
  10. #include <unistd.h>
  11.  
  12. #define SERV_PORT 10007 /* arbitrary, but client and server must agree */
  13. #define QUEUE_SIZE 10
  14. #define BUF_SIZE 1024
  15.  
  16. typedef struct user{
  17. char *name;
  18. int fd;
  19. int status; //on ou off
  20. user *pnext; //apontador para o proximo user
  21. }USER;
  22.  
  23. typedef struct allUsers{
  24. int nUsers;
  25. USER *pfirst;
  26. }ALLUSERS;
  27.  
  28. typedef struct message{
  29. char text[50];
  30. int type; //tipo da mensagem
  31. }MESSAGE;
  32.  
  33.  
  34. struct sockaddr_in init_server_info()
  35. {
  36. struct sockaddr_in servaddr;
  37. bzero(&servaddr, sizeof(servaddr));
  38. servaddr.sin_family = AF_INET;
  39. servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  40. servaddr.sin_port = htons(SERV_PORT);
  41. return servaddr;
  42. }
  43. /*
  44. Adicionar um novo user
  45. */
  46. void addNewUser(ALLUSERS users , int fd, char name[20]);
  47. // USER *new=malloc(sizeof(USER));
  48. // new->name=malloc(sizeof(char)*strlen(name)+1);
  49. // strcpy(new->name,name);
  50. new->fd=malloc(sizeof(fd));//?
  51. new->fd=fd;//?
  52. new->pnext=NULL;
  53. if(users.nUsers!=0){
  54. USER *current=users.pfirst;
  55. while(1){//loop infinito até encontrar o ultimo elemento, quando o encontra coloca o novo asseguir
  56. if (current->pnext==NULL){
  57. current->pnext=new;
  58. break;
  59. }
  60. current=current->pnext;
  61. }
  62. }
  63. else
  64. users.pfirst=new;
  65. users.nUsers++;
  66. }
  67. int main(int argc, char **argv)
  68. {
  69. /*
  70. criar a lista ligada de users
  71. */
  72. USERS users = {0,NULL};
  73. int maxfd, listenfd;
  74. fd_set rset, allset;
  75. char buf[BUF_SIZE];
  76. struct sockaddr_in cliaddr, servaddr;
  77.  
  78. listenfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  79.  
  80. servaddr = init_server_info();
  81.  
  82. bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
  83.  
  84. listen(listenfd, QUEUE_SIZE);
  85.  
  86. maxfd = listenfd;
  87.  
  88. FD_ZERO(&allset);
  89. FD_SET(listenfd, &allset);//watch for new connections
  90. FD_SET(0, &allset);//watch for something from the keyboard
  91. for (;;)
  92. {
  93. rset = allset;//copia do allset porque sempre que usamos o select ele destroy o fd_set
  94. int nready = select(maxfd + 1, &rset, NULL, NULL, NULL);//blocks until something happen in the file descriptors watched
  95. /*
  96. ciclo para iterar todas as sockets
  97. */
  98. for(int i=0;i<nready;i++){
  99. if (FD_ISSET(listenfd, &rset))//check if something happen in the listenfd (new connection)
  100. {
  101. printf("Read from socket %d\n",i);
  102. socklen_t clilen = sizeof(cliaddr);
  103. int connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen);//connfd= fd da nova coneção
  104. /*
  105. add á lista ligada o novo user
  106. */
  107. addNewUser(users,connfd,?);
  108.  
  109. int bytes = read(connfd, buf, BUF_SIZE);
  110. write(1, buf, bytes);
  111. write(1,"\n",2);
  112. }
  113. else if (FD_ISSET(0, &rset)) //check if something happen in the stdin (new input from the keyboard)
  114. {
  115. printf("Read from keyboard\n");
  116. int bytes = read(0, buf, BUF_SIZE);
  117. write(1, buf, bytes);
  118. }
  119. }
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement