Chinaunix首页 | 论坛 | 博客
  • 博客访问: 130043
  • 博文数量: 30
  • 博客积分: 972
  • 博客等级: 中士
  • 技术积分: 332
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-08 10:04
文章分类

全部博文(30)

文章存档

2012年(30)

分类: LINUX

2012-07-19 19:54:43


点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<arpa/inet.h>
  4. #include<sys/types.h>
  5. #include<sys/socket.h>
  6. #include<netinet/in.h>
  7. #include<unistd.h>
  8. #include<pthread.h>
  9. #include<string.h>
  10. #include"IPMSG.h"
  11. #include"all.h"

  12. typedef void (*FUN)(int argc,char *argv[]);

  13. typedef struct cmd{
  14.                     char *name;
  15.                     FUN fun;
  16.                  }CMD;
  17.                 
  18. char user[30]="";
  19. char machine[30]="";
  20. int num=1;
  21. char blacklist[30]="";
  22. char group[10][30];

  23. IPMSG_USER *head_t;
  24. IPMSG_FILE *head_file=NULL,*head_send=NULL;

  25. CMD cmdlist[]={
  26.                 {"sendfile",sendfile},
  27.                 {"creatgroup",creatgroup},
  28.                 {"sendgroup",sendgroup},
  29.                 {"send",Sendto},
  30.                 {"cls",cls_fun},
  31.                 {"ls",ls_fun},
  32.                 {"help",help_fun},
  33.                 {"filels",filels_fun},
  34.                 {"getfile",getfile},
  35.                 {"setlist",setlist}
  36.              };

  37. int msg_deal(char *msg_src,char *stk, char *msg_done[])
  38. {
  39.     int i=0;
  40.     char *f_src,*f_stk;
  41.     f_src=msg_src;
  42.     f_stk=stk;
  43.     msg_done[i]=strtok(f_src,f_stk);
  44.     while(msg_done[i]!=NULL)
  45.         msg_done[++i]=strtok(NULL,f_stk);
  46.     
  47.     return i-1;
  48. }

  49. void get_info(void)
  50. {
  51.     printf("Pls input your name:");
  52.     fflush(stdout);
  53.     scanf("%s",user);
  54.     gethostname(machine,sizeof(machine));
  55. }

  56. int broadcast_online(int fd,char flags)
  57. {
  58.     char bro_packet[50]="";
  59.     int bro_len=0;
  60.     unsigned short port =2425;
  61.     char bro_ip[16]="192.168.1.255";
  62.     struct sockaddr_in bro_addr;
  63.     bzero(&bro_addr,sizeof(bro_addr));
  64.     
  65.     bro_addr.sin_family = AF_INET;
  66.     bro_addr.sin_port = htons(port);
  67.     inet_pton(AF_INET,bro_ip,&bro_addr.sin_addr);
  68.     
  69.     bro_len = sprintf(bro_packet,"%c:%ld:%s:%s:%c:%s",'1',(long int)time(NULL),user,machine,flags,"hello");
  70.     sendto(fd,bro_packet,bro_len,0,(struct sockaddr*)&bro_addr,sizeof(bro_addr));
  71.     
  72.     return 1;
  73. }

  74. void ls_fun(int argc, char *argv[])
  75. {
  76.     print_list(head_t);
  77. }

  78. void filels_fun(int argc, char *argv[])
  79. {
  80.     if(argv[1] == NULL)
  81.     {
  82.         printf("command is wrong\n");
  83.         return;
  84.     }
  85.     if(!strncmp(argv[1],"recv",4))
  86.     print_filelist(head_file);
  87.     if(!strncmp(argv[1],"send",4))
  88.     print_filelist(head_send);
  89. }

  90. void cls_fun(int argc, char *argv[])
  91. {
  92.     system("clear");
  93.     printf("\rIPMSG:");
  94.     fflush(stdout);
  95. }

  96. void setlist(int argc, char *argv[])
  97. {
  98.     if((argv[1] == NULL) || (!search_list(head_t,argv[1])))
  99.     {
  100.         printf("Pls check the username\n");
  101.         return;
  102.     }
  103.     strcpy(blacklist,argv[1]);
  104.     printf("\rIPMSG:");
  105.     fflush(stdout);
  106. }

  107. void help_fun(int argc, char *argv[])        //帮助
  108. {
  109.     char *cmd= "*****************************************************************************************\n"\
  110.             "* help                 :帮助                            *\n"\
  111.             "* ls/filels             :显示在线人/list                    *\n"\
  112.             "* send [username]         :发送消息                        *\n"\
  113.             "* cls                 :清屏                            *\n"\
  114.             "* exit                 :退出                            *\n"\
  115.             "* setlist [username]        :设置黑名单                        *\n"\
  116.             "* creatgroup/sendgroup         :创建组/发送组消息                    *\n"\
  117.             "*****************************************************************************************\n"
  118.             ;
  119.     printf("%s\n",cmd);
  120. }

  121. void Sendto(int argc, char *argv[])
  122. {
  123.     int snd_len=0;
  124.     char ip[16]="";
  125.     char snd_packet[50]="",buf[50]="";
  126.     unsigned short port =2425;
  127.     struct sockaddr_in snd_addr;
  128.     bzero(&snd_addr,sizeof(snd_addr));
  129.     
  130.     if((argv[1] == NULL) || (!search_list(head_t,argv[1])))
  131.     {
  132.         printf("command is wrong\n");
  133.         return;
  134.     }
  135.     
  136.     strcpy(ip,search_list(head_t,argv[1]));
  137.     snd_addr.sin_family = AF_INET;
  138.     snd_addr.sin_port = htons(port);
  139.     inet_pton(AF_INET,ip,&snd_addr.sin_addr);
  140.     
  141.     printf("Pls input sending message:");
  142.     fflush(stdout);
  143.     fgets(buf,sizeof(buf),stdin);
  144.     buf[strlen(buf)-1]='\0';
  145.     
  146.     snd_len = sprintf(snd_packet,"%c:%ld:%s:%s:%d:%s",'1',(long int)time(NULL),user,machine,288,buf);
  147.     sendto(argc,snd_packet,snd_len,0,(struct sockaddr*)&snd_addr,sizeof(snd_addr));
  148.     
  149. }

  150. void sndgroupmsg(int argc,char *buf,int i)
  151. {
  152.     int snd_len=0;
  153.     char ip[16]="";
  154.     char snd_packet[50]="";
  155.     unsigned short port =2425;
  156.     struct sockaddr_in snd_addr;
  157.     bzero(&snd_addr,sizeof(snd_addr));
  158.     if((group[i] == NULL) || (!search_list(head_t,group[i])))
  159.         return;
  160.     strcpy(ip,search_list(head_t,group[i]));
  161.     snd_addr.sin_family = AF_INET;
  162.     snd_addr.sin_port = htons(port);
  163.     inet_pton(AF_INET,ip,&snd_addr.sin_addr);
  164.     // printf("Pls input sending message:");
  165.     // fflush(stdout);
  166.     // fgets(buf,sizeof(buf),stdin);
  167.     // buf[strlen(buf)-1]='\0';
  168.     snd_len = sprintf(snd_packet,"%c:%ld:%s:%s:%d:%s",'1',(long int)time(NULL),user,machine,288,buf);
  169.     sendto(argc,snd_packet,snd_len,0,(struct sockaddr*)&snd_addr,sizeof(snd_addr));
  170.     return;
  171. }

  172. void sendgroup(int argc, char *argv[])
  173. {
  174.     int i=0;
  175.     char buf[50]="";
  176.     
  177.     printf("Pls input sending message:");
  178.     fflush(stdout);
  179.     fgets(buf,sizeof(buf),stdin);
  180.     buf[strlen(buf)-1]='\0';
  181.     for(i=0;i<10;i++)
  182.         sndgroupmsg(argc,buf,i);
  183.     return;
  184. }

  185. void creatgroup(int argc, char *argv[])
  186. {
  187.     int n=0,i=0;
  188.     char buf[30]="";
  189.     
  190.     printf("Pls input group member number:");
  191.     fflush(stdout);
  192.     scanf("%d",&n);
  193.     bzero(group,sizeof(group));
  194.     
  195.     for(i=0;i<n;i++)
  196.     {
  197.         printf("Pls input group member %d:\n",i);
  198.         scanf("%s",buf);
  199.         strncpy(group[i],buf,strlen(buf));
  200.     }
  201.     return;
  202. }

  203. int recvfile(char *filename, struct sockaddr_in recv_addr,int packetno,int fileno)
  204. {
  205.     int recv_fd=0,file_fd=0,len=0;
  206.     char buf[500]="",recv_packet[50]="";

  207.     if((recv_fd = socket(AF_INET,SOCK_STREAM,0)) < 0)
  208.     {
  209.         perror("Tcp socket");
  210.         return 0;
  211.     }
  212.     if((file_fd = open(filename,O_WRONLY|O_CREAT,0666)) < 0)
  213.     {
  214.         perror("open");
  215.         close(recv_fd);
  216.         return 0;
  217.     }
  218.     // file_fd = open(filename,O_RDWR|O_CREAT,0777);
  219.     connect(recv_fd,(struct sockaddr*)&recv_addr,sizeof(recv_addr));
  220.     
  221.     len = sprintf(recv_packet,"%c:%ld:%s:%s:%d:%x:%x:%x",'1',(long int)time(NULL),user,machine,96,packetno,fileno,0);
  222.     sendto(recv_fd,recv_packet,len,0,(struct sockaddr*)&recv_addr,sizeof(recv_addr));
  223.     
  224.     do
  225.     {    
  226.         len=read(recv_fd,buf,sizeof(buf));
  227.         write(file_fd,buf,len);
  228.     }while(len == sizeof(buf));
  229.     
  230.     close(file_fd);
  231.     close(recv_fd);
  232.     return 1;
  233. }

  234. void *handler(void *conn_fd)
  235. {
  236.     int len=0,fd=(int)conn_fd,file_fd=0;
  237.     char buf[500]="",store[100]="";
  238.     long count=0,packetno=0;
  239.     int command=0;
  240.     char *done[10];
  241.     IPMSG_FILE *temp;
  242.     
  243.     recv(fd,store,sizeof(store),0);
  244.     msg_deal(store,":",done);
  245.     command = atoi(done[4]);
  246.     
  247.     if( GET_MODE(command) == 96)
  248.     {
  249.     sscanf(done[5],"%lx",&packetno);
  250.     temp = search(head_send,packetno);
  251.     file_fd = open(temp->name,O_RDWR);
  252.     do
  253.     {
  254.         len = read(file_fd,buf,sizeof(buf));
  255.         count +=len;
  256.         write(fd,buf,len);
  257.         
  258.     }while(count < temp->size);
  259.     del_filelist(head_send,temp->name);
  260.     printf("\rFile tranfer compeleted\n");
  261.     printf("\rIPMSG:");
  262.     fflush(stdout);
  263.     close(file_fd);
  264.     }
  265.     close(fd);
  266.     return NULL;
  267. }

  268. void *send_file(void *a)
  269. {
  270.     int send_fd=0,conn_fd=0;
  271.     pthread_t tid_send;
  272.     struct sockaddr_in send_addr;
  273.     bzero(&send_addr,sizeof(send_addr));
  274.     
  275.     send_addr.sin_family = AF_INET;
  276.     send_addr.sin_port = htons(2425);
  277.     send_addr.sin_addr.s_addr = htonl(INADDR_ANY);

  278.     if((send_fd = socket(AF_INET,SOCK_STREAM,0)) < 0)
  279.     {
  280.         perror("Tcp socket");
  281.         return 0;
  282.     }
  283.     
  284.     if(bind(send_fd,(struct sockaddr*)&send_addr,sizeof(send_addr)) < 0)
  285.     {
  286.         perror("bind");
  287.         return NULL;
  288.     }
  289.     
  290.     listen(send_fd,10);
  291.     
  292.     while(1)
  293.     {
  294.         conn_fd = accept(send_fd,NULL,NULL);
  295.         
  296.         if(conn_fd < 0){
  297.             perror("accept error\n");
  298.             continue;
  299.         }
  300.         pthread_create(&tid_send,NULL,handler,(void *)conn_fd);
  301.         pthread_detach(tid_send);
  302.     }
  303.     close(send_fd);
  304.     return NULL;
  305. }

  306. void getfile(int argc, char *argv[])
  307. {
  308.     IPMSG_FILE *temp=NULL;
  309.     unsigned short port = 2425;
  310.     struct sockaddr_in recv_addr;
  311.     recv_addr.sin_family = AF_INET;
  312.     recv_addr.sin_port = htons(port);
  313.     if((argv[1] == NULL))
  314.     {
  315.         printf("command is wrong\n");
  316.         return;
  317.     }
  318.     temp = search_filelist(head_file,argv[1]);
  319.     inet_pton(AF_INET,search_list(head_t,temp->user),&recv_addr.sin_addr);
  320.     recvfile(argv[1],recv_addr,temp->pkgnum,temp->num);
  321.     del_filelist(head_file,argv[1]);
  322. }

  323. void sendfile(int argc, char *argv[])
  324. {
  325.     struct stat buf;
  326.     int len=0,size=0;
  327.     char send_packet[50]="";
  328.     long packetno=(long int)time(NULL);
  329.     int ctime;
  330.     unsigned short port = 2425;
  331.     struct sockaddr_in send_addr;
  332.     bzero(&send_addr,sizeof(send_addr));
  333.     
  334.     if((argv[1] == NULL) || (argv[1] == NULL))
  335.     {
  336.         printf("command is wrong\n");
  337.         return;
  338.     }
  339.     
  340.     if(stat(argv[2],&buf) < 0)
  341.     {
  342.         printf("The file is not here\n");
  343.         return;
  344.     }
  345.     send_addr.sin_family = AF_INET;
  346.     send_addr.sin_port = htons(port);
  347.     inet_pton(AF_INET,search_list(head_t,argv[1]),&send_addr.sin_addr);
  348.     //stat(argv[2],&buf);
  349.     size = buf.st_size;
  350.     ctime = (int)buf.st_ctime;
  351.         
  352.     len = sprintf(send_packet,"%c:%ld:%s:%s:%d:%c%d:%s:%x:%x:%x",'1',packetno,user,machine,2097440,0,num,argv[2],size,ctime,1);
  353.     sendto(argc,send_packet,len,0,(struct sockaddr*)&send_addr,sizeof(send_addr));
  354.     num++;
  355.     head_send = add_filelist(head_send,argv[2],num,packetno,size,ctime,1,argv[1]);
  356. }


  357. void *get_handler(void *arg)
  358. {
  359.     int fd=(int)arg,i;
  360.     int command=0,first=0;
  361.     unsigned short port = 2425;
  362.     char buf[500]="",opp_ip[16]="",ack_packet[50]="",buf2[500]="";
  363.     char *msg[20],*msg2[20],p=':',*p_f;
  364.     struct sockaddr_in opp_addr;
  365.     socklen_t ack_len=0,opp_len = sizeof(opp_addr);
  366.     
  367.     bzero(&opp_addr,sizeof(opp_addr));
  368.     
  369.     opp_addr.sin_family = AF_INET;
  370.     opp_addr.sin_port = htons(port);
  371.     
  372.     while(1)
  373.     {
  374.         recvfrom(fd,buf,sizeof(buf),0,(struct sockaddr*)&opp_addr,&opp_len);
  375.         strcpy(buf2,buf);
  376.         buf[strlen(buf)]='1';
  377.         //printf("buf==%s\n",buf);
  378.         msg_deal(buf2,&p,msg);
  379.         msg_deal(buf,&p,msg2);
  380.         command = atoi(msg[4]);
  381.         inet_ntop(AF_INET,&opp_addr.sin_addr,opp_ip,sizeof(opp_ip));
  382.         
  383.         if(!strcmp(msg[2],blacklist))
  384.             continue;
  385.         
  386.         if( GET_MODE(command) == 3 || GET_MODE(command) == 1)
  387.         {
  388.             if(!search_list(head_t,msg[2]))
  389.             add_list(head_t,msg[2],msg[3],opp_ip);
  390.             
  391.             if( GET_MODE(command) == 1 )
  392.             {
  393.                 broadcast_online(fd,'3');
  394.                 // printf("\r%s is online\n",msg[2]);
  395.                 // printf("\rIPMSG:");
  396.                 // fflush(stdout);
  397.             }
  398.             
  399.         }
  400.         if( GET_MODE(command) == 2 )
  401.         {
  402.             // printf("\r%s is offline\n",msg[2]);
  403.             // printf("\rIPMSG:");
  404.             // fflush(stdout);
  405.             del_list(head_t,msg[2]);
  406.         }
  407.         if( GET_MODE(command) == 32 )
  408.         {
  409.             if( GET_OPT(command) != 2097408)
  410.             {
  411.                 p_f = msg[5];
  412.                 p_f[strlen(msg[5])] = '\0';
  413.                 printf("\r[%s:%s]:%s\n",opp_ip,msg[2],msg[5]);
  414.                 ack_len = sprintf(ack_packet,"%c:%ld:%s:%s:%d:%s",'1',(long int)time(NULL),user,machine,33,msg[1]);
  415.                 sendto(fd,ack_packet,ack_len,0,(struct sockaddr*)&opp_addr,sizeof(opp_addr));
  416.                 printf("\rIPMSG:");
  417.                 fflush(stdout);
  418.             }
  419.             if( GET_OPT(command) == 2097408)
  420.             {
  421.                 if(first == 0)
  422.                 {
  423.                     printf("\rRecv a file packet:%s\n",msg2[6]);
  424.                     p_f = (char *)msg2[5];
  425.                     head_file=add_filelist(head_file,msg2[6],atoi(p_f+1),atol(msg2[1]),atol(msg2[7]),atol(msg2[8]),atoi(msg2[9]),msg2[2]);
  426.                     ack_len = sprintf(ack_packet,"%c:%ld:%s:%s:%d:%s",'1',(long int)time(NULL),user,machine,33,msg2[1]);
  427.                     for(i=0;i<5;i++)
  428.                     sendto(fd,ack_packet,ack_len,0,(struct sockaddr*)&opp_addr,sizeof(opp_addr));
  429.                     first = 1;
  430.                     printf("\rIPMSG:");
  431.                     fflush(stdout);
  432.                 }
  433.                  if((first == 1) && (!search_filelist(head_file,msg2[6])))
  434.                 {
  435.                     printf("\rRecv a file packet:%s\n",msg2[6]);
  436.                     p_f = (char *)msg2[5];
  437.                     add_filelist(head_file,msg2[6],atoi(p_f+1),atol(msg2[1]),atol(msg2[7]),atol(msg2[8]),atoi(msg2[9]),msg2[2]);
  438.                     ack_len = sprintf(ack_packet,"%c:%ld:%s:%s:%d:%s",'1',(long int)time(NULL),user,machine,33,msg2[1]);
  439.                     for(i=0;i<5;i++)
  440.                     sendto(fd,ack_packet,ack_len,0,(struct sockaddr*)&opp_addr,sizeof(opp_addr));
  441.                     printf("\rIPMSG:");
  442.                     fflush(stdout);
  443.                 }
  444.             }
  445.         }

  446.         bzero(buf,sizeof(buf));
  447.         bzero(buf2,sizeof(buf2));
  448.     }
  449.     return NULL;
  450. }

  451. void *put_handler(void *arg)
  452. {
  453.     int fd=(int)arg;
  454.     int i=0,found=0;
  455.     char buf[100]="",temp[100]="";
  456.     char *done[5];
  457.     
  458.     while(1)
  459.     {
  460.         printf("\rIPMSG:");
  461.         fflush(stdout);
  462.         bzero(buf,sizeof(buf));
  463.         bzero(temp,sizeof(temp));
  464.         
  465.         fgets(temp,sizeof(temp),stdin);
  466.         strncpy(buf,temp,strlen(temp)-1);
  467.                 
  468.         msg_deal(buf," ",done);

  469.         if(strstr(buf,"exit"))
  470.         {
  471.             broadcast_online(fd,'2');
  472.             exit(1);
  473.         }
  474.         
  475.         found = 1;
  476.         if(temp[0] != '\n')
  477.         {
  478.             for(i=0;i < sizeof(cmdlist)/sizeof(CMD);i++)
  479.             {    
  480.                 if(strcmp(cmdlist[i].name,done[0]) == 0 )
  481.                 {
  482.                     found = 0;
  483.                     cmdlist[i].fun(fd,done);
  484.                     break;
  485.                 }
  486.             }
  487.             if(found == 1)
  488.             printf("\rThe command is wrong,Pls type help\n");
  489.         }
  490.     }
  491.     return NULL;
  492. }

  493. int main()
  494. {
  495.     int udp_fd;
  496.     pthread_t tid_get,tid_put,tid_sndfile;
  497.     unsigned short port = 2425;
  498.     int broadcast_en= 1;
  499.     socklen_t broadcast_len = sizeof(broadcast_en);
  500.     
  501.     struct sockaddr_in srv_addr,cli_addr;
  502.     bzero(&srv_addr,sizeof(srv_addr));
  503.     bzero(&cli_addr,sizeof(cli_addr));
  504.     
  505.     srv_addr.sin_family = AF_INET;
  506.     srv_addr.sin_port = htons(port);
  507.     srv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  508.     
  509.     get_info();
  510.     
  511.     if((udp_fd = socket(AF_INET,SOCK_DGRAM,0)) < 0)
  512.     {
  513.         perror("socket");
  514.         exit(-1);
  515.     }
  516.     
  517.     setsockopt(udp_fd,SOL_SOCKET,SO_BROADCAST,&broadcast_en,broadcast_len);
  518.     
  519.     if( bind(udp_fd,(struct sockaddr*)&srv_addr,sizeof(srv_addr)) < 0)
  520.     {
  521.         perror("bind");
  522.         exit(-1);
  523.     }
  524.     
  525.     broadcast_online(udp_fd,'1');
  526.     head_t = creat_list();
  527.     
  528.     pthread_create(&tid_get,NULL,get_handler,(void *)udp_fd);
  529.     pthread_create(&tid_put,NULL,put_handler,(void *)udp_fd);
  530.     pthread_create(&tid_sndfile,NULL,send_file,NULL);
  531.     while(1);
  532.     return 0;
  533. }


