| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1661 人关注过本帖
标题:多路复用的聊天工具小代码
取消只看楼主 加入收藏
遗矢的老人
Rank: 9Rank: 9Rank: 9
来 自:成都
等 级:蜘蛛侠
威 望:7
帖 子:325
专家分:1131
注 册:2012-7-20
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:7 
多路复用的聊天工具小代码
服务端:
myhead.h
#ifndef _LIST_
#define _LIST_

#include <stdio.h>
#include <stdlib.h>      
#include <sys/types.h>      
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/select.h>
#include<strings.h>

typedef int data_t;
typedef struct node{
    data_t data;
    struct node *next;
}NODE;

NODE *creat_node(data_t data);
void insert(NODE *head, data_t data);
void _delete(NODE *head, data_t data);
void show_link(NODE *head);

#endif

list.c   //主要拿来存登陆用户的文件描述符

#include "myhead.h"

NODE *creat_node(data_t data)
{
    NODE *link_node = (NODE *)malloc(sizeof(NODE));
    if( NULL == link_node )
        exit(-1);
    link_node->data = data;
    link_node->next = NULL;
    return link_node;
}

void insert(NODE *head, data_t data)
{
    NODE *link_node = creat_node(data);
    while(NULL != head->next)
    {
        head = head->next;
    }
    head->next = link_node;
}

void _delete(NODE *head, data_t data)
{   
    NODE *p =NULL;
    while( head->next )
    {
        p = head;
        head = head->next;
        if( data == head->data )
        {
            p->next = head->next;
            free(head);
            return;
        }
    }
    printf("%d outwith the link!\n", data);   
}

void show_link(NODE *head)
{
    while( head->next )
    {   
        head = head->next;
        printf("Connected ID:%d\t", head->data);
    }
    printf("\n");
}

server.c
#include "myhead.h"

int main()
{
    int listenfd,connfd,maxfd,i,nbyte;
    struct sockaddr_in myaddr, cliaddr;
    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    fd_set global_rdfs,current_rdfs;
    char buf[10] = {0};
    if (0 > listenfd)
    {
        perror("socket");
        exit(-1);
    }
    int ii = 1;
    int sres = setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &ii, sizeof(ii));
    if (0 > sres)
    {
        printf("setsockopt error\n");
        exit(-1);
    }

    memset(&myaddr, 0, sizeof(myaddr));
    myaddr.sin_family = AF_INET;
    myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    myaddr.sin_port = htons(8888);
    if(0 > bind(listenfd, (struct sockaddr *)&myaddr, sizeof(myaddr)))
    {
        perror("bind");
        exit(-1);
    }   
    listen(listenfd, 5);
    printf("listening...\n");
   

    FD_ZERO(&global_rdfs);
    FD_SET(listenfd, &global_rdfs);
    FD_SET(0, &global_rdfs);
    maxfd = listenfd;
    char send_buf[BUFSIZ] = {0};
    char recv_buf[BUFSIZ] = {0};
    int len = sizeof(cliaddr);
    int size = 0;
    NODE *head = creat_node(-1);
    while(1)
    {
        current_rdfs = global_rdfs;
        if(0 > select(maxfd + 1, &current_rdfs, NULL, NULL, 0))
        {
            perror("select");
            exit(-1);
        }
        else
        {
            for (i = 0; i <= maxfd; ++i)
            {
                if(FD_ISSET(i, &current_rdfs))
                {
                    if(i == listenfd)
                    {
                        connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &len);
                        if(0 > connfd)
                        {
                            perror("accept");
                            exit(-1);
                        }
                        printf("Connection from %s\tport %d\t ID:%d\n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port), connfd);
                        send(connfd, "welcome to server!", 30, 0);
                        FD_SET(connfd, &global_rdfs);
                        maxfd = maxfd > connfd ? maxfd : connfd;
                        insert(head, connfd);
                    }
                    else if(0 == i)
                    {   
                        printf("useage:id:msg\n");
                        show_link(head);
                        fgets(send_buf, sizeof(send_buf), stdin);
                        if(!strncmp(send_buf, "quit", 4))
                        {
                            printf("goodbye!\n");
                            exit(-1);
                        }
                        char *p = strstr(send_buf, ":");
                        size = p - send_buf + 1;
                        snprintf(buf, size, "%s", send_buf);
                        send(atoi(buf), send_buf + size, strlen(send_buf) - size, 0);
                    }
                    else
                    {
                        if(0 >= (nbyte= recv(i, recv_buf, sizeof(recv_buf), 0)))
                        {
                            _delete(head,i);
                            close(i);
                            FD_CLR(i, &global_rdfs);
                        }
                        else
                            printf("recv%d:%s\n", i, recv_buf);
                        
                    }
                }
            }
        }
    }
    return 0;
}

用户端:
client.c

#include <myhead.h>
#define MAXSIZE 1024

int main(int argc, char *argv[])  //带命令行ip
{
    if(2 != argc)
    {
        printf("useage:%s + server's ip\n");
        exit(-1);
    }
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(0 > sockfd)
    {
        perror("socket");
        exit(-1);
    }
   
    struct sockaddr_in servaddr;
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = inet_addr(argv[1]);
    servaddr.sin_port = htons(8888);
   
    char buf[MAXSIZE] = {0};   
        
    connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
    recv(sockfd, buf, sizeof(buf), 0);
    puts(buf);
   
    int i;
    fd_set readfd;
    int maxfd = -1;
    char recv_buf[MAXSIZE] = {0};
    char send_buf[MAXSIZE] = {0};
    while(1)
    {   
        FD_ZERO(&readfd);
        FD_SET(0, &readfd);
        maxfd = maxfd > 0 ? maxfd : 0;
        FD_SET(sockfd, &readfd);
        maxfd = maxfd > sockfd ? maxfd : sockfd;
        if(0 > select(maxfd + 1, &readfd, NULL, NULL, NULL))
        {
            perror("select");
            exit(-1);
        }

        for(i=0; i<=maxfd; i++)
        {
            if(FD_ISSET(i, &readfd))
            {
                if(i == sockfd)
                {
                    if(0 >= recv(sockfd, recv_buf, sizeof(recv_buf), 0))
                        exit(-1);
                    printf("recv:%s", recv_buf);
                    memset(recv_buf, 0, sizeof(recv_buf));
                }
                if(0 == i)
                {
                    fgets(send_buf, sizeof(send_buf), stdin);
                    send(sockfd, send_buf, strlen(send_buf) + 1, 0);
                    memset(send_buf, 0, sizeof(send_buf));
                }
            }
        }
    }
    return 0;
}
小弟socket编程差劲,目前写的只能1对多聊天,再准备写个好点的多对多聊天代码,各位大侠给点建议,不要多进程的,那个很浪费资源

[ 本帖最后由 遗矢的老人 于 2012-8-23 00:56 编辑 ]
搜索更多相关主题的帖子: void next include 聊天工具 insert 
2012-08-23 00:37
遗矢的老人
Rank: 9Rank: 9Rank: 9
来 自:成都
等 级:蜘蛛侠
威 望:7
帖 子:325
专家分:1131
注 册:2012-7-20
收藏
得分:0 
回复 2楼 madfrogme
send(atoi(buf), send_buf + size, strlen(send_buf) - size, 0); 在终端输入的是文件描述符 fd + msg(即4:msg),但fd是字符,先提取出字符fd,用atoi转换成int型也就是文件描述符fd,size是这个(fd + : )的长度,send_buf + size 就是 msg啦
2012-08-23 15:22
遗矢的老人
Rank: 9Rank: 9Rank: 9
来 自:成都
等 级:蜘蛛侠
威 望:7
帖 子:325
专家分:1131
注 册:2012-7-20
收藏
得分:0 
服务端:
myhead.h                                   //头文件
#ifndef _LIST_
#define _LIST_   
 
#include <stdio.h>
#include <stdlib.h>      
#include <sys/types.h>        
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/select.h>
#include<strings.h>
 
typedef int data_t;
typedef struct node{                           //结构体,成员fd是装文件描述符file discretion
    data_t fd;
    data_t id;                                // id就是IDentity,这儿是用户登录时服务端给他分配一个ID号,没存在外部文件,所以只有客户退出连接就无效
    struct node *next;
}NODE;
 
