Chatting server using epoll - unexpected behavior

914 views Asked by At

I'm working on writing a simple chatting server-client C program using epoll. I'm writing and testing it in Xubuntu, and I found something strange. Message from server to client is delivered well. But Message from client to server is delivered at the moment when I push enter key at 'SERVER SIDE(putty window)' For example, if I type 'Hello World' in the client window and push enter key, the message doesn't show on the server side, however, when I push enter key on the server side, 'Hello World' shows on the window! I think something is wrong in server.c but I can't find what's wrong. I used epoll and let it monitor EPOLLIN|EPOLLOUT to receive and send messages. In other hand, I wrote client program with multi-threading. Full src are below without header. Any comment will be appreciated. Thank you!

server.c

int main(int argc, char *argv[])
{
    int serv_sock, clnt_sock;
    struct sockaddr_in serv_adr, clnt_adr;
    socklen_t adr_sz;
    int str_len, i;
    char buf[BUF_SIZE];

    struct epoll_event *ep_events;
    struct epoll_event event;
    int epfd, event_cnt;

    if(argc!=2) {
        printf("Usage : %s <port>\n", argv[0]);
        exit(1);
    }

    serv_sock=socket(PF_INET, SOCK_STREAM, 0);
    memset(&serv_adr, 0, sizeof(serv_adr));
    serv_adr.sin_family=AF_INET;
    serv_adr.sin_addr.s_addr=htonl(INADDR_ANY);
    serv_adr.sin_port=htons(atoi(argv[1]));

    if(bind(serv_sock, (struct sockaddr*) &serv_adr, sizeof(serv_adr))==-1)
        error_handling("bind() error");
    if(listen(serv_sock, 5)==-1)
        error_handling("listen() error");

    epfd=epoll_create(EPOLL_SIZE);
    ep_events=malloc(sizeof(struct epoll_event)*EPOLL_SIZE);

    //set_nonblocking_mode(serv_sock);
    event.events=EPOLLIN;
    event.data.fd=serv_sock;    
    epoll_ctl(epfd, EPOLL_CTL_ADD, serv_sock, &event);

    while(1) {
        event_cnt=epoll_wait(epfd, ep_events, EPOLL_SIZE, -1);
        if(event_cnt==-1) {
            puts("epoll_wait() error");
            break;
        }

        for(i=0; i<event_cnt; i++) {
            if(ep_events[i].data.fd==serv_sock) {
                adr_sz=sizeof(clnt_adr);
                clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_adr, &adr_sz);
                //set_nonblocking_mode(clnt_sock);
                event.events=EPOLLIN|EPOLLOUT;
                event.data.fd=clnt_sock;
                epoll_ctl(epfd, EPOLL_CTL_ADD, clnt_sock, &event);
                printf("connected client: %d \n", clnt_sock);
            }
            else {
                if((ep_events[i].events & EPOLLIN) == EPOLLIN) {
                    memset(buf, 0, BUF_SIZE);
                    str_len=read(ep_events[i].data.fd, buf, BUF_SIZE-1);
                    buf[str_len] = 0;
                    if(str_len==0) {    // close request!
                        epoll_ctl(epfd, EPOLL_CTL_DEL, ep_events[i].data.fd, NULL);
                        close(ep_events[i].data.fd);
                        printf("closed client: %d \n", ep_events[i].data.fd);
                    }
                    /*
                    else if(str_len < 0) {
                        if(errno == EAGAIN)
                            break;
                    }*/
                    else {
                        fputs(buf, stdout);
                    }
                }

                if((ep_events[i].events & EPOLLOUT) == EPOLLOUT) {
                    fgets(buf, BUF_SIZE, stdin);
                    if(!strcmp(buf, "q\n") || !strcmp(buf, "Q\n")) {
                        epoll_ctl(epfd, EPOLL_CTL_DEL, ep_events[i].data.fd, NULL);
                        close(ep_events[i].data.fd);
                        printf("closed client: %d \n", ep_events[i].data.fd);
                    }
                    else {
                        write(ep_events[i].data.fd, buf, strlen(buf));
                    }   
                }
            }
        }
    }
    close(serv_sock);
    close(epfd);
    return 0;
}

void set_nonblocking_mode(int fd)
{
    int flag = fcntl(fd, F_GETFL, 0);
    fcntl(fd, F_SETFL, flag|O_NONBLOCK);
}

void error_handling(char *buf)
{
    fputs(buf, stderr);
    fputc('\n', stderr);
    exit(1);
}

client.c

void * send_msg(void * arg);
void * recv_msg(void * arg);
void error_handling(char * msg);

char name[NAME_SIZE]="[DEFAULT]";
char msg[BUF_SIZE];

int main(int argc, char *argv[])
{
    int sock;
    struct sockaddr_in serv_addr;
    pthread_t snd_thread, rcv_thread;
    void * thread_return;
    if(argc!=4) {
        printf("Usage : %s <IP> <port> <name>\n", argv[0]);
        exit(1);
     }

    sprintf(name, "[%s]", argv[3]);
    sock=socket(PF_INET, SOCK_STREAM, 0);

    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family=AF_INET;
    serv_addr.sin_addr.s_addr=inet_addr(argv[1]);
    serv_addr.sin_port=htons(atoi(argv[2]));

    if(connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr))==-1)
        error_handling("connect() error");

    pthread_create(&snd_thread, NULL, send_msg, (void*)&sock);
    pthread_create(&rcv_thread, NULL, recv_msg, (void*)&sock);
    pthread_join(snd_thread, &thread_return);
    pthread_join(rcv_thread, &thread_return);
    close(sock);  
    return 0;
}

void * send_msg(void * arg)   // send thread main
{
    int sock=*((int*)arg);
    char name_msg[NAME_SIZE+BUF_SIZE];
    while(1) 
    {
        fgets(msg, BUF_SIZE, stdin);
        if(!strcmp(msg,"q\n")||!strcmp(msg,"Q\n")) 
        {
            close(sock);
            exit(0);
        }
        sprintf(name_msg,"%s %s", name, msg);
        write(sock, name_msg, strlen(name_msg));
    }
    return NULL;
}

void * recv_msg(void * arg)   // read thread main
{
    int sock=*((int*)arg);
    char name_msg[NAME_SIZE+BUF_SIZE];
    int str_len;
    while(1)
    {
        str_len=read(sock, name_msg, NAME_SIZE+BUF_SIZE-1);
        if(str_len==-1) 
            return (void*)-1;
        name_msg[str_len]=0;
        fputs(name_msg, stdout);
    }
    return NULL;
}

void error_handling(char *msg)
{
    fputs(msg, stderr);
    fputc('\n', stderr);
    exit(1);
}
0

There are 0 answers