点击(此处)折叠或打开

  1. #include"all.h"
  2. #include"IPMSG.h"

  3. IPMSG_USER *creat_list(void)
  4. {
  5.     IPMSG_USER *head=NULL,*pb=NULL;
  6.     char loc_ip[16]="192.168.1.133";
  7.     
  8.     pb = (IPMSG_USER *)malloc(sizeof(IPMSG_USER));
  9.     strcpy(pb->name,user);
  10.     strcpy(pb->host,machine);
  11.     strcpy(pb->s_addr,loc_ip);
  12.     
  13.     head = pb;    
  14.     pb->next = NULL;
  15.     
  16.     return head;
  17. }

  18. IPMSG_USER *add_list(IPMSG_USER *head,char *name,char *host,char *ip)
  19. {
  20.     
  21.     IPMSG_USER *temp=head;
  22.     IPMSG_USER *p_new=NULL;
  23.     
  24.     p_new = (IPMSG_USER *)malloc(sizeof(IPMSG_USER));
  25.     strcpy(p_new->name,name);
  26.     strcpy(p_new->host,host);
  27.     strcpy(p_new->s_addr,ip);
  28.     
  29.     if(temp == NULL)
  30.     {
  31.         head = p_new;
  32.         p_new = NULL;
  33.     }
  34.     while(temp->next != NULL)
  35.         temp = temp->next;
  36.     temp->next = p_new;
  37.     p_new->next = NULL;
  38.     
  39.     return head;
  40. }

  41. IPMSG_FILE *add_filelist(IPMSG_FILE *head,char *filename,int fileno,long packetno,long filesize,long ltime,int att,char *opp_user)
  42. {
  43.     IPMSG_FILE *temp=head;
  44.     IPMSG_FILE *p_new=NULL;
  45.     p_new = (IPMSG_FILE *)malloc(sizeof(IPMSG_FILE));
  46.     strcpy(p_new->name,filename);
  47.     p_new->num = fileno;
  48.     p_new->pkgnum = packetno;
  49.     p_new->size = filesize;
  50.     p_new->ltime = ltime;
  51.     p_new->att = att;
  52.     strcpy(p_new->user,opp_user);
  53.     
  54.     if(temp == NULL)
  55.     {
  56.         head = p_new ;
  57.         // p_new = head;
  58.         p_new = NULL;
  59.         return head;
  60.     }
  61.     while(temp->next != NULL)
  62.         temp = temp->next;
  63.     temp->next = p_new;
  64.     p_new->next = NULL;
  65.     
  66.     return head;
  67. }


  68. IPMSG_USER *del_list(IPMSG_USER *head,char *name)
  69. {
  70.     IPMSG_USER *temp=head;
  71.     IPMSG_USER *pf;
  72.     
  73.     while(temp!=NULL)
  74.     {
  75.         if(!strcmp(name,temp->name))
  76.         {        
  77.             if(temp==head)
  78.             head=temp->next;
  79.         else if(temp->next==NULL)
  80.             pf->next=NULL;
  81.         else
  82.             pf->next=temp->next;
  83.         }
  84.             pf=temp;
  85.             temp=temp->next;
  86.     }
  87.     free(temp);
  88.     return head;
  89. }

  90. IPMSG_FILE *del_filelist(IPMSG_FILE *head,char *name)
  91. {
  92.     IPMSG_FILE *temp=head;
  93.     IPMSG_FILE *pf;
  94.     
  95.     while(temp!=NULL)
  96.     {
  97.         if(!strcmp(name,temp->name))
  98.         {        
  99.             if(temp==head)
  100.             head=temp->next;
  101.         else if(temp->next==NULL)
  102.             pf->next=NULL;
  103.         else
  104.             pf->next=temp->next;
  105.         }
  106.             pf=temp;
  107.             temp=temp->next;
  108.     }
  109.     free(temp);
  110.     return head;
  111. }

  112. void print_list(IPMSG_USER *head)
  113. {
  114.     IPMSG_USER *temp=head;
  115.     while(temp!=NULL)
  116.     {
  117.         printf("%s\t%s\t%s\n",temp->name,temp->host,temp->s_addr);
  118.         temp=temp->next;
  119.     }
  120. }

  121. void print_filelist(IPMSG_FILE *head)
  122. {
  123.     IPMSG_FILE *temp=head;
  124.     while(temp!=NULL)
  125.     {
  126.         printf("%d\t%s\t%s\t%ld\n",temp->num,temp->name,temp->user,temp->pkgnum);
  127.         temp=temp->next;
  128.     }
  129. }

  130. char* search_list(IPMSG_USER *head,char *username)
  131. {
  132.     IPMSG_USER *temp=head;
  133.     while(strcmp(temp->name,username))
  134.         {
  135.             if(temp->next == NULL)
  136.                 return (char*)0;
  137.             temp=temp->next;
  138.         }
  139.         
  140.     return temp->s_addr;
  141. }

  142. IPMSG_FILE *search_filelist(IPMSG_FILE *head,char *filename)
  143. {
  144.     IPMSG_FILE *temp=head;
  145.     while(strcmp(temp->name,filename))
  146.         {
  147.             //printf("%s\n",temp->name);
  148.             if(temp->next == NULL)
  149.                 return (IPMSG_FILE *)0;
  150.             temp=temp->next;
  151.         }
  152.         
  153.     return temp;
  154. }

  155. IPMSG_FILE *search(IPMSG_FILE *head,long packetno)
  156. {
  157.     IPMSG_FILE *temp=head;
  158.     while(temp->pkgnum != packetno)
  159.         {
  160.             //printf("%s\n",temp->name);
  161.             if(temp->next == NULL)
  162.                 return (IPMSG_FILE *)0;
  163.             temp=temp->next;
  164.         }
  165.         
  166.     return temp;
  167. }


