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

全部博文(72)

文章存档

2010年(5)

2009年(14)

2008年(53)

分类: LINUX

2009-07-05 14:18:42

server.c

#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];

    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);
        while (1)
        {
            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
            {
                printf("receive message failure: %s\nerror code is %d \nerror message is %s\n",
                       buf, errno, strerror(errno));
                break;
            }

            bzero(buf, MAX_BUF_SIZE + 1);
            printf("please input message that will send to other side : ");
            fgets(buf, MAX_BUF_SIZE, stdin);
            if (!strncmp(buf, "quit", 4))
                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;
            }
        }

        close(conn_fd);
    }

    close(sockfd);
    return 0;
}

client.c

#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];

    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("server connected!\n");

    while (1)
    {
        memset(buf, 0, MAX_BUF_SIZE +1);
        printf("please input message that will send to other side: ");
        fgets(buf, MAX_BUF_SIZE, stdin);
        if (!strncmp(buf, "quit", 4))
        {
            printf("quit chat!\n");
            break;
        }

        sin_size = send(sockfd, 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;
        }
        memset(buf, 0, 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:  %s\nerror code is %d \nerror message is %s\n",
                       buf, errno, strerror(errno));
            else
                printf("quit chat!\n");
            break;
        }
    }

    close(sockfd);
    return 0;
}

编译
gcc server.c -o server
gcc client.c -o client
运行
./server 8003 1
./client 127.0.0.1 8003
阅读(867) | 评论(0) | 转发(0) |
0

上一篇:ubuntu中pdf乱码

下一篇:异步聊天程序

给主人留下些什么吧!~~