NODE *creat_node(data_t fd, data_t id);                 //创建链表结点
void insert(NODE *head, data_t fd, data_t id);          //插入fd和id
NODE *find_fd(NODE *head, data_t id);                   //根据id找fd
NODE *find_id(NODE *head, data_t fd);                  //根据fd找id
void delete_fd(NODE *head, data_t fd);                 //用户退出时删除含fd节点
void show_id(NODE *head);                              //服务器浏览已连接的id,使用list命令
 
#endif
 
list.c   //具体的函数实现
 
#include "myhead.h"
 
NODE *creat_node(data_t fd , data_t id)
{
    NODE *link_node = (NODE *)malloc(sizeof(NODE));
    if( NULL == link_node )
        exit(-1);
    link_node->fd = fd;
    link_node->id = id;
    link_node->next = NULL;
    return link_node;
}
 
void insert(NODE *head, data_t fd, data_t id)
{
    NODE *link_node = creat_node(fd, id);
    while(NULL != head->next)
    {
        head = head->next;
    }
    head->next = link_node;
}
 
NODE *find_fd(NODE *head, data_t id)
{     
    while(head->next)
    {
        if(id == head->next->id)
            return head->next;
        head = head->next;
    }
    return NULL;
}
 
NODE *find_id(NODE *head, data_t fd)
{
    while(head->next)
    {
        if(fd == head->next->fd)
            return head->next;
        head = head->next;
    }
    return NULL;
}
 
void delete_fd(NODE *head, data_t fd)
{   
    NODE *p =NULL;
    while( head->next )
    {
        p = head;
        head = head->next;
        if( fd == head->fd )
        {
            p->next = head->next;
            free(head);
            return;
        }
    }
    printf("%d outwith the link!\n", fd);     
}
 
void show_id(NODE *head)
{
    printf("========================================================================\n");
    while( head->next )
    {   
        head = head->next;
         
        printf("Connected ID:%d\t", head->id);
    }
    printf("\n");
}
server.c
 
#include "myhead.h"
 
int main()
{
    int listenfd,connfd,maxfd,i,nbyte;                        //listenfd 是socket file discretion  connfd是客户连接文件描述符
    struct sockaddr_in myaddr, cliaddr;                        //sockaddr_in 是内核结构体,具体在man 7 ip
    listenfd = socket(AF_INET, SOCK_STREAM, 0);                 //创建打开ipv4内型TCP套接字文件
    fd_set global_rdfs,current_rdfs;  
    char buf[10] = {0};
    if (0 > listenfd)
    {
        perror("socket");
        exit(-1);
    }
    int ii = 1;
    int sres = setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &ii, sizeof(ii));      //设置listenfd的属性,为IO复用型
    if (0 > sres)
    {
        printf("setsockopt error\n");
        exit(-1);
    }
 
    memset(&myaddr, 0, sizeof(myaddr));                                       //设置服务器分ip 及端口,内核的宏在man 7 ip 中查询
    myaddr.sin_family = AF_INET;                                             //   网络内型ipv4
    myaddr.sin_addr.s_addr = htonl(INADDR_ANY);                              //ip让内核取
    myaddr.sin_port = htons(8888);                                           //端口号为8888
    if(0 > bind(listenfd, (struct sockaddr *)&myaddr, sizeof(myaddr)))        //绑定ip及端口
    {
        perror("bind");
        exit(-1);
    }     
    listen(listenfd, 5);                                             //一次听五个客服连接,可以多设
    printf("listening...\n");
     
 
    FD_ZERO(&global_rdfs);                                         //global清零
    FD_SET(listenfd, &global_rdfs);                               //listenfd设为监听
    FD_SET(0, &global_rdfs);                                     //stdin设为监听
    maxfd = listenfd;                                            //文件描述符的最大值
    char send_buf[BUFSIZ] = {0};
    char recv_buf[BUFSIZ] = {0};
    int len = sizeof(cliaddr);
    int size = 0;
    int id = 0;
    NODE *head = creat_node(-1, 0);                              //链表头结点初始化
    while(1)
    {
        current_rdfs = global_rdfs;
        if(0 > select(maxfd + 1, &current_rdfs, NULL, NULL, 0))  //监听已设好的文件描述符
        {
            perror("select");
            exit(-1);
        }
        else
        {
            for (i = 0; i <= maxfd; ++i)
            {
                if(FD_ISSET(i, &current_rdfs))
                {
                    if(i == listenfd)
                    {
                        connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &len);                                         //监听到有新客户连接
                        if(0 > connfd)
                        {
                            perror("accept");
                            exit(-1);
                        }
                        id++;
                        printf("Connection from %s\tport %d\t ID:%d\n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port), id); //把刚连接新客户的ip 和端口号打印子在服务端
                        char buf1[100] = "Welcome to server! you ID:";
                        char buf2[10] = {0};
                        snprintf(buf2, sizeof(data_t), "%d", id);                // 把id转化成字符串
                        strncat(buf1, buf2, strlen(buf2));                     //把字符id接在welcome to server后面
                        send(connfd, buf1, strlen(buf1), 0);                  //把给客户分配id发个客户
                        FD_SET(connfd, &global_rdfs);                         //把新增的文件描述符加入监听
                        maxfd = maxfd > connfd ? maxfd : connfd;             //取最大的文件描述符
                        insert(head, connfd, id);                            //把新增的文件描述符和id加在链表
                    }
                    else if(0 == i)
                    {     
                        fgets(send_buf, sizeof(send_buf), stdin);         //监听到标准输入有数据
                        if(!strncmp(send_buf, "quit", 4))                 //如果输入quit则退出服务器
                        {   
                            printf("goodbye!\n");
                            exit(-1);
                        }
                        if(!strncmp(send_buf, "list", 4))                 //输入list则显示在线客户id
                        {
                            show_id(head);
                            continue;
                        }
                        if(!strncmp(send_buf, "BC@", 3))                   //广播 标致《BC@》
                        {   
                            NODE *p = head;
                            while(NULL != p->next)
                            {     
                                send(p->next->fd, send_buf + 3, strlen(send_buf) - 2, 0);
                                p = p->next;
                            }
                            continue;
                        }     
 
                        char *p = strstr(send_buf, "@");                     //输入中检测@符
                        if (!p)                                              //没检测到上面的情况,则提醒输入格式
                        {
                            printf("useage:<id@msg>\n");
                            continue;
                        }
                        size = p - send_buf + 1;                           //检测到@符后把@之前的字符id长度算出为size
                        snprintf(buf, size, "%s", send_buf);              //剪取字符id放在buf
                        if(NULL == find_fd(head, atoi(buf)))              //atoi(buf)把字符id转化成int型,去找文件描述符fd
                        {
                            printf("The user doesn't have online\n");      //检测这个id是否在链表里,没在就提醒此id没在线
                            continue;
                        }
                        send(find_fd(head, atoi(buf))->fd, send_buf + size, strlen(send_buf) - size, 0);  //排除以上,在线id,把@后面的msg发给此id
                    }
                    else
                    {
                        if(0 >= (nbyte= recv(i, recv_buf, sizeof(recv_buf), 0)))                         //监听到接收端有消息
                        {
                            delete_fd(head,i);                                                            //id下线情况,则从链表中删除此NODE
                            close(i);                                                                     //关闭文件描述符
                            FD_CLR(i, &global_rdfs);                                                      //不再监听此fd
                        }
                        else
                            printf("recv<ID:%d>msg:\n%s", find_id(head, i)->id, recv_buf);              //接收到有msg情况,id号标志打印在终端
                    }
                }
            }
        }
    }
    return 0;
}
再修改了下 id 和 fd 分开,之前表示抱歉,我一些完,运行下没注释就发上来,客户端注释差不多