点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<arpa/inet.h>
  4. #include<sys/types.h>
  5. #include<sys/socket.h>
  6. #include<netinet/in.h>
  7. #include<unistd.h>
  8. #include<pthread.h>
  9. #include<string.h>
  10. #include<sys/stat.h>
  11. #include<fcntl.h>
  12. #include"IPMSG.h"

  13. //用户信息结构体
  14. typedef struct userlist
  15. {
  16.     char name[20];    //用户名
  17.     char host[20];    //主机名
  18.     char s_addr[16];        //IP地址(32位网络字节序)
  19.     struct userlist *next;
  20. }IPMSG_USER;

  21. //文件信息结构体
  22. typedef struct filelist
  23. {
  24.     char name[50];//文件名
  25.     int num;// 文件序号
  26.     int att;//文件属性
  27.     long pkgnum;//包编号
  28.     long size;//文件大小
  29.     long ltime;//最后修改时间
  30.     char user[10];    //发送者用户名
  31.     struct filelist *next;
  32. }IPMSG_FILE;

  33. extern IPMSG_USER *creat_list(void);
  34. extern IPMSG_USER *add_list(IPMSG_USER *head,char *name,char *host,char *ip);
  35. extern IPMSG_USER *del_list(IPMSG_USER *head,char *name);
  36. extern void print_list(IPMSG_USER *head);
  37. extern char* search_list(IPMSG_USER *head,char *username);

  38. IPMSG_FILE *add_filelist(IPMSG_FILE *head,char *filename,int fileno,long packetno,long filesize,long ltime,int att,char *opp_user);
  39. extern IPMSG_FILE *del_filelist(IPMSG_FILE *head,char *name);
  40. extern void print_filelist(IPMSG_FILE *head);
  41. extern IPMSG_FILE *search_filelist(IPMSG_FILE *head,char *filename);
  42. extern IPMSG_FILE *search(IPMSG_FILE *head,long packetno);

  43. extern void ls_fun();
  44. extern void help_fun();
  45. extern void Sendto();
  46. extern void cls_fun();
  47. extern void filels_fun(int argc, char *argv[]);
  48. extern void getfile(int argc, char *argv[]);
  49. extern void sendfile(int argc, char *argv[]);

  50. extern char user[30];
  51. extern char machine[30];
  52. ern char user[30];
  53. extern char machine[30];
  54. extern void sendgroup(int argc, char *argv[]);
  55. extern void creatgroup(int argc, char *argv[]);

  56. extern char user[30];
  57. extern char machine[30];

阅读(2740) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~