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

全部博文(72)

文章存档

2010年(5)

2009年(14)

2008年(53)

分类: LINUX

2009-07-05 16:20:00

server.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, conn_fd;
    struct sockaddr_in my_addr, their_addr;
    unsigned int backlog, port;
    int sin_size;
    char buf[MAX_BUF_SIZE + 1];
    fd_set rfds;
    struct timeval tv;
    int retval, maxfd = -1;

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

    if (argv[2])
        backlog = atoi(argv[2]);
    else
        backlog = 2;
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        fprintf(stderr, "socket error!\n");
        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);
    }

    while (1)
    {
        printf("\n***** wait new connection to new chat *****\n");

        sin_size = sizeof(struct sockaddr);
        if ((conn_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1)
        {
            fprintf(stderr, "accept error!\n");
            exit(1);
        }
        else
            printf("\n***** server: got connection from %s, port %d, socket %d *****\n",
                 inet_ntoa(their_addr.sin_addr), ntohs(their_addr.sin_port), conn_fd);

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

        while (1)
        {
            FD_ZERO(&rfds);
            FD_SET(0, &rfds);
            FD_SET(conn_fd, &rfds);
            maxfd = 0;
            if (conn_fd > maxfd)
                maxfd = conn_fd;

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

            retval = select(maxfd + 1, &rfds, NULL, NULL, &tv);
            if (retval == -1)
            {
                printf("select error: %s", strerror(errno));
                break;
            }
            else if (retval == 0)
            {
                /* printf("no message arrived, user do not press key, keep waiting...\n"); */
                continue;
            }
            else
            {
                if (FD_ISSET(0, &rfds))
                {
                    /* user press key, data will be sent out */
                    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(conn_fd, buf, strlen(buf)-1, 0);
                    if (sin_size > 0)
                    {
                        printf("message: %s\t send successfully, total %d bytes!\n", buf, sin_size);
                    }
                    else
                    {
                        printf("message: %s\t send failure, error code is %d, error message is %s\n",
                               buf, errno, strerror(errno));
                        break;
                    }
                }
                if (FD_ISSET(conn_fd, &rfds))
                {
                    bzero(buf, MAX_BUF_SIZE + 1);
                    sin_size = recv(conn_fd, buf, MAX_BUF_SIZE, 0);
                    if (sin_size > 0)
                    {
                        printf("receive message 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));
                        else
                            printf("quit chat!\n");
                        break;
                    }
                }
            }
        }
        close(conn_fd);
        printf("any chat?(no -> quit)");
        fflush(stdout);
        bzero(buf, MAX_BUF_SIZE + 1);
        fgets(buf, MAX_BUF_SIZE, stdin);
        if (!strncasecmp(buf, "no", 2))
        {
            printf("quit chat!\n");
            break;
        }
    }
    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");

    while (1)
    {
        FD_ZERO(&rfds);
        FD_SET(0, &rfds);
        FD_SET(sockfd, &rfds);
        maxfd = 0;
        if (sockfd > maxfd)
            maxfd = sockfd;

        tv.tv_sec = 1;
        tv.tv_usec = 0;
        retval = select(maxfd + 1, &rfds, NULL, NULL, &tv);
        if (retval == -1)
        {
            printf("select error: %s", strerror(errno));
            break;
        }
        else if (retval == 0)
        {
            /* no message arrived, user do not press key, keep waiting...\n"); */
            continue;
        }
        else
        {
            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 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));
                    else
                        printf("quit chat!\n");
                    break;
                }
            }
            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 successfully, total %d bytes\n", buf, sin_size);
                }
            }
        }
    }
    close(sockfd);
    return 0;
}

编译
gcc server.c -o server
gcc client.c -o client
运行
./server 8003 1
./client 127.0.0.1 8003
阅读(863) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~