Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <strings.h>
- #include <sys/types.h>
- #include <sys/fcntl.h>
- #include <sys/socket.h>
- #include <sys/select.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <unistd.h>
- #define SERV_PORT 10007 /* arbitrary, but client and server must agree */
- #define QUEUE_SIZE 10
- #define BUF_SIZE 1024
- typedef struct user{
- char *name;
- int fd;
- int status; //on ou off
- user *pnext; //apontador para o proximo user
- }USER;
- typedef struct allUsers{
- int nUsers;
- USER *pfirst;
- }ALLUSERS;
- typedef struct message{
- char text[50];
- int type; //tipo da mensagem
- }MESSAGE;
- struct sockaddr_in init_server_info()
- {
- struct sockaddr_in servaddr;
- bzero(&servaddr, sizeof(servaddr));
- servaddr.sin_family = AF_INET;
- servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
- servaddr.sin_port = htons(SERV_PORT);
- return servaddr;
- }
- /*
- Adicionar um novo user
- */
- void addNewUser(ALLUSERS users , int fd, char name[20]);
- // USER *new=malloc(sizeof(USER));
- // new->name=malloc(sizeof(char)*strlen(name)+1);
- // strcpy(new->name,name);
- new->fd=malloc(sizeof(fd));//?
- new->fd=fd;//?
- new->pnext=NULL;
- if(users.nUsers!=0){
- USER *current=users.pfirst;
- while(1){//loop infinito até encontrar o ultimo elemento, quando o encontra coloca o novo asseguir
- if (current->pnext==NULL){
- current->pnext=new;
- break;
- }
- current=current->pnext;
- }
- }
- else
- users.pfirst=new;
- users.nUsers++;
- }
- int main(int argc, char **argv)
- {
- /*
- criar a lista ligada de users
- */
- USERS users = {0,NULL};
- int maxfd, listenfd;
- fd_set rset, allset;
- char buf[BUF_SIZE];
- struct sockaddr_in cliaddr, servaddr;
- listenfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- servaddr = init_server_info();
- bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
- listen(listenfd, QUEUE_SIZE);
- maxfd = listenfd;
- FD_ZERO(&allset);
- FD_SET(listenfd, &allset);//watch for new connections
- FD_SET(0, &allset);//watch for something from the keyboard
- for (;;)
- {
- rset = allset;//copia do allset porque sempre que usamos o select ele destroy o fd_set
- int nready = select(maxfd + 1, &rset, NULL, NULL, NULL);//blocks until something happen in the file descriptors watched
- /*
- ciclo para iterar todas as sockets
- */
- for(int i=0;i<nready;i++){
- if (FD_ISSET(listenfd, &rset))//check if something happen in the listenfd (new connection)
- {
- printf("Read from socket %d\n",i);
- socklen_t clilen = sizeof(cliaddr);
- int connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen);//connfd= fd da nova coneção
- /*
- add á lista ligada o novo user
- */
- addNewUser(users,connfd,?);
- int bytes = read(connfd, buf, BUF_SIZE);
- write(1, buf, bytes);
- write(1,"\n",2);
- }
- else if (FD_ISSET(0, &rset)) //check if something happen in the stdin (new input from the keyboard)
- {
- printf("Read from keyboard\n");
- int bytes = read(0, buf, BUF_SIZE);
- write(1, buf, bytes);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement