免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 924 | 回复: 0
打印 上一主题 下一主题

linux下简单的聊天程序(多路复用I/O) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-07-09 16:45 |只看该作者 |倒序浏览
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)
            {
                printf("receive message from client %d successfully : [%s]\ntotal %d bytes\n", client_fd, buf, sin_size);
            }
            else
            {
                close(client_fd);
                FD_CLR(client_fd, &rfds);
                printf("del socket %d from select pipe. \n", client_fd);
                client_fd = -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  0)
                {
                    printf("message: %s\t send to client %d successfully, total %d bytes!\n", buf, client_fd, 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
#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)
            {
                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;
}
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/78437/showart_1990894.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP