标题:多路复用的聊天工具小代码
只看楼主
爱维尔
Rank: 2
等 级:论坛游民
帖 子:3
专家分:11
注 册:2012-8-21
得分:7 
最近正在做这个,学习了!!!
2012-08-24 16:17
遗矢的老人
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



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-379961-1-1.html




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

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