服务器功能:
1、显示目前上线与下线情况;
2、广播通知用户登录、退出情况;
3、接受客户消息转发给所有客户端;
客户端功能:
1、接受服务器信息并显示;
2、发送消息给服务器端(指向群用户);
CODE:
服务器端:
#include "server.h" //可能用到的头文件;
- #include "server.h"
- int main()
- {
- int sockfd;
- struct sockaddr_in serveraddr, clientaddr;
- struct sockaddr_in addrbuf[20];
- char buf[MAXBUF];
- int length;
- sockfd = socket(AF_INET, SOCK_DGRAM, 0);
- memset(&serveraddr, 0, sizeof(serveraddr));
- serveraddr.sin_family = AF_INET;
- serveraddr.sin_port = htons(8000);
- serveraddr.sin_addr.s_addr = inet_addr("192.168.0.104");
- bind(sockfd, (SA *)&serveraddr, sizeof(serveraddr));
- length = sizeof(clientaddr);
- int i = 0;
- int j;
- char buf_addr[50];
- char bufport[10];
- while(1)
- {
- memset(buf, '\0', sizeof(buf));
- recvfrom(sockfd, buf, sizeof(buf), 0, (SA *)&clientaddr, &length);
-
- if(strcmp("IN", buf) == 0)
- {
- printf("%s:%d is on line\n", inet_ntoa(clientaddr.sin_addr),\
- ntohs(clientaddr.sin_port));
- addrbuf[i] = clientaddr;
- i ++;
- sprintf(buf_addr, "%s",inet_ntoa(clientaddr.sin_addr));
- sprintf(bufport, ":%d is on line ...", ntohs(clientaddr.sin_port));
- strcat(buf_addr, bufport);
-
- for(j = 0; j < i; j ++)
- {
- sendto(sockfd, buf_addr, sizeof(buf_addr), 0, (SA *)&(addrbuf[j]), sizeof(addrbuf[j]));
- }
-
- }
- else if(strcmp("quit",buf) == 0)
- {
- printf("%s:%d is off line\n", inet_ntoa(clientaddr.sin_addr),\
- ntohs(clientaddr.sin_port));
- j = 0;
- while(addrbuf[j].sin_addr.s_addr != clientaddr.sin_addr.s_addr ||
- addrbuf[j].sin_port != clientaddr.sin_port)
- {
- j ++;
- }
- //j --;
- int k;
- for(k = j; k < i - 1; k ++)
- addrbuf[k] = addrbuf[k + 1];
- i --;
-
- sprintf(buf_addr, "%s",inet_ntoa(clientaddr.sin_addr));
- sprintf(bufport, ":%d is off line ...", ntohs(clientaddr.sin_port));
- strcat(buf_addr, bufport);
- for(j = 0; j < i; j ++)
- {
- sendto(sockfd, buf_addr, sizeof(buf_addr), 0, (SA *)&(addrbuf[j]), sizeof(addrbuf[j]));
- }
- }
- else
- {
- char bufport[MAXBUF + 10];
- sprintf(bufport, "%d say :",ntohs(clientaddr.sin_port));
- strcat(bufport,buf);
- for(j = 0; j < i; j ++)
- {
- sendto(sockfd, bufport, sizeof(bufport), 0, (SA *)&(addrbuf[j]), sizeof(addrbuf[j]));
- }
- /*
- for(j = 0; j < i; j ++)
- printf("%s:%d say: %s\n", inet_ntoa(addrbuf[j].sin_addr),\
- ntohs(addrbuf[j].sin_port),buf);
- */
- }
-
- }
- return 0;
- }
客户端:
- #include "client.h"
- #include <signal.h>
- void exit_fc()
- {
- exit(0);
- return ;
- }
- int main()
- {
- int sockfd,length;
- struct sockaddr_in serveraddr;
- char buf[MAXBUF];
- pid_t pid;
- sockfd = socket(AF_INET, SOCK_DGRAM, 0);
- memset(&serveraddr, 0, sizeof(serveraddr));
- serveraddr.sin_family = AF_INET;
- serveraddr.sin_port = htons(8000);
- serveraddr.sin_addr.s_addr = inet_addr("192.168.0.104");
- printf("*******************chat room******************\n");
- printf("==============================================\n");
-
- memset(buf, '\0', sizeof(buf));
- buf[0] = 'I';
- buf[1] = 'N';
- buf[2] = '\0';
- sendto(sockfd, buf, sizeof(buf), 0, (SA *)&serveraddr, sizeof(serveraddr));
- length = sizeof(serveraddr);
- sleep(1);
- while(1)
- {
- if((pid = fork())==0)
- {
- while(1)
- {
- memset(buf, '\0', sizeof(buf));
- //printf("Send:");
- fgets(buf, MAXBUF, stdin);
- buf[strlen(buf) - 1] = '\0';
- sendto(sockfd, buf, sizeof(buf), 0, (SA *)&serveraddr, sizeof(serveraddr));
- if(strcmp("quit", buf) == 0)
- {
- kill(getppid(), SIGUSR1);
- break;
- }
- }
- exit(0);
- }
- else if(pid > 0)
- {
- signal(SIGUSR1, exit_fc);
- while(1)
- {
- memset(buf,'\0', sizeof(buf));
- recvfrom(sockfd, buf, sizeof(buf), 0, (SA *)&serveraddr, &length);
- printf("%s\n",buf);
- }
- wait(NULL);
- exit(0);
- break;
- }
- else
- {
- perror("fail to connect!\n");
- break;
- }
- }
- return 0;
- }
阅读(3206) | 评论(1) | 转发(1) |