[ 本帖最后由 遗矢的老人 于 2012-8-24 11:28 编辑 ]
2012-08-23 15:30
遗矢的老人
Rank: 9Rank: 9Rank: 9
来 自:成都
等 级:蜘蛛侠
威 望:7
帖 子:325
专家分:1131
注 册:2012-7-20
收藏
得分:0 
回复 5楼 madfrogme
我写完就发上来了,就是没时间解释,下来我会解释的,你们给点意见啊,我准备写个多对多聊天的
2012-08-23 19:51
遗矢的老人
Rank: 9Rank: 9Rank: 9
来 自:成都
等 级:蜘蛛侠
威 望:7
帖 子:325
专家分:1131
注 册:2012-7-20
收藏
得分:0 
回复 8楼 madfrogme
谢谢
2012-08-24 00:34
遗矢的老人
Rank: 9Rank: 9Rank: 9
来 自:成都
等 级:蜘蛛侠
威 望:7
帖 子:325
专家分:1131
注 册:2012-7-20
收藏
得分:0 
回复 6楼 zklhp
帮忙顶下
2012-08-24 00:45
遗矢的老人
Rank: 9Rank: 9Rank: 9
来 自:成都
等 级:蜘蛛侠
威 望:7
帖 子:325
专家分:1131
注 册:2012-7-20
收藏
得分:0 
基于ipv4网络类型的TCP协议的多路复用多对多聊天工具:
服务端:
myhead.h
#ifndef _MYFUCTION_
#define _MYFUCTION_   

#include <stdio.h>
#include <stdlib.h>      
#include <sys/types.h>        
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/select.h>
#include <strings.h>

#define SIZE 1024

typedef int data_t;
typedef struct node{                            //结构体,成员fd是装文件描述符file discretion
    data_t fd;
    data_t id;                                // id就是IDentity,这儿是用户登录时服务端给他分配一个ID号,没存在外部文件,所以只有客户退出连接就无效
    struct node *next;
}NODE;

NODE *creat_node(data_t fd, data_t id);                 //创建链表结点
void insert(NODE *head, data_t fd, data_t id);          //插入fd和id
NODE *find_fd(NODE *head, data_t id);                   //根据id找fd
NODE *find_id(NODE *head, data_t fd);                  //根据fd找id
void delete_fd(NODE *head, data_t fd);                 //用户退出时删除含fd节点
void show_id(NODE *head);                              //服务器浏览已连接的id,使用list命令
void broadcast(NODE *head, char buf[]);                //广播函数
void send_online_id(NODE *head , data_t fd);           //给相应的id转发信息函数

#endif

myfunctio.c
#include "myhead.h"    上面函数的具体实现,很简单的,不做注释了

NODE *creat_node(data_t fd , data_t id)
{
    NODE *link_node = (NODE *)malloc(sizeof(NODE));
    if( NULL == link_node )
        exit(-1);
    link_node->fd = fd;
    link_node->id = id;
    link_node->next = NULL;
    return link_node;
}

void insert(NODE *head, data_t fd, data_t id)
{
    NODE *link_node = creat_node(fd, id);
    while(NULL != head->next)
    {
        head = head->next;
    }
    head->next = link_node;
}

NODE *find_fd(NODE *head, data_t id)
{     
     while(head->next)
    {
        if(id == head->next->id)
            return head->next;
        head = head->next;
    }
    return NULL;
}

NODE *find_id(NODE *head, data_t fd)
{
    while(head->next)
    {
        if(fd == head->next->fd)
            return head->next;
        head = head->next;
    }
    return NULL;
}

void delete_fd(NODE *head, data_t fd)
{   
    NODE *p =NULL;
    while( head->next )
    {
        p = head;
        head = head->next;
        if( fd == head->fd )
        {
            p->next = head->next;
            free(head);
            return;
        }
    }
    printf("%d outwith the link!\n", fd);     
 }

void show_id(NODE *head)
{
    printf("========================================================================\n");
    while( head->next )
    {   
        head = head->next;
         
        printf("Connected ID:%d\t", head->id);
    }
    printf("\n");
}

void broadcast(NODE *head, char buf[])
{   
    while(NULL != head->next)
    {     
        send(head->next->fd, buf, strlen(buf) + 1, 0);
        head = head->next;
    }
}

void send_online_id(NODE *head, data_t fd)     //给用户发送在线的id号
{
    char buf1[BUFSIZ] = {"\nOnline ID:\n=============================================\n"};
    while(head->next)
    {   
        head = head->next;
        char buf2[100] = {0};
        snprintf(buf2, 10, "%d", head->id);
        strncat(buf2, "\t", 2);
        strcat(buf1, buf2);   
    }
    strncat(buf1, "\n", 2);
    send(fd, buf1, strlen(buf1) + 1, 0);
}

