Chinaunix首页 | 论坛 | 博客
  • 博客访问: 343840
  • 博文数量: 72
  • 博客积分: 2130
  • 博客等级: 大尉
  • 技术积分: 857
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-05 16:10
文章分类

全部博文(72)

文章存档

2010年(5)

2009年(14)

2008年(53)

分类: LINUX

2009-07-09 16:45:22

server.c

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define MAX_BUF_SIZE 1024
#define MAX_LISTEN 12

int main(int argc, char *argv[])
{
    int sockfd, conn_fd;
    int client_fd[MAX_LISTEN];
    struct sockaddr_in my_addr, client_addr;
    unsigned int backlog, port;
    int sin_size;
    char buf[MAX_BUF_SIZE + 1];
    fd_set rfds;
    struct timeval tv;
    int retval, maxfd = -1;
    int i;

    if (argv[1])
        port = atoi(argv[1]);
    else
        port = 8003;

    if (argv[2])
        backlog = atoi(argv[2]);
    else
        backlog = 12;
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        fprintf(stderr, "socket error!\n");
        exit(1);
    }

    int on = 1;
    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
    {
        fprintf(stderr, "setsockopt failed: %s\n", strerror(errno));
        exit(1);
    }

    bzero(&my_addr, sizeof(my_addr));
    my_addr.sin_family = AF_INET;
    my_addr.sin_port = htons(port);
    if (argv[3])
        my_addr.sin_addr.s_addr = inet_addr(argv[3]);
    else
        my_addr.sin_addr.s_addr = INADDR_ANY;

    if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
    {
        fprintf(stderr, "bind error!\n");
        exit(1);
    }

    if (listen(sockfd, backlog) == -1)
    {
        fprintf(stderr, "listen error!\n");
        exit(1);
    }
    printf("*****wait new connection to new chat*****\n");

    for (i=0; i        client_fd[i] = -1;
    FD_ZERO(&rfds);
    maxfd = sockfd;

    while (1)
    {
        FD_SET(sockfd, &rfds);
        FD_SET(0, &rfds);

        tv.tv_sec = 1;
        tv.tv_usec = 0;

        if ((retval = select(maxfd + 1, &rfds, NULL, NULL, &tv)) < 0)
        {
            if (errno == EINTR)
            {
                fprintf(stdout, "closed by singal, continue...\n");
                continue;
            }
            else if (errno == EAGAIN)
            {
                continue;
            }
            else
            {
                fprintf(stderr, "select error!\n");
            }
        }
        else if (retval == 0)
        {
            for (i=0; i            {
                if (client_fd[i] == -1)
                    continue;
                FD_SET(client_fd[i], &rfds);
            }
            continue;
        }

        if (FD_ISSET(sockfd, &rfds))
        {
            sin_size = sizeof(struct sockaddr);
            conn_fd = accept(sockfd, (struct sockaddr *)&client_addr, &sin_size);
            if (conn_fd < 0)
            {
                if (errno == EINTR)
                    continue;
                else
                {
                    fprintf(stderr, "accept error!\n");
                    exit(1);
                }
            }

            for (i=0; i            {
                if(client_fd[i] != -1)
                    continue;
                client_fd[i] = conn_fd;
                FD_SET(client_fd[i], &rfds);
                break;
            }
            if (maxfd < conn_fd)
                maxfd = conn_fd;

            printf("Add client socket connection %d to select\n", conn_fd);
        }

        for (i=0; i        {
            if (client_fd[i] == -1)
                continue;
            if (!FD_ISSET(client_fd[i], &rfds))
            {
                FD_SET(client_fd[i], &rfds);
                continue;
            }

            bzero(buf, MAX_BUF_SIZE + 1);
            sin_size = recv(client_fd[i], buf, MAX_BUF_SIZE, 0);
            if (sin_size > 0)
            {
                printf("receive message from client %d successfully : [%s]\ntotal %d bytes\n", client_fd[i], buf, sin_size);

            }
            else
            {
                close(client_fd[i]);
                FD_CLR(client_fd[i], &rfds);
                printf("del socket %d from select pipe. \n", client_fd[i]);
                client_fd[i] = -1;
                continue;
            }
        }
        if (FD_ISSET(0, &rfds))
        {
            bzero(buf, MAX_BUF_SIZE + 1);
            fgets(buf, MAX_BUF_SIZE, stdin);
            if (!strncasecmp(buf, "quit", 4))
            {
                printf("quit chat!\n");
                break;
            }
            for (i = 0; i < MAX_LISTEN; i++)
            {
                if(client_fd[i] == -1)
                    continue;
                sin_size = send(client_fd[i], buf, strlen(buf)-1, 0);
                if (sin_size > 0)
                {
                    printf("message: %s\t send to client %d successfully, total %d bytes!\n", buf, client_fd[i], sin_size);
                }
                else
                {
                    printf("message: %s\t send failure, error code is %d, error message is %s\n",
                            buf, errno, strerror(errno));
                    break;
                }
            }
        }

        for (i=0; i        {
            if (maxfd < client_fd[i])
            {
                maxfd = client_fd[i];
            }
        }
    }
    close(sockfd);
    return 0;
}

client.c

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define MAX_BUF_SIZE 1024

int main(int argc, char *argv[])
{
    int sockfd;
    int sin_size;
    struct sockaddr_in dest;
    char buf[MAX_BUF_SIZE + 1];
    fd_set rfds;
    struct timeval tv;
    int retval, maxfd = -1;

    if (argc != 3)
    {
        printf("usage: %s ip port\n for example: %s 127.0.0.1 8003\n", argv[0], argv[0]);
        exit(1);
    }

    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        fprintf(stderr, "socket error!\n");
        exit(1);
    }
    bzero(&dest, sizeof(dest));
    dest.sin_family = AF_INET;
    dest.sin_port = htons(atoi(argv[2]));
    if (inet_aton(argv[1], (struct in_addr *)&dest.sin_addr.s_addr) == 0)
    {
        fprintf(stderr, "%s error\n", argv[1]);
        exit(1);
    }

    if (connect(sockfd, (struct sockaddr *)&dest, sizeof(struct sockaddr)) == -1)
    {
        fprintf(stderr, "connect error!\n");
        exit(1);
    }

    printf("Be ready, can chat!\n");

    FD_ZERO(&rfds);
    maxfd = sockfd;

    while (1)
    {
        FD_CLR(sockfd, &rfds);
        FD_SET(sockfd, &rfds);
        FD_SET(0, &rfds);

        tv.tv_sec = 1;
        tv.tv_usec = 0;
        if ((retval = select(maxfd + 1, &rfds, NULL, NULL, &tv)) < 0)
        {
            if (errno == EINTR)
            {
                fprintf(stdout, "closed by signal, continue...\n");
                continue;
            }
            else if (errno == EAGAIN)
                    continue;
            }
            else
            {
                fprintf(stderr, "select error!\n");
                break;
            }
        }
        else if (retval == 0)
        {
            continue;
        }

        if (FD_ISSET(sockfd, &rfds))
        {
            bzero(buf, MAX_BUF_SIZE + 1);
            sin_size = recv(sockfd, buf, MAX_BUF_SIZE, 0);
            if (sin_size > 0)
            {
                printf("receive message from server successfully: [%s]\ntotal %d bytes\n", buf, sin_size);
            }
            else
            {
                if (sin_size < 0)
                    printf("receive message failure! error code is %d, error message is %s\n",
                           errno, strerror(errno));
            }
        }
       
        if (FD_ISSET(0, &rfds))
        {
            bzero(buf, MAX_BUF_SIZE + 1);
            fgets(buf, MAX_BUF_SIZE, stdin);
            if (!strncasecmp(buf, "quit", 4))
            {
                printf("quit chat!\n");
                break;
            }
            sin_size = send(sockfd, buf, strlen(buf) - 1, 0);
            if (sin_size < 0)
            {
                printf("message send failure: [%s], error code is %d, error message is %s\n",
                       buf, errno, strerror(errno));
                break;
            }
            else
            {
                printf("message: %s\tsend to server successfully, total %d bytes\n", buf, sin_size);
            }
        }
    }
    close(sockfd);
    return 0;
}

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

chinaunix网友2009-11-19 15:25:37

很好,学习了