项目:基于UDP的网络聊天室
项目需求:
- 如果有用户登录,其他用户可以收到这个人的登录信息
- 如果有人发送信息,其他用户可以收到这个人的群聊信息
- 如果有人下线,其他用户可以收到这个人的下线信息
- 服务器可以发送系统信息
补充:
设置了最大在线人数
用户输入quit会自动下线
服务器输入query,可查询在线用户
服务器输入quit,服务器会通知客户端,然后服务器客户端全部下线
服务器输入kick, 服务器会踢掉指定用户,用户收到消息后,自动下线
服务端:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <stdlib.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line=__%d__",__LINE__);\
perror(msg);\
}while(0)
//服务器地址+端口
#define SER_PORT 2222 //1024~49151
#define SER_IP "192.168.31.200"
//最大用户数
#define MAX_USER 3
#define COLON ":"
#define SYSTEM "**system**:"
#define LOGIN_IN "登录成功"
#define LOGIN_OUT "登出"
#define USER_EXIST "该用户名已被使用"
#define USER_FULL "登录用户数目已达上限,请稍后登录"
#define SYSTEM_CLOSE_DOWN "服务器已关闭"
#define KICK_OFF "已被踢下线"
//定义用户信息
typedef struct user
{
char name[20];
struct sockaddr_in cin;
}user,User;
typedef struct Node{
union{
int len;
User userinfo;
};
struct Node *next;
}Node,*lp;
//通过用户客户端信息来判断两个用户是否来着同一个客户端
int compare_user(User u1,User u2){
if(u1.cin.sin_addr.s_addr == u2.cin.sin_addr.s_addr && u1.cin.sin_port == u2.cin.sin_port){
return 0;
}
return -1;
}
lp create_node(){
lp head = (lp)malloc(sizeof(Node));
return head;
}
//初始化head
void init_head(lp head){
head->len = 0;
head->next = NULL;
}
//展示用户
void show_userlist(lp head){
printf("~~~~~~~~~~~~~~~~~~~~~~~\n");
lp node = head->next;
int i = 1;
while(node != NULL){
printf("user %d:name=%s addr=%s port=%d\n",i,\
node->userinfo.name,\
inet_ntoa(node->userinfo.cin.sin_addr),\
ntohs(node->userinfo.cin.sin_port));
i++;
node = node->next;
}
printf("~~~~~~~~~~~~~~~~~~~~~~~\n");
}
//查找用户 用户客户端地址
int find_user(lp head,User *u){
if(head->len == 0){
return 0;
}
lp node = head->next;
while(node != NULL){
//判断两人是否来着同一客户端
if(compare_user(node->userinfo,*u) == 0){
strcpy(u->name,node->userinfo.name);
return 1;
}
node = node->next;
}
return 0;
}
//查找用户 用户姓名
int find_user_by_name(lp head,User *u){
if(head->len == 0){
return 0;
}
lp node = head->next;
while(node != NULL){
//判断两人是否来着同一客户端
if(strcmp(node->userinfo.name,u->name) == 0){
u->cin = node->userinfo.cin;
return 1;
}
node = node->next;
}
return 0;
}
//登录
int login_in(lp head,User *u){
//在线人数已满
if(head->len == MAX_USER){
return -1;
}
//该人员已存在列表中
if(find_user(head,u) == 1){
return 0;
}
//添加新成员
lp node = create_node();
lp p = head;
while(p->next != NULL){
p = p->next;
}
node->userinfo = *u;
node->next = NULL;
p->next = node;
head->len++;
return 1;
}
//用户登出
void login_out(lp head,User *u){
//在线人数为0
if(head->len == 0){
return;
}
lp last = head;
lp p = head->next;
while(p != NULL){
if(compare_user(p->userinfo,*u) == 0){
last->next = p->next;
head->len--;
free(p);
p=NULL;
break;
}
last = p;
p = p->next;
}
}
//释放空间,关闭cfd
void *free_all(lp head,int cfd){
lp node;
while(head != NULL){
node = head;
head = head->next;
free(node);
node = NULL;
}
close(cfd);
return NULL;
}
//转发信息
void broadcast_msg(int cfd,char *buf,int len,lp head,struct user *u,int sign){
if(1 == sign){
//sign=1只单独发给u
if(sendto(cfd,buf,len,0,(struct sockaddr *)&u->cin,sizeof(u->cin)) < 0){
ERR_MSG("broadcast_msg");
}
}else{
lp node = head->next;
while(node != NULL){
//sign=0,群发给除了u以外所有人
//sign=-1 群发
if(!(sign == 0 && strcmp(u->name,node->userinfo.name) == 0) ){
if(sendto(cfd,buf,len,0,(struct sockaddr *)&node->userinfo.cin,sizeof(node->userinfo.cin)) < 0){
ERR_MSG("broadcast_msg");
}
}
node = node->next;
}
}
}
int main(int argc, const char *argv[])
{
//初始化链表
lp head = create_node();
init_head(head);
//创建新的套接字
int cfd = socket(AF_INET,SOCK_DGRAM,0);
if(-1 == cfd){
ERR_MSG("socket");
return -1;
}
//printf("cfd=%d\n",cfd);
//地址信息结构体
struct sockaddr_in sin;
//IPV4 地址簇
sin.sin_family = AF_INET;
//将端口号从主机字节序转成网络字节序
sin.sin_port = htons(SER_PORT);
//将IPV4地址的字符串转换为网络字节序的整数
sin.sin_addr.s_addr = inet_addr(SER_IP);
//绑定ip地址和端口
if(bind(cfd,(struct sockaddr *)&sin,sizeof(sin)) < 0){
ERR_MSG("bind");
return -1;
}
//printf("bind success\n");
printf("服务器已启动\n");
printf("输入query查询在线用户\n");
printf("输入quit关闭服务器\n");
printf("输入kick可踢特定用户下线\n");
printf("输入其他可与所有在线用户对话\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~\n");
//用于接收用户信息
char buf[1025]="";
//用于群发
char msg[1024]="";
//判断操作类型 1登录 2聊天 3登出
char opt=0;
ssize_t res = 0;
int u_res = -1;
//系统信息
char cmd[100]="";
fd_set readfds,tempfds;
FD_SET(0,&readfds);
FD_SET(cfd,&readfds);
while(1){
tempfds = readfds;
bzero(msg,sizeof(msg));
res = select(cfd+1,&tempfds,NULL,NULL,NULL);
if(res < 0){
ERR_MSG("select");
break;
}else if(res == 0){
printf("time out...\n");
}
if(FD_ISSET(0,&tempfds)){
fgets(cmd,sizeof(cmd),stdin);
cmd[strlen(cmd)-1]=0;
if(strcmp(cmd,"") == 0){
continue;
}
if(strcmp(cmd,"query") == 0){
show_userlist(head);
}else if(strcmp(cmd,"quit") == 0){
msg[0]='Q';//客户端退出
strcat(msg,SYSTEM);
strcat(msg,SYSTEM_CLOSE_DOWN);
broadcast_msg(cfd,msg,sizeof(msg),head,NULL,-1);
head = free_all(head,cfd);//释放空间关闭cfd
exit(0);
}else if(strcmp(cmd,"kick") == 0){
User temp;
while(1){
printf("请输入要踢掉的用户姓名:");
scanf("%s",temp.name);
u_res = find_user_by_name(head,&temp);
if(u_res == 0){
printf("该用户不存在,请重新输入\n");
continue;
}
login_out(head,&temp);
msg[0]='Q';//对话用户,让用户自动下线
strcat(msg,SYSTEM);
strcat(msg,temp.name);
strcat(msg,KICK_OFF);
broadcast_msg(cfd,msg,sizeof(msg),head,&temp,1);//对话踢掉的用户
msg[0]='T';
broadcast_msg(cfd,msg,sizeof(msg),head,NULL,-1);//对话其他在线用户
break;
}
}else{
msg[0]='T';
strcat(msg,SYSTEM);
strcat(msg,cmd);
broadcast_msg(cfd,msg,sizeof(msg),head,NULL,-1);
}
}
if(FD_ISSET(cfd,&tempfds)){
User temp;
socklen_t addrlen = sizeof(temp.cin);
bzero(buf,sizeof(buf));
//获取客户端信息
res = recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr *)&temp.cin,&addrlen);
if(res < 0){
ERR_MSG("recvfrom");
break;
}
//printf("get msg=%s\n addr=%s port=%d\n",buf,inet_ntoa(temp.sin_addr),ntohs(temp.sin_port));
//获取操作码
opt = buf[0];
if(opt == '1'){
//用户登录,获取用户姓名
strcpy(temp.name,buf+1);
//注册用户
u_res = login_in(head,&temp);
if(u_res == 0){
//该用户已存在
msg[0]='Q';//客户端退出
strcat(msg,SYSTEM);
strcat(msg,USER_EXIST);
broadcast_msg(cfd,msg,sizeof(msg),head,&temp,1);//对话用户
}else if(u_res == -1){
//登录用户数目达到上限
msg[0]='Q';//客户端退出
strcat(msg,SYSTEM);
strcat(msg,USER_FULL);
broadcast_msg(cfd,msg,sizeof(msg),head,&temp,1);//对话用户
}else{
//登录成功
msg[0]='T';//正常通话
strcat(msg,SYSTEM);
strcat(msg,temp.name);
strcat(msg,LOGIN_IN);
broadcast_msg(cfd,msg,sizeof(msg),head,&temp,-1);//群发
}
}else if(opt == '2'){
//个人聊天信息
u_res = find_user(head,&temp);
//未找到该人员,忽略该消息
if(u_res == 0){
continue;
}
msg[0]='T';
strcat(msg,temp.name);
strcat(msg,COLON);
strcat(msg,buf+1);
broadcast_msg(cfd,msg,sizeof(msg),head,&temp,0);//对话其他用户
}else if(opt == '3'){
//用户登出
u_res=find_user(head,&temp);
//未找到该用户,退出
if(u_res == 0){
continue;
}
msg[0]='T';
strcat(msg,SYSTEM);
strcat(msg,temp.name);
strcat(msg,LOGIN_OUT);
login_out(head,&temp);
broadcast_msg(cfd,msg,sizeof(msg),head,&temp,0);//对话其他用户
}
}
}
head = free_all(head,cfd);
return 0;
}
客户端:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line=__%d__",__LINE__);\
perror(msg);\
}while(0)
//服务器地址+端口
#define SER_PORT 2222 //1024~49151
#define SER_IP "192.168.31.200"
int main(int argc, const char *argv[])
{
printf("登录需要输入用户名\n");
printf("登录成功后输入quit即可退出\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~\n");
//创建一个新的套接字
int cfd = socket(AF_INET,SOCK_DGRAM,0);
if(-1 == cfd){
ERR_MSG("socket");
return -1;
}
//printf("cfd=%d\n",cfd);
//地址信息结构体
struct sockaddr_in sin;
//IPV4 地址簇
sin.sin_family = AF_INET;
//将端口号从主机字节序转成网络字节序
sin.sin_port = htons(SER_PORT);
//将IPV4地址的字符串转换为网络字节序的整数
sin.sin_addr.s_addr = inet_addr(SER_IP);
char buf[128]="";
char msg[129]="";
char name[20]="";
char opt = '1';//登录
ssize_t res = 0;
bzero(buf,sizeof(buf));
printf("请输入用户名:");
scanf("%s",name);
sprintf(msg,"%c%s",opt,name);
res = sendto(cfd,msg,sizeof(msg),0,(struct sockaddr *)&sin,sizeof(sin));
if(res < 0){
ERR_MSG("sendto");
return -1;
}
fd_set readfds,tempfds;
FD_SET(0,&readfds);
FD_SET(cfd,&readfds);
while(1){
tempfds = readfds;
res = select(cfd+1,&tempfds,NULL,NULL,NULL);
if(res < 0){
ERR_MSG("select");
return -1;
}else if(res == 0){
printf("time out...\n");
break;
}
if(FD_ISSET(0,&tempfds)){
//对话内容输入
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
if(strcmp(buf,"") == 0){
continue;
}
if(strcmp(buf,"quit") == 0){
opt = '3';//退出
}else{
opt = '2';//对话
}
sprintf(msg,"%c%s",opt,buf);
//发送信息给服务器
res = sendto(cfd,msg,sizeof(msg),0,(struct sockaddr *)&sin,sizeof(sin));
if(res < 0){
ERR_MSG("sendto");
return -1;
}
//退出
if(opt == '3')
break;
}
if(FD_ISSET(cfd,&tempfds)){
bzero(buf,sizeof(buf));
//收到服务器信息
res = recv(cfd,buf,sizeof(buf),0);
if(res < 0){
ERR_MSG("recvfrom");
break;
}
printf("%s\n",buf+1);
if(buf[0] == 'Q')
break;
}
}
close(cfd);
return 0;
}
服务器展示
ubuntu@ubuntu:20230309_net$ ./ser
服务器已启动
输入query查询在线用户
输入quit关闭服务器
输入kick可踢特定用户下线
输入其他可与所有在线用户对话
~~~~~~~~~~~~~~~~~~~~~~~
query
~~~~~~~~~~~~~~~~~~~~~~~
user 1:name=aaa addr=192.168.31.200 port=38667
user 2:name=bbb addr=192.168.31.200 port=53147
user 3:name=ccc addr=192.168.31.200 port=35327
~~~~~~~~~~~~~~~~~~~~~~~
hi
kick
请输入要踢掉的用户姓名:bbb
系统即将下线
quit
ubuntu@ubuntu:20230309_net$
客户端1
ubuntu@ubuntu:20230309_net$ ./cli
登录需要输入用户名
登录成功后输入quit即可退出
~~~~~~~~~~~~~~~~~~~~~~~
请输入用户名:aaa
**system**:aaa登录成功
**system**:bbb登录成功
**system**:ccc登录成功
*system**:hi
this is aaa
bbb:this is bbb
ccc:this is ccc
**system**:bbb已被踢下线
*system**:系统即将下线
**system**:服务器已关闭
ubuntu@ubuntu:20230309_net$
客户端2
ubuntu@ubuntu:20230309_net$ ./cli
登录需要输入用户名
登录成功后输入quit即可退出
~~~~~~~~~~~~~~~~~~~~~~~
请输入用户名:bbb
**system**:bbb登录成功
**system**:ccc登录成功
*system**:hi
aaa:this is aaa
this is bbb
ccc:this is ccc
**system**:bbb已被踢下线
ubuntu@ubuntu:20230309_net$
客户端3
ubuntu@ubuntu:20230309_net$ ./cli
登录需要输入用户名
登录成功后输入quit即可退出
~~~~~~~~~~~~~~~~~~~~~~~
请输入用户名:ccc
**system**:ccc登录成功
*system**:hi
aaa:this is aaa
bbb:this is bbb
this is ccc
**system**:bbb已被踢下线
*system**:系统即将下线
**system**:服务器已关闭
ubuntu@ubuntu:20230309_net$
客户端4
ubuntu@ubuntu:20230309_net$ ./cli
登录需要输入用户名
登录成功后输入quit即可退出
~~~~~~~~~~~~~~~~~~~~~~~
请输入用户名:ddd
**system**:登录用户数目已达上限,请稍后登录
ubuntu@ubuntu:20230309_net$