服务器:
server.c
#include "myhead.h"

int main()
{
    int listenfd,connfd,maxfd,i,nbyte;                        //listenfd 是socket file discretion  connfd是客户连接文件描述符
    struct sockaddr_in myaddr, cliaddr;                        //sockaddr_in 是内核结构体,具体在man 7 ip
    listenfd = socket(AF_INET, SOCK_STREAM, 0);                 //创建打开ipv4内型TCP套接字文件
    fd_set global_rdfs,current_rdfs;  
    char buf[SIZE] = {0};
    if (0 > listenfd)
    {
        perror("socket");
        exit(-1);
    }
    int ii = 1;
    int sres = setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &ii, sizeof(ii));      //设置listenfd的属性,为IO复用型
    if (0 > sres)
    {
        printf("setsockopt error\n");
        exit(-1);
    }

    memset(&myaddr, 0, sizeof(myaddr));                                       //设置服务器分ip 及端口,内核的宏在man 7 ip 中查询
    myaddr.sin_family = AF_INET;                                             //   网络内型ipv4
    myaddr.sin_addr.s_addr = htonl(INADDR_ANY);                              //ip让内核取
    myaddr.sin_port = htons(8888);                                           //端口号为8888
    if(0 > bind(listenfd, (struct sockaddr *)&myaddr, sizeof(myaddr)))        //绑定ip及端口
    {
        perror("bind");
        exit(-1);
    }     
    listen(listenfd, 5);                                             //一次听五个客服连接,可以多设
    printf("listening...\n");


    FD_ZERO(&global_rdfs);                                         //global清零
    FD_SET(listenfd, &global_rdfs);                               //listenfd设为监听
    FD_SET(0, &global_rdfs);                                     //stdin设为监听
    maxfd = listenfd;                                            //文件描述符的最大值
    char send_buf[BUFSIZ] = {0};
    char recv_buf[BUFSIZ] = {0};
    int len = sizeof(cliaddr);
    int size = 0;
    int id = 10000;
    NODE *head = creat_node(-1, 0);                              //链表头结点初始化
    while(1)
    {
        current_rdfs = global_rdfs;
        if(0 > select(maxfd + 1, &current_rdfs, NULL, NULL, 0))  //监听已设好的文件描述符
        {
            perror("select");
            exit(-1);
        }
        else
        {
            for (i = 0; i <= maxfd; ++i)
            {
                if(FD_ISSET(i, &current_rdfs))
                {
                    if(i == listenfd)
                    {
                        connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &len);                                         //监听到有新客户连接
                        if(0 > connfd)
                        {
                            perror("accept");
                            exit(-1);
                        }
                        id++;
                        printf("Connection from %s\tport %d\t ID:%d\n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port), id); //把刚连接新客户的ip 和端口号打印子在服务端
                        char buf1[SIZE] = "Welcome to server! you ID:";
                        char buf2[SIZE] = {0};
                        snprintf(buf2, 10, "%d", id);                // 把id转化成字符串
                        strncat(buf1, buf2, strlen(buf2));                     //把字符id接在welcome to server后面
                        send(connfd, buf1, strlen(buf1), 0);                  //把给客户分配id发个客户
                        FD_SET(connfd, &global_rdfs);                         //把新增的文件描述符加入监听
                        maxfd = maxfd > connfd ? maxfd : connfd;             //取最大的文件描述符
                        insert(head, connfd, id);                            //把新增的文件描述符和id加在链表
                    }
                    else if(0 == i)
                    {     
                        fgets(send_buf, sizeof(send_buf), stdin);         //监听到标准输入有数据
                        if(!strncmp(send_buf, "quit", 4))                 //如果输入quit则退出服务器
                        {   
                            printf("goodbye!\n");
                            exit(-1);
                        }
                        if(!strncmp(send_buf, "list", 4))                 //输入list则显示在线客户id
                        {
                            show_id(head);
                            continue;
                        }
                        if(!strncmp(send_buf, "BC@", 3))                   //广播 标致《BC@》
                        {   
                            char buf[BUFSIZ] = {"server:\n"};
                            strcat(buf, send_buf);
                            broadcast(head, buf);
                            continue;
                        }     

                        char *p = strstr(send_buf, "@");                     //输入中检测@符
                        if (!p)                                              //没检测到上面的情况,则提醒输入格式
                        {
                            printf("useage:<id@msg>\n");
                            continue;
                        }
                        size = p - send_buf + 1;                           //检测到@符后把@之前的字符id长度算出为size
                        snprintf(buf, size, "%s", send_buf);              //剪取字符id放在buf
                        if(NULL == find_fd(head, atoi(buf)))              //atoi(buf)把字符id转化成int型,去找文件描述符fd
                        {
                            printf("The user doesn't have online\n");      //检测这个id是否在链表里,没在就提醒此id没在线
                            continue;
                        }
                        send(find_fd(head, atoi(buf))->fd, send_buf + size, strlen(send_buf) - size, 0);  //排除以上,在线id,把@后面的msg发给此id
                    }
                    else
                    {
                        if(0 >= (nbyte= recv(i, recv_buf, sizeof(recv_buf), 0)))                         //监听到接收端有消息
                        {
                            delete_fd(head,i);                                                            //id下线情况,则从链表中删除此NODE
                            close(i);                                                                     //关闭文件描述符
                            FD_CLR(i, &global_rdfs);                                                      //不再监听此fd
                        }
                        else
                        {
                            if(!strncmp(recv_buf, "list", 4))
                            {   
                                send_online_id(head, i);    //检测list命令后给该用户发送所有在线的id号
                                continue;
                            }
                            else if(!strncmp(recv_buf, "BC@", 3))   //广播交换
                            {   
                                char buf1[BUFSIZ] = {"ID:"};
                                char buf2[SIZE] = {0};
                                snprintf(buf2, 100, "%d", find_id(head, i)->id);
                                strcat(buf1, buf2);
                                strcat(buf1, "\n");
                                strcat(buf1, recv_buf);
                                broadcast(head, buf1);
                                printf("recv<ID:%d>msg:\n%s", find_id(head, i)->id, recv_buf);
                                continue;
                            }
                            else
                            {
                                char *p = NULL;
                                char buf1[SIZE] = {0};
                                char buf2[BUFSIZ] = {"<ID:"};
                                char buf3[SIZE] = {0};
                                p = strstr(recv_buf, "@");
                                if(NULL == p)
                                {
                                    send(i, "Please input online id!\n", 100, 0);
                                    continue;
                                }
                                int size = p - recv_buf;
                                strncpy(buf1, recv_buf, size);
                                if(!strncmp(buf1, "10000", 5))   //服务器id默认为10000
                                {
                                    printf("recv<ID:%d>msg:\n%s", find_id(head, i)->id, recv_buf);
                                    continue;     
                                }

                                NODE *q = find_fd(head, atoi(buf1));       //ID 无效处理
                                if(NULL == q)
                                {
                                    send(i, "You input the id number of does not exist\n", 100, 0);
                                    continue;
                                }
                                else    //有效id的信息做转发到相应的id号上
                                {   
                                    snprintf(buf3, 100, "%d", find_id(head, i)->id);
                                    strcat(buf2, buf3);
                                    strcat(buf2, ">msg:\n");
                                    strcat(buf2, recv_buf + size +1);
                                    send(q->fd, buf2, strlen(buf2) + 1, 0);
                                    continue;
                                }
                            }                                    
                        }                  
                    }
                }
            }
        }
    }
    return 0;
}
以上三个文件就是服务器啦,这三个文件要一起编译,在linux下用 gcc -o server myfunctio.c server.c   生成server二进制文件直接用 ./server 执行此服务端,等待用户连接.....
client.c   这是客户端啦,代码是跟服务端的部分差不多,由于小弟学习很忙所以不做注释了,现在还在练习阶段,只是简单的实现聊天功能,仅供学习交流,所以搞手就没喷人了啊,希望愿意和小弟一起学习的顶起,小弟是搞linux C 的,本人比较喜欢开源的,所以有和小弟一样喜欢开源代码的,多多交流,共同学习,共同进步
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>         
#include <sys/socket.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netinet/in.h>

