linux网络编程 UDP聊天程序 包括群聊和私聊

2023-12-08 10:32

本文主要是介绍linux网络编程 UDP聊天程序 包括群聊和私聊,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原文地址:http://blog.csdn.net/yanghaoran321/article/details/7885937


程序功能:

   能够支持多个客户端同时运行,具有群聊,私聊,上线通知,下线通知,系统消息等功能。


程序如下:

1. server.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#define CLIENT_LOGIN    100
#define CLIENT_CHAT     200
#define CLIENT_QUIT     300
#define SERVER_CHAT     400
#define SERVER_QUIT     500
#define PRINT_ONLINE    600
#define PRIVATE_CHAT    700
struct node 
{
char name[20];
struct sockaddr_in client_addr;
struct node *next;
};
struct message
{
long type;
char name[20];
char peer_name[20];
char mtext[512];
};
struct node *create_list(void);
void insert_list(struct node *, char *,struct sockaddr_in *);
void delete_list(struct node *, char *);
void recv_message(int , struct node *);
void send_message(int , struct sockaddr_in *, pid_t );
void client_login(int , struct node *, struct message *, struct sockaddr_in *);
void client_chat(int , struct node *, struct message *);
void client_quit(int , struct node *, struct message *);
void server_chat(int , struct node *, struct message *);
void server_quit(int , struct node *, struct message *);
void brocast_msg(int , struct node *, struct message *);
void print_online(int , struct node *, struct message *);
void private_chat(int , struct node *, struct message *);
void father_func(int sig_no)
{
return ;
}
int main(int argc, const char *argv[])
{
int socket_fd;
pid_t pid;
struct sockaddr_in server_addr;
struct node *head;
if (argc < 3)
{
fprintf(stderr, "usages : %s ip port\n", argv[0]);
exit(-1);
}
if ((socket_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("failed to create socket");
exit(-1);
}
head = create_list();
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(atoi(argv[2]));
server_addr.sin_addr.s_addr = inet_addr(argv[1]);
if (bind(socket_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
{
perror("failed to bind");
exit(-1);
}
if ((pid = fork()) < 0)
{
perror("failed to fork pid");
exit(-1);
}
if (pid == 0)
recv_message(socket_fd, head);
else
send_message(socket_fd, &server_addr, pid);
return 0;
}
struct node *create_list(void)
{
struct node *head;
head = (struct node *)malloc(sizeof(struct node));
head->next = NULL;
return head;
}
void insert_list(struct node *head, char *name, struct sockaddr_in *client_addr)
{
struct node *new;
new = (struct node *)malloc(sizeof(struct node));
strcpy(new->name, name);
new->client_addr = *client_addr;
new->next = head->next;
head->next = new;
return ;
}
void delete_list(struct node *head, char *name)
{
struct node *p = head->next;
struct node *q = head;
while (p != NULL)
{
if (strcmp(p->name, name) == 0)
break;
p = p->next;
q = q->next;
}
q->next = p->next;
p->next = NULL;
free(p);
return ;
}
void recv_message(int socket_fd, struct node *head)
{
struct message msg;
struct sockaddr_in client_addr;
int client_addrlen = sizeof(struct sockaddr);
while (1)
{
memset(&msg, 0, sizeof(msg));
if (recvfrom(socket_fd, &msg, sizeof(msg), 0, (struct sockaddr *)&client_addr, &client_addrlen) < 0)
{
perror("failed to recvform client");
exit(-1);
}
switch(msg.type)
{
case CLIENT_LOGIN:
client_login(socket_fd, head, &msg, &client_addr);
break;
case CLIENT_CHAT:
client_chat(socket_fd, head, &msg);
break;
case CLIENT_QUIT:
client_quit(socket_fd, head, &msg);
break;
case SERVER_CHAT:
server_chat(socket_fd, head, &msg);
break;
case SERVER_QUIT:
server_quit(socket_fd, head, &msg);
break;
case PRINT_ONLINE:
print_online(socket_fd, head, &msg);
break;
case PRIVATE_CHAT:
private_chat(socket_fd, head, &msg);
break;
default:
break;
}
}
return ;
}
void send_message(int socket_fd, struct sockaddr_in *server_addr, pid_t pid)
{
struct message msg;
char buf[512];
signal(getppid(), father_func);
while (1)
{
usleep(500);
printf(">");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = 0;
strcpy(msg.mtext, buf);
strcpy(msg.name , "server");
msg.type = SERVER_CHAT;
if (strncmp(buf, "quit", 4) == 0)
{
msg.type = SERVER_QUIT;
if (sendto(socket_fd, &msg, sizeof(msg), 0, 
(struct sockaddr *)server_addr, sizeof(struct sockaddr)) < 0)
{
perror("failed to send server_quit message");
exit(-1);
}
pause();
kill(pid, SIGKILL);
waitpid(pid, NULL, 0);
close(socket_fd);
exit(0);
}
if (sendto(socket_fd, &msg, sizeof(msg), 0, (struct sockaddr *)server_addr, sizeof(struct sockaddr)) < 0)
{
perror("failed to send server_chat message");
exit(-1);
}
}
return ;
}
void client_login(int socket_fd, struct node *head, struct message *msg, struct sockaddr_in *client_addr)
{
printf("********Login In********\n");
printf("%s is Login In\n", msg->name);
printf("************************\n");
insert_list(head, msg->name, client_addr);
brocast_msg(socket_fd, head, msg);
return ;
}
void client_chat(int socket_fd, struct node *head, struct message *msg)
{
printf("********Group Chat********\n");
printf("name: %s\n", msg->name);
printf("msg: %s\n", msg->mtext);
printf("**************************\n");
brocast_msg(socket_fd, head, msg);
return ;
}
void client_quit(int socket_fd, struct node *head, struct message *msg)
{
printf("*********Quit Msg********\n");
printf("%s is Quit\n", msg->name);
printf("*************************\n");
delete_list(head, msg->name);
brocast_msg(socket_fd, head, msg);
return ;
}
void server_chat(int socket_fd, struct node *head, struct message *msg)
{
printf("********Server Msg*********\n");
printf("msg: %s\n", msg->mtext);
printf("***************************\n");
brocast_msg(socket_fd, head, msg);
return ;
}
void server_quit(int socket_fd, struct node *head, struct message *msg)
{
brocast_msg(socket_fd, head, msg);
kill(getppid(), SIGUSR1);
return ;
}
void print_online(int socket_fd, struct node *head, struct message *msg)
{
struct node *p = head->next;
struct sockaddr_in my_addr;
char buf[512];
printf("%s is request to print online client\n", msg->name);
memset(buf, 0, sizeof(buf));
while (p != NULL)
{
if (strcmp(p->name, msg->name) == 0)
my_addr = p->client_addr;
strcat(buf, p->name);
strcat(buf, " ");
p = p->next;
}
strcpy(msg->mtext, buf);
msg->type = PRINT_ONLINE;
if (sendto(socket_fd, msg, sizeof(struct message), 0, 
(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
{
perror("failed to send print_online message");
exit(-1);
}
return ;
}
void private_chat(int socket_fd, struct node *head, struct message *msg)
{
struct node *p = head->next;
struct sockaddr_in peer_addr;
printf("******** Private Msg ********\n");
printf("from %s\n", msg->name);
printf("to %s\n", msg->peer_name);
printf("msg: %s\n", msg->mtext);
printf("*****************************\n");
while (p != NULL)
{
if (strcmp(p->name, msg->peer_name) == 0)
{
peer_addr = p->client_addr;
break;
}
p = p->next;
}
if (sendto(socket_fd, msg, sizeof(struct message), 0, 
(struct sockaddr *)&peer_addr, sizeof(peer_addr)) < 0)
{
perror("failed to send private message");
exit(-1);
}
return ;
}
void brocast_msg(int socket_fd, struct node *head, struct message *msg)
{
struct node *p = head->next;
while(p != NULL)
{
if (msg->type == CLIENT_LOGIN)
{
if (strcmp(p->name, msg->name) == 0)
{
p = p->next;
continue;
}
}
sendto(socket_fd, msg, sizeof(struct message), 0, (struct sockaddr *)&(p->client_addr), sizeof(struct sockaddr));
p = p->next;
}
return ;
}

2.client.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>#define CLIENT_LOGIN    100
#define CLIENT_CHAT     200
#define CLIENT_QUIT     300#define SERVER_CHAT     400
#define SERVER_QUIT     500#define PRINT_ONLINE    600
#define PRIVATE_CHAT    700struct message
{long type;char name[20];char peer_name[20];char mtext[512];
};void recv_message(int );
void send_message(int , struct sockaddr_in *, char *, pid_t);void login_msg(struct message *);
void group_msg(struct message *);
void quit_msg(struct message *);
void server_msg(struct message *);
void server_quit(void);
void online_msg(struct message *);
void private_msg(struct message *);void print_online(int , struct message *, struct sockaddr_in *);
void group_chat(int , struct message *, struct sockaddr_in *);
void private_chat(int ,struct message *, struct sockaddr_in *);
void client_quit(int , struct message *, struct sockaddr_in *, pid_t );int main(int argc, char *argv[])
{pid_t pid;int server_fd;struct sockaddr_in server_addr;if (argc < 4){fprintf(stderr, "usages: %s ip port name\n", argv[0]);exit(-1);}if ((server_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0){perror("failed to create server_fd");exit(-1);}server_addr.sin_family = AF_INET;server_addr.sin_port = htons(atoi(argv[2]));server_addr.sin_addr.s_addr = inet_addr(argv[1]);// inet_pton(AF_INET,argv[1],&server_addr.sin_addr);  //ipv4 if ((pid = fork()) < 0){perror("failed to fork pid");exit(-1);}if (pid == 0)recv_message(server_fd);elsesend_message(server_fd, &server_addr, argv[3], pid);return 0;
}void recv_message(int server_fd)
{struct message msg;while (1){memset(&msg, 0, sizeof(msg));if (recvfrom(server_fd, &msg, sizeof(msg), 0, NULL, NULL) < 0){perror("failed to recv server message");exit(-1);}switch(msg.type){case CLIENT_LOGIN:login_msg(&msg);break;case CLIENT_CHAT:group_msg(&msg);break;case CLIENT_QUIT:quit_msg(&msg);break;case SERVER_CHAT:server_msg(&msg);break;case SERVER_QUIT:server_quit();break;case PRINT_ONLINE:online_msg(&msg);break;case PRIVATE_CHAT:private_msg(&msg);break;default:break;}}return ;
}void send_message(int server_fd, struct sockaddr_in *server_addr, char *name, pid_t pid)
{struct message msg;char buf[512];int c;msg.type = CLIENT_LOGIN;strcpy(msg.name, name);if (sendto(server_fd, &msg, sizeof(msg), 0, (struct sockaddr *)server_addr, sizeof(struct sockaddr)) < 0){perror("failed to send login message");exit(-1);}while(1){usleep(500);printf("#####################################\n");printf("the following options can be choose:\n");printf("input 1 is to print online client\n");printf("input 2 is to chat to all the client\n");printf("input 3 is to chat to only one client\n");printf("input q is to quit\n");printf("#####################################\n");printf(">");c = getchar(); //输入回车符时停止输入到缓冲区while (getchar() != '\n'); //读出全部缓冲区的数据,包括'\n'switch(c){case '1':print_online(server_fd, &msg, server_addr);break;case '2':group_chat(server_fd, &msg, server_addr);break;case '3':private_chat(server_fd, &msg, server_addr);break;case 'q':client_quit(server_fd, &msg, server_addr, pid);break;default:break;}}return ;
}void login_msg(struct message *msg)
{printf("######## Login in ########\n");printf("%s is login in\n", msg->name);printf("######## Login in ########\n");return ;
}void group_msg(struct message *msg)
{printf("******** Group Msg ********\n");printf("name: %s\n", msg->name);printf("msg: %s\n", msg->mtext);printf("******** Group Msg ********\n");return ;
}void quit_msg(struct message *msg)
{printf("######## Quit Msg ########\n");printf("%s is Quit\n", msg->name);printf("######## Quit Msg ########\n");return ;
}void server_msg(struct message *msg)
{printf("******** Server Msg ********\n");printf("msg: %s\n", msg->mtext);printf("******** Server Msg ********\n");return ;
}void server_quit(void )
{kill(getppid(), SIGKILL);exit(0);
}void online_msg(struct message *msg)
{printf("******** Online Client ********\n");printf("%s\n", msg->mtext);printf("******** Online Client ********\n");return ;
}void private_msg(struct message *msg)
{printf("******** Private Msg ********\n");printf("name: %s\n", msg->name);printf("msg: %s\n", msg->mtext);printf("******** Private Msg ********\n");return ;
}void print_online(int server_fd, struct message *msg, struct sockaddr_in *server_addr)
{msg->type = PRINT_ONLINE;if (sendto(server_fd, msg, sizeof(struct message), 0, (struct sockaddr *)server_addr, sizeof(struct sockaddr)) < 0){perror("failed to send online message");exit(-1);}return ;
}void group_chat(int server_fd, struct message *msg, struct sockaddr_in *server_addr)
{char buf[512];memset(buf, 0, sizeof(buf));printf("*********** Group Chat Room ***********\n");printf("****** Welcome to group chat room ******\n");printf("* if you want to quit, please input quit\n");printf("****************************************\n");while(1){memset(buf, 0, sizeof(buf));usleep(500);printf(">");fgets(buf, sizeof(buf), stdin);buf[strlen(buf) - 1] = 0;msg->type = CLIENT_CHAT;strcpy(msg->mtext, buf);if (strncmp(buf, "quit", 4) == 0)break;if (sendto(server_fd, msg, sizeof(struct message), 0, (struct sockaddr *)server_addr, sizeof(struct sockaddr)) < 0){perror("failed to send group message");exit(-1);}}return ;
}void private_chat(int server_fd, struct message *msg, struct sockaddr_in *server_addr)
{char name[20];char buf[512];memset(name, 0, sizeof(name));printf("please input the peer_name\n");printf(">");fgets(name, sizeof(name), stdin);name[strlen(name) - 1] = 0;strcpy(msg->peer_name, name);msg->type = PRIVATE_CHAT;printf("connect to %s OK!\n", msg->peer_name);while(1){usleep(500);printf(">");fgets(buf, sizeof(buf), stdin);buf[strlen(buf) - 1] = 0;strcpy(msg->mtext, buf);if (strncmp(buf, "quit", 4) == 0)break;if (sendto(server_fd, msg, sizeof(struct message), 0, (struct sockaddr *)server_addr, sizeof(struct sockaddr)) < 0){perror("failed to send private message");exit(-1);}}return ;
}void client_quit(int server_fd, struct message *msg, struct sockaddr_in *server_addr, pid_t pid)
{msg->type = CLIENT_QUIT;if (sendto(server_fd, msg, sizeof(struct message), 0, (struct sockaddr *)server_addr, sizeof(struct sockaddr)) < 0){perror("failed to send quit message");exit(-1);}kill(pid, SIGKILL);waitpid(pid, NULL, 0);exit(0);
}






这篇关于linux网络编程 UDP聊天程序 包括群聊和私聊的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/469533

相关文章

Linux中shell解析脚本的通配符、元字符、转义符说明

《Linux中shell解析脚本的通配符、元字符、转义符说明》:本文主要介绍shell通配符、元字符、转义符以及shell解析脚本的过程,通配符用于路径扩展,元字符用于多命令分割,转义符用于将特殊... 目录一、linux shell通配符(wildcard)二、shell元字符(特殊字符 Meta)三、s

Linux之软件包管理器yum详解

《Linux之软件包管理器yum详解》文章介绍了现代类Unix操作系统中软件包管理和包存储库的工作原理,以及如何使用包管理器如yum来安装、更新和卸载软件,文章还介绍了如何配置yum源,更新系统软件包... 目录软件包yumyum语法yum常用命令yum源配置文件介绍更新yum源查看已经安装软件的方法总结软

linux报错INFO:task xxxxxx:634 blocked for more than 120 seconds.三种解决方式

《linux报错INFO:taskxxxxxx:634blockedformorethan120seconds.三种解决方式》文章描述了一个Linux最小系统运行时出现的“hung_ta... 目录1.问题描述2.解决办法2.1 缩小文件系统缓存大小2.2 修改系统IO调度策略2.3 取消120秒时间限制3

Linux alias的三种使用场景方式

《Linuxalias的三种使用场景方式》文章介绍了Linux中`alias`命令的三种使用场景:临时别名、用户级别别名和系统级别别名,临时别名仅在当前终端有效,用户级别别名在当前用户下所有终端有效... 目录linux alias三种使用场景一次性适用于当前用户全局生效,所有用户都可调用删除总结Linux

Linux:alias如何设置永久生效

《Linux:alias如何设置永久生效》在Linux中设置别名永久生效的步骤包括:在/root/.bashrc文件中配置别名,保存并退出,然后使用source命令(或点命令)使配置立即生效,这样,别... 目录linux:alias设置永久生效步骤保存退出后功能总结Linux:alias设置永久生效步骤

Linux使用fdisk进行磁盘的相关操作

《Linux使用fdisk进行磁盘的相关操作》fdisk命令是Linux中用于管理磁盘分区的强大文本实用程序,这篇文章主要为大家详细介绍了如何使用fdisk进行磁盘的相关操作,需要的可以了解下... 目录简介基本语法示例用法列出所有分区查看指定磁盘的区分管理指定的磁盘进入交互式模式创建一个新的分区删除一个存

利用Python编写一个简单的聊天机器人

《利用Python编写一个简单的聊天机器人》这篇文章主要为大家详细介绍了如何利用Python编写一个简单的聊天机器人,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 使用 python 编写一个简单的聊天机器人可以从最基础的逻辑开始,然后逐步加入更复杂的功能。这里我们将先实现一个简单的

Linux使用dd命令来复制和转换数据的操作方法

《Linux使用dd命令来复制和转换数据的操作方法》Linux中的dd命令是一个功能强大的数据复制和转换实用程序,它以较低级别运行,通常用于创建可启动的USB驱动器、克隆磁盘和生成随机数据等任务,本文... 目录简介功能和能力语法常用选项示例用法基础用法创建可启动www.chinasem.cn的 USB 驱动

高效管理你的Linux系统: Debian操作系统常用命令指南

《高效管理你的Linux系统:Debian操作系统常用命令指南》在Debian操作系统中,了解和掌握常用命令对于提高工作效率和系统管理至关重要,本文将详细介绍Debian的常用命令,帮助读者更好地使... Debian是一个流行的linux发行版,它以其稳定性、强大的软件包管理和丰富的社区资源而闻名。在使用

Linux Mint Xia 22.1重磅发布: 重要更新一览

《LinuxMintXia22.1重磅发布:重要更新一览》Beta版LinuxMint“Xia”22.1发布,新版本基于Ubuntu24.04,内核版本为Linux6.8,这... linux Mint 22.1「Xia」正式发布啦!这次更新带来了诸多优化和改进,进一步巩固了 Mint 在 Linux 桌面