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

全部博文(38)

文章存档

2011年(1)

2009年(37)

我的朋友

分类: LINUX

2009-07-18 16:56:40

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

#define SERV_ADDR "127.0.0.1"
#define SERV_PORT 5358
#define BUF_LEN 1024

#define MAX(a, b) (a)>(b)?(a):(b)

struct thread_arg {
        FILE *fp;
        int sockfd;
};

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 *thread_func(void *arg)
{
    struct thread_arg *fds;
    char sendbuf[BUF_LEN];

    fds = (struct thread_arg *)arg;
    while(fgets(sendbuf, BUF_LEN, fds->fp) != NULL) {
        writen(fds->sockfd, sendbuf, strlen(sendbuf));
        bzero(sendbuf, BUF_LEN);
    }
    shutdown(fds->sockfd, SHUT_WR);
    free(fds);
    return NULL;
}

void str_cli(FILE *fp, int sockfd)
{
    pthread_t tid;
    char recvbuf[BUF_LEN];
    struct thread_arg *fds;
    
    fds = (struct thread_arg *)malloc(sizeof(struct thread_arg));
    fds->fp = fp;
    fds->sockfd = sockfd;
    if(pthread_create(&tid, NULL, thread_func, (void *)fds) != 0) {
        printf("create thread failed!\n");
        return;
    }

    while(read(sockfd, recvbuf, BUF_LEN) > 0) {
        fputs(recvbuf, stdout);
        bzero(recvbuf, BUF_LEN);
    }
    return;
}

int main(int argc, char **argv)
{
    int fd;
    struct sockaddr_in servaddr;

    fd = socket(AF_INET, SOCK_STREAM, 0);

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = inet_addr(SERV_ADDR);
    servaddr.sin_port = htons(SERV_PORT);

    if (connect(fd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) {
        printf("connect error: %s\n", strerror(errno));
        return 0;
    }

    str_cli(stdin, fd);
    return 0;
}

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