Chinaunix首页 | 论坛 | 博客
  • 博客访问: 45830
  • 博文数量: 19
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 57
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-28 09:40
个人简介

静下心,不浮躁,方能得智慧

文章分类
文章存档

2015年(7)

2014年(12)

我的朋友

分类: LINUX

2015-10-19 17:05:30

服务器功能:
1、显示目前上线与下线情况;
2、广播通知用户登录、退出情况;
3、接受客户消息转发给所有客户端;
客户端功能:
1、接受服务器信息并显示;
2、发送消息给服务器端(指向群用户);
CODE:
服务器端:
#include "server.h" //可能用到的头文件;

点击(此处)折叠或打开

  1. #include "server.h"

  2. int main()
  3. {
  4.     int sockfd;
  5.     struct sockaddr_in serveraddr, clientaddr;
  6.     struct sockaddr_in addrbuf[20];
  7.     char buf[MAXBUF];
  8.     int length;

  9.     sockfd = socket(AF_INET, SOCK_DGRAM, 0);

  10.     memset(&serveraddr, 0, sizeof(serveraddr));
  11.     serveraddr.sin_family = AF_INET;
  12.     serveraddr.sin_port = htons(8000);
  13.     serveraddr.sin_addr.s_addr = inet_addr("192.168.0.104");

  14.     bind(sockfd, (SA *)&serveraddr, sizeof(serveraddr));

  15.     length = sizeof(clientaddr);

  16.     int i = 0;
  17.     int j;
  18.     char buf_addr[50];
  19.     char bufport[10];

  20.     while(1)
  21.     {
  22.         memset(buf, '\0', sizeof(buf));
  23.         recvfrom(sockfd, buf, sizeof(buf), 0, (SA *)&clientaddr, &length);
  24.         
  25.         if(strcmp("IN", buf) == 0)
  26.         {
  27.             printf("%s:%d is on line\n", inet_ntoa(clientaddr.sin_addr),\
  28.                     ntohs(clientaddr.sin_port));

  29.             addrbuf[i] = clientaddr;
  30.             i ++;

  31.             sprintf(buf_addr, "%s",inet_ntoa(clientaddr.sin_addr));
  32.             sprintf(bufport, ":%d is on line ...", ntohs(clientaddr.sin_port));
  33.             strcat(buf_addr, bufport);
  34.             
  35.             for(j = 0; j < i; j ++)
  36.             {
  37.                 sendto(sockfd, buf_addr, sizeof(buf_addr), 0, (SA *)&(addrbuf[j]), sizeof(addrbuf[j]));
  38.             }
  39.     
  40.         }        
  41.         else if(strcmp("quit",buf) == 0)
  42.         {
  43.             printf("%s:%d is off line\n", inet_ntoa(clientaddr.sin_addr),\
  44.                     ntohs(clientaddr.sin_port));
  45.             j = 0;
  46.             while(addrbuf[j].sin_addr.s_addr != clientaddr.sin_addr.s_addr ||
  47.                     addrbuf[j].sin_port != clientaddr.sin_port)
  48.             {
  49.                 j ++;
  50.             }
  51.             //j --;

  52.             int k;
  53.             for(k = j; k < i - 1; k ++)
  54.                 addrbuf[k] = addrbuf[k + 1];
  55.             i --;

  56.             
  57.             sprintf(buf_addr, "%s",inet_ntoa(clientaddr.sin_addr));
  58.             sprintf(bufport, ":%d is off line ...", ntohs(clientaddr.sin_port));
  59.             strcat(buf_addr, bufport);

  60.             for(j = 0; j < i; j ++)
  61.             {
  62.                 sendto(sockfd, buf_addr, sizeof(buf_addr), 0, (SA *)&(addrbuf[j]), sizeof(addrbuf[j]));
  63.             }
  64.         }
  65.         else
  66.         {
  67.             char bufport[MAXBUF + 10];
  68.             sprintf(bufport, "%d say :",ntohs(clientaddr.sin_port));
  69.             strcat(bufport,buf);

  70.             for(j = 0; j < i; j ++)
  71.             {
  72.                 sendto(sockfd, bufport, sizeof(bufport), 0, (SA *)&(addrbuf[j]), sizeof(addrbuf[j]));
  73.             }
  74.             /*
  75.             for(j = 0; j < i; j ++)
  76.                 printf("%s:%d say: %s\n", inet_ntoa(addrbuf[j].sin_addr),\
  77.                         ntohs(addrbuf[j].sin_port),buf);
  78.             */

  79.         }
  80.         

  81.     }

  82.     return 0;
  83. }
客户端:

点击(此处)折叠或打开

  1. #include "client.h"
  2. #include <signal.h>

  3. void exit_fc()
  4. {
  5.     exit(0);
  6.     return ;
  7. }

  8. int main()
  9. {
  10.     int sockfd,length;
  11.     struct sockaddr_in serveraddr;
  12.     char buf[MAXBUF];
  13.     pid_t pid;

  14.     sockfd = socket(AF_INET, SOCK_DGRAM, 0);

  15.     memset(&serveraddr, 0, sizeof(serveraddr));
  16.     serveraddr.sin_family = AF_INET;
  17.     serveraddr.sin_port = htons(8000);
  18.     serveraddr.sin_addr.s_addr = inet_addr("192.168.0.104");

  19.     printf("*******************chat room******************\n");
  20.     printf("==============================================\n");
  21.     
  22.     memset(buf, '\0', sizeof(buf));
  23.     buf[0] = 'I';
  24.     buf[1] = 'N';
  25.     buf[2] = '\0';
  26.     sendto(sockfd, buf, sizeof(buf), 0, (SA *)&serveraddr, sizeof(serveraddr));

  27.     length = sizeof(serveraddr);
  28.     sleep(1);
  29.     while(1)
  30.     {
  31.         if((pid = fork())==0)
  32.         {
  33.             while(1)
  34.             {
  35.                 memset(buf, '\0', sizeof(buf));
  36.                 //printf("Send:");
  37.                 fgets(buf, MAXBUF, stdin);
  38.                 buf[strlen(buf) - 1] = '\0';
  39.                 sendto(sockfd, buf, sizeof(buf), 0, (SA *)&serveraddr, sizeof(serveraddr));

  40.                 if(strcmp("quit", buf) == 0)
  41.                 {
  42.                     kill(getppid(), SIGUSR1);
  43.                     break;
  44.                 }
  45.             }

  46.             exit(0);
  47.         }
  48.         else if(pid > 0)
  49.         {
  50.             signal(SIGUSR1, exit_fc);
  51.             while(1)
  52.             {
  53.                 memset(buf,'\0', sizeof(buf));
  54.                 recvfrom(sockfd, buf, sizeof(buf), 0, (SA *)&serveraddr, &length);
  55.                 printf("%s\n",buf);
  56.             }
  57.             wait(NULL);
  58.             exit(0);
  59.             break;
  60.         }
  61.         else
  62.         {
  63.             perror("fail to connect!\n");
  64.             break;
  65.         }
  66.     }

  67.     return 0;
  68. }



阅读(425) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册