免费注册 查看新帖 |

Chinaunix

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

ipmsg实例 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-10 16:13 |只看该作者 |倒序浏览
/***********main**********************************************************/#include #include #include #include #include #include #include #include #include #include #include "ipmsg.h"#include "usrManage.h"#include "msgProcess.h"int sockfd;struct sockaddr_in localAddr;size_t localAddr_len=sizeof(localAddr);pthread_mutex_t work_mutex;int main(){    struct sockaddr_in broadAddr;    size_t broadAddr_len=sizeof(broadAddr);            int err,cmdW;    int broadcast_en = 1;    socklen_t broadcast_len = sizeof(broadcast_en);        pthread_t usrM_id,msgS_id;    USR *pUsr;    char send_buf[50]="",lineInfo_buf[50]="",cmd_buf[20]="";            bzero(&broadAddr,sizeof(broadAddr));    broadAddr.sin_family = AF_INET;    broadAddr.sin_port = htons(2425);    broadAddr.sin_addr.s_addr = inet_addr("192.168.220.255");    bzero(&localAddr,sizeof(localAddr));    localAddr.sin_family = AF_INET;    localAddr.sin_addr.s_addr = htonl(INADDR_ANY);    localAddr.sin_port = htons(2425);        if( (sockfd = socket(AF_INET,SOCK_DGRAM,0))     {    perror("socket");exit(1);}    if(bind(sockfd,(struct sockaddr*)&localAddr,sizeof(struct sockaddr)) !=0)    {    perror("bind");exit(1);}        setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,&broadcast_en,broadcast_len);    sprintf(send_buf,"1:%ld:sure_msg:sun_box:%ld",time((time_t *)NULL),IPMSG_BR_ENTRY);    sendto(sockfd,send_buf,50,0,(struct sockaddr *)&broadAddr,broadAddr_len);        if((err=pthread_mutex_init(&work_mutex,NULL)) != 0)    {    perror("pthread_mutex_init");exit(1);}        if((err=pthread_create(&usrM_id,NULL,usrM,NULL))!=0)    {    perror("pthread_create"); exit(1);}    if((err=pthread_create(&msgS_id,NULL,msgS,NULL))!=0)    {    perror("pthread_create"); exit(1);}    while(1);    return 0;}/********msgS.c******************************************************************/#include "msgProcess.h"#include "usrManage.h"void *msgS(void *arg){        char myMsg_buf[80]="",send_buf[80]="";    struct sockaddr_in sendAddr;    size_t sendAddr_len = sizeof(sendAddr);    while(1)    {        int    err;         memset(myMsg_buf,0,80);        memset(send_buf,0,80);        printf("sure>");        fgets(myMsg_buf,80,stdin);        if(!strcmp(myMsg_buf,"list\n"))        {            listUsr(head);            printf("Input Ip...\nIp>");            fflush(stdout);            memset(myMsg_buf,0,80);            fgets(myMsg_buf,80,stdin);            printf("usr = %s",myMsg_buf);            sendAddr.sin_addr.s_addr=inet_addr(myMsg_buf);        }        else        {            sprintf(send_buf,"1:%ld:sure_msg:sun_box:%ld:%s",time((time_t *)NULL),IPMSG_SENDMSG,myMsg_buf);            sendto(sockfd,send_buf,strlen(send_buf),0,(struct sockaddr *)&sendAddr,sendAddr_len);        }    }    }/************msgProcess.h**************************************************************/#ifndef _MSGPROCESS_H#define _MSGPROCESS_H#include #include #include #include "ipmsg.h"#include #include #include #include #include #include "usrManage.h"#include "ipmsg.h"void *msgS(void *arg);//void *msgR(void *arg);#endif/*************usrManage.c******************************************************/#include "usrManage.h"#include "msgProcess.h"USR *head = NULL;USR *pb=NULL;void *usrM(void *arg){    USR *pUsr;    char send_buf[80]="",back_buf[80]="",cmd_buf[5]="",name_buf[20]="",myMsg_buf[80]="";        struct sockaddr_in recvAddr;    size_t recvAddr_len = sizeof(recvAddr);    while(1)    {        int cmdW;            memset(back_buf,0,80);        memset(myMsg_buf,0,80);        memset(name_buf,0,20);        memset(send_buf,0,80);        memset(cmd_buf,0,5);        pUsr = (USR *)malloc(sizeof(USR));        recvfrom(sockfd,back_buf,80,0,(struct sockaddr *)&recvAddr,&recvAddr_len);            strcpy(pUsr->ipAddr,inet_ntoa(recvAddr.sin_addr));        sscanf(back_buf,"%*[^:]:%*[^:]:%*[^:]:%*[^:]:%[^:]",cmd_buf);        cmdW=atoi(cmd_buf)&0xff;        switch(cmdW)        {            case IPMSG_SENDMSG:            {//        pthread_mutex_lock(&work_mutex);                sprintf(send_buf,"1:%ld:sure_msg:sun_box:%ld",time((time_t *)NULL),IPMSG_RECVMSG);                sendto(sockfd,send_buf,80,0,(struct sockaddr *)&recvAddr,recvAddr_len);//    pthread_mutex_unlock(&work_mutex);                sscanf(back_buf,"%*[^:]:%*[^:]:%*[^:]:%*[^:]:%*[^:]%[^\n]",myMsg_buf);                    sscanf(back_buf,"%*[^:]:%*[^:]:%[^:]",name_buf);                    printf("\n%s from %s sya>",name_buf,inet_ntoa(recvAddr.sin_addr));                printf("%s\n",myMsg_buf);                printf("sure>");                fflush(stdout);                free(pUsr);                break;            }            case IPMSG_ANSENTRY:            {//                pUsr->usrAddr=recvAddr.sin_addr.s_addr;                sscanf(back_buf,"%*[^:]:%*[^:]:%[^:]",name_buf);                    strcpy(pUsr->usrName,name_buf);                memset(name_buf,0,20);                sscanf(back_buf,"%*[^:]:%*[^:]:%*[^:]:%[^:]",name_buf);                strcpy(pUsr->hostName,name_buf);                addUsr(pUsr);                break;            }            case IPMSG_BR_ENTRY:            {//                pUsr->usrAddr=recvAddr.sin_addr.s_addr;                sscanf(back_buf,"%*[^:]:%*[^:]:%[^:]",name_buf);                    strcpy(pUsr->usrName,name_buf);                memset(name_buf,0,20);                sscanf(back_buf,"%*[^:]:%*[^:]:%*[^:]:%[^:]",name_buf);                strcpy(pUsr->hostName,name_buf);                if(findUsr(pUsr->ipAddr))                {                    printf("%s log in\n",pUsr->ipAddr);                    addUsr(pUsr);                }                else free(pUsr);                                    break;            }            case IPMSG_BR_EXIT:            {                                printf("%s log out\n",pUsr->ipAddr);                delUsr(pUsr);                free(pUsr);                break;            }        }    }    }USR *addUsr(USR *pi){    static i=0;    if(head == NULL)    {        head = pi;        pb = head;    }    else    {        USR *p=head;        while((strcmp(p->ipAddr,pi->ipAddr)!=0) && (p->next != NULL))            p=p->next;        if(strcmp(p->ipAddr,pi->ipAddr)==0)            return head;        else         {            pb->next=pi;            pi->next=NULL;            pb=pi;        }    }    pb->ID=i++;//    printf("add usr i = %d\n",i);    return head;}USR *delUsr(USR *pi){        USR *pb = head;    USR *pf = NULL;    while((strcmp(pb->ipAddr,pi->ipAddr)!=0) && (pb->next != NULL))//    while(strcmp(pb->ipAddr,pi->ipAddr)!=0)    {        pf = pb;        pb=pb->next;    }    if(pb==head)    {        head=pb->next;        free(pb);        printf("pb=head del pb\n");        pb=head;        while(pb->next != NULL)        {            pb->ID--;            pb=pb->next;                }    }    else if(pb->next == NULL);    else    {        pf->next = pb->next;        free(pb);            printf("del pb\n");        pb=pf->next;        while(pb!= NULL)        {            pb->ID--;            pb=pb->next;                }    }    return head;}int findUsr(char *ip){    USR *pb = head;    if(pb != NULL)    {        while((strcmp(pb->ipAddr,ip)!=0)&&(pb->next !=NULL))            pb = pb->next;    }    if((pb == head)||(pb->next!=NULL))    {        return 0;    }    else        return 1;//can not find}void listUsr(USR *head){    USR *pb = head;    while(pb != NULL)    {        printf("ID=%-2d  usrName:%-10s  hostName:%-10s  ipAddr:%-15s\n",pb->ID,pb->usrName,pb->hostName,pb->ipAddr);        pb=pb->next;    }    printf("Please Chose Usr\n");}/***********usrManage.h******************************************************/#ifndef _USRMANAGE_H#define _USRMANAGE_H    #include #include #include #include #include #include #include #include #include "usrManage.h"#include "ipmsg.h"typedef  struct usr{    int ID;    char ipAddr[16];    char usrName[20];    char hostName[20];//    int usrAddr;        struct usr *next;}USR;extern int sockfd;extern struct sockaddr_in localAddr;extern socklen_t localAddr_len;extern USR *head;extern pthread_mutex_t work_mutex;USR *addUsr(USR *pi);USR *delUsr(USR *pi);int findUsr(char *ipAddr);void listUsr(USR *head);void *usrM(void *arg);#endif/***********makefile*******************************************************/ipmsg:main.c usrManage.c msgProcess.c    gcc -o $@ $^ -lpthreadclean:    rm  -rfv *.o/***********ipmsg.h************************************************************/#ifndef IPMSG_H#define IPMSG_H#include /*  IP Messenger Communication Protocol version 1.2 define  *//*  macro  */#define GET_MODE(command)    (command & 0x000000ffUL)#define GET_OPT(command)    (command & 0xffffff00UL)/*  header  */#define IPMSG_VERSION            0x0001#define IPMSG_DEFAULT_PORT        0x0979/*  command  */#define IPMSG_NOOPERATION        0x00000000UL#define IPMSG_BR_ENTRY            0x00000001UL#define IPMSG_BR_EXIT            0x00000002UL#define IPMSG_ANSENTRY            0x00000003UL#define IPMSG_BR_ABSENCE        0x00000004UL#define IPMSG_BR_ISGETLIST        0x00000010UL#define IPMSG_OKGETLIST            0x00000011UL#define IPMSG_GETLIST            0x00000012UL#define IPMSG_ANSLIST            0x00000013UL#define IPMSG_BR_ISGETLIST2        0x00000018UL#define IPMSG_SENDMSG            0x00000020UL#define IPMSG_RECVMSG            0x00000021UL#define IPMSG_READMSG            0x00000030UL#define IPMSG_DELMSG            0x00000031UL#define IPMSG_ANSREADMSG        0x00000032UL#define IPMSG_GETINFO            0x00000040UL#define IPMSG_SENDINFO            0x00000041UL#define IPMSG_GETABSENCEINFO    0x00000050UL#define IPMSG_SENDABSENCEINFO    0x00000051UL#define IPMSG_GETFILEDATA        0x00000060UL#define IPMSG_RELEASEFILES        0x00000061UL#define IPMSG_GETDIRFILES        0x00000062UL#define IPMSG_GETPUBKEY            0x00000072UL#define IPMSG_ANSPUBKEY            0x00000073UL/*  option for all command  */#define IPMSG_ABSENCEOPT        0x00000100UL#define IPMSG_SERVEROPT            0x00000200UL#define IPMSG_DIALUPOPT            0x00010000UL#define IPMSG_FILEATTACHOPT        0x00200000UL#define IPMSG_ENCRYPTOPT        0x00400000UL/*  option for send command  */#define IPMSG_SENDCHECKOPT        0x00000100UL#define IPMSG_SECRETOPT            0x00000200UL#define IPMSG_BROADCASTOPT        0x00000400UL#define IPMSG_MULTICASTOPT        0x00000800UL#define IPMSG_NOPOPUPOPT        0x00001000UL#define IPMSG_AUTORETOPT        0x00002000UL#define IPMSG_RETRYOPT            0x00004000UL#define IPMSG_PASSWORDOPT        0x00008000UL#define IPMSG_NOLOGOPT            0x00020000UL#define IPMSG_NEWMUTIOPT        0x00040000UL#define IPMSG_NOADDLISTOPT        0x00080000UL#define IPMSG_READCHECKOPT        0x00100000UL#define IPMSG_SECRETEXOPT        (IPMSG_READCHECKOPT|IPMSG_SECRETOPT)/* encryption flags for encrypt command */#define IPMSG_RSA_512            0x00000001UL#define IPMSG_RSA_1024            0x00000002UL#define IPMSG_RSA_2048            0x00000004UL#define IPMSG_RC2_40            0x00001000UL#define IPMSG_RC2_128            0x00004000UL#define IPMSG_RC2_256            0x00008000UL#define IPMSG_BLOWFISH_128        0x00020000UL#define IPMSG_BLOWFISH_256        0x00040000UL#define IPMSG_SIGN_MD5            0x10000000UL/* compatibilty for Win beta version */#define IPMSG_RC2_40OLD            0x00000010UL    // for beta1-4 only#define IPMSG_RC2_128OLD        0x00000040UL    // for beta1-4 only#define IPMSG_BLOWFISH_128OLD    0x00000400UL    // for beta1-4 only#define IPMSG_RC2_40ALL            (IPMSG_RC2_40|IPMSG_RC2_40OLD)#define IPMSG_RC2_128ALL        (IPMSG_RC2_128|IPMSG_RC2_128OLD)#define IPMSG_BLOWFISH_128ALL    (IPMSG_BLOWFISH_128|IPMSG_BLOWFISH_128OLD)/* file types for fileattach command */#define IPMSG_FILE_REGULAR        0x00000001UL#define IPMSG_FILE_DIR            0x00000002UL#define IPMSG_FILE_RETPARENT    0x00000003UL    // return parent directory#define IPMSG_FILE_SYMLINK        0x00000004UL#define IPMSG_FILE_CDEV            0x00000005UL    // for UNIX#define IPMSG_FILE_BDEV            0x00000006UL    // for UNIX#define IPMSG_FILE_FIFO            0x00000007UL    // for UNIX#define IPMSG_FILE_RESFORK        0x00000010UL    // for Mac/* file attribute options for fileattach command */#define IPMSG_FILE_RONLYOPT        0x00000100UL#define IPMSG_FILE_HIDDENOPT    0x00001000UL#define IPMSG_FILE_EXHIDDENOPT    0x00002000UL    // for MacOS X#define IPMSG_FILE_ARCHIVEOPT    0x00004000UL#define IPMSG_FILE_SYSTEMOPT    0x00008000UL/* extend attribute types for fileattach command */#define IPMSG_FILE_UID            0x00000001UL#define IPMSG_FILE_USERNAME        0x00000002UL    // uid by string#define IPMSG_FILE_GID            0x00000003UL#define IPMSG_FILE_GROUPNAME    0x00000004UL    // gid by string#define IPMSG_FILE_PERM            0x00000010UL    // for UNIX#define IPMSG_FILE_MAJORNO        0x00000011UL    // for UNIX devfile#define IPMSG_FILE_MINORNO        0x00000012UL    // for UNIX devfile#define IPMSG_FILE_CTIME        0x00000013UL    // for UNIX#define IPMSG_FILE_MTIME        0x00000014UL#define IPMSG_FILE_ATIME        0x00000015UL#define IPMSG_FILE_CREATETIME    0x00000016UL#define IPMSG_FILE_CREATOR        0x00000020UL    // for Mac#define IPMSG_FILE_FILETYPE        0x00000021UL    // for Mac#define IPMSG_FILE_FINDERINFO    0x00000022UL    // for Mac#define IPMSG_FILE_ACL            0x00000030UL#define IPMSG_FILE_ALIASFNAME    0x00000040UL    // alias fname#define IPMSG_FILE_UNICODEFNAME    0x00000041UL    // UNICODE fname#define FILELIST_SEPARATOR    '\a'#define HOSTLIST_SEPARATOR    '\a'#define HOSTLIST_DUMMY        "\b"/*  end of IP Messenger Communication Protocol version 1.2 define  *//*  IP Messenger for Windows  internal define  */#define IPMSG_REVERSEICON            0x0100#define IPMSG_TIMERINTERVAL            500#define IPMSG_ENTRYMINSEC            5#define IPMSG_GETLIST_FINISH        0#define IPMSG_BROADCAST_TIMER        0x0101#define IPMSG_SEND_TIMER            0x0102#define IPMSG_LISTGET_TIMER            0x0104#define IPMSG_LISTGETRETRY_TIMER    0x0105#define IPMSG_ENTRY_TIMER            0x0106#define IPMSG_DUMMY_TIMER            0x0107#define IPMSG_RECV_TIMER            0x0108#define IPMSG_ANS_TIMER                0x0109#define IPMSG_NICKNAME            1#define IPMSG_FULLNAME            2#define IPMSG_NAMESORT            0x00000000#define IPMSG_IPADDRSORT        0x00000001#define IPMSG_HOSTSORT            0x00000002#define IPMSG_NOGROUPSORTOPT    0x00000100#define IPMSG_ICMPSORTOPT        0x00000200#define IPMSG_NOKANJISORTOPT    0x00000400#define IPMSG_ALLREVSORTOPT        0x00000800#define IPMSG_GROUPREVSORTOPT    0x00001000#define IPMSG_SUBREVSORTOPT        0x00002000#endif

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

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP