Chinaunix首页 | 论坛 | 博客
  • 博客访问: 220945
  • 博文数量: 38
  • 博客积分: 2060
  • 博客等级: 大尉
  • 技术积分: 388
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-17 10:10
文章分类

全部博文(38)

文章存档

2011年(1)

2009年(37)

我的朋友

分类: LINUX

2009-07-18 16:54:43

#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>

#define SERV_PORT 5358
#define MAX_CONN 10
#define BUF_LEN 1024

ssize_t readn(int fd, void *vptr, size_t n)
{
    ssize_t nread;
    size_t nleft;
    char *ptr;

    ptr = vptr;
    nleft = n;
    while(nleft > 0){
        if((nread = read(fd, ptr, nleft)) < 0){
            if(errno == EINTR){        /*interrupt by signal*/
                nread = 0;
            }
            else{
                return -1;
            }
        } else if(nread == 0){        /*EOF*/
            break;
        }
        nleft -= nread;
        ptr += nread;
    }
    return (n-nleft);
}

ssize_t writen(int fd, const void *vptr, size_t n)
{
    size_t nleft;
    ssize_t nwriten;
    const char *ptr;

    ptr = vptr;
    nleft = n;
    while(nleft>0) {
        if((nwriten = write(fd, ptr, nleft)) <= 0) {
            if(nwriten < 0 && errno == EINTR) {
                nwriten = 0;        /*interrupt by signal*/
            } else {
                return -1;
            }
        }

        nleft -= nwriten;
        ptr += nwriten;
    }
    return n;
}

void str_echo(int sockfd){
    ssize_t nread;
    char buf[BUF_LEN] = {0};
    while(1) {
        bzero(buf, BUF_LEN);
        if((nread = read(sockfd, buf, BUF_LEN)) == -1) {
            if(errno == EINTR) {
                continue;
            }
            else {
                printf("readn error: %s\n", strerror(errno));
                continue;
            }
        }
        else if (nread == 0) {
            break;
        }
        else {
            fputs(buf, stdout);
            write(sockfd, buf, nread);
        }
    }    
}

void *thread_func(void *arg)
{
    int connfd;
    connfd = *((int *)arg);
    free(arg);
    pthread_detach(pthread_self());
    str_echo(connfd);
    close(connfd);
    return NULL;
}

int main(int argc, char **argv)
{
    int listenfd, *connfdptr;
    socklen_t cliaddrlen;
    pthread_t tid;
    struct sockaddr_in servaddr, cliaddr;

    if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
        printf("socket error: %s\n", strerror(errno));
        return 0;
    }

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(SERV_PORT);
    if(bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1){
        printf("bind error: %s\n", strerror(errno));
        return 0;
    }

    if(listen(listenfd, MAX_CONN) == -1){
        printf("listen error: %s\n", strerror(errno));
        return 0;
    }

    //cliaddr = (struct sockaddr *)malloc(sizeof(struct sockaddr));

    while(1){
        cliaddrlen = sizeof(cliaddr);
        connfdptr = malloc(sizeof(int));
        if((*connfdptr = accept(listenfd, (struct sockaddr *)&cliaddr, &cliaddrlen)) == -1){
            if(errno == EINTR){
                continue;
            }
            else{
                printf("accept error: %s\n", strerror(errno));
                continue;
            }
        }
        pthread_create(&tid, NULL, thread_func, connfdptr);
    }
}

阅读(1450) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~