#define MAXSIZE 1024

int main(int argc, char *argv[])
{
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(0 > sockfd)
    {
        perror("socket");
        exit(-1);
    }
   
    struct sockaddr_in servaddr;
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = inet_addr(argv[1]);
    servaddr.sin_port = htons(8888);
   
    char buf[MAXSIZE] = {0};   
        
    connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
    recv(sockfd, buf, sizeof(buf), 0);
    puts(buf);
   
    int i;
    fd_set readfd;
    int maxfd = -1;
    char recv_buf[MAXSIZE] = {0};
    char send_buf[MAXSIZE] = {0};
    while(1)
    {   
        FD_ZERO(&readfd);
        FD_SET(0, &readfd);
        maxfd = maxfd > 0 ? maxfd : 0;
        FD_SET(sockfd, &readfd);
        maxfd = maxfd > sockfd ? maxfd : sockfd;
        if(0 > select(maxfd + 1, &readfd, NULL, NULL, NULL))
        {
            perror("select");
            exit(-1);
        }

        for(i=0; i<=maxfd; i++)
        {
            if(FD_ISSET(i, &readfd))
            {
                if(i == sockfd)
                {
                    if(0 >= recv(sockfd, recv_buf, sizeof(recv_buf), 0))
                    {   
                        printf("Server is not online\n");
                        exit(-1);
                    }
                    printf("recv:%s", recv_buf);
                    memset(recv_buf, 0, sizeof(recv_buf));
                }
                if(0 == i)
                {
                    fgets(send_buf, sizeof(send_buf), stdin);
                    if(!strncmp(send_buf, "quit", 4))                 //如果输入quit则退出
                        {   
                            printf("goodbye!\n");
                            exit(-1);
                        }
                    send(sockfd, send_buf, strlen(send_buf) + 1, 0);
                    memset(send_buf, 0, sizeof(send_buf));
                }
            }
        }
    }
    return 0;
}

客户端满简单的,我第一次写好了就没怎么改过了,新增功能全在服务端改,编译就还是在linux终端下 用:gcc -o client client.c生成二进制client文件,然后用
./client 《ip》    这是带命令行参数的,ip是服务器的ip啦,本人用虚拟机克隆两个系统来测试的,也和我朋友做过远端测试,只要两台电脑ping的通就行,应该可以同时登陆很多人的,虽然我没做实际检验过,但理论上是可以的,多路复用不占很多资源,如果是多进程的话一般的电脑(私人电脑啊,不是服务器)最多可执行30000左右的进程就卡死。我加入这论坛好没多久,以前也很少逛论坛,所以很多规则不懂,总觉得这论坛很大,高手也很多,但感觉太冷清了,每天就那么几个在问问题,答问题,感觉不是很好,希望愿意的朋友加入一个团队做东西,本人下来再想做个ftp服务器代码,想一起学习的可以加入,在此帖上留言,我们可以分工写函数,最后组织在一起,总之一句话,一起学习一起进步



[ 本帖最后由 遗矢的老人 于 2012-8-25 23:56 编辑 ]
2012-08-25 22:31
遗矢的老人
Rank: 9Rank: 9Rank: 9
来 自:成都
等 级:蜘蛛侠
威 望:7
帖 子:325
专家分:1131
注 册:2012-7-20
收藏
得分:0 
有想拷去测试的我说明下啊,服务器先执行,在执行客户端,记住带服务器ip  linux的ip查询在终端输入ifconfig就查到了,最好用vmware装虚拟机再克隆一个测试,一个执行服务端,一个执行客户端,一台电脑就搞定,其中list命令是查询在线的id号,quit是退出,发“id@msg”给相应的在线id号发消息,给服务器发默认id=10000(即10000@ 你好)就可以了,还有广播功能,用“BC@msg”这样发在线所有id都能看的到msg,如果格式不对,都会做出判断功能,大家记住是linux C 不要拿去widows上试啦啊

[ 本帖最后由 遗矢的老人 于 2012-8-25 23:16 编辑 ]
2012-08-25 23:14
快速回复:多路复用的聊天工具小代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.039481 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved