当前位置:网站首页>Group chat room based on UDP
Group chat room based on UDP
2022-07-24 01:00:00 【lhb2998658795】
1.UDP The function of group chat
There are new users logging in , Other online users can receive login information
User group chat , Other online users can receive group chat information
Some users quit , Other online users can receive the exit message
The server can send system information
2 Write the process of the project
Draw flow chart
Write the framework according to the flow chart
One function, one function
3 flow chart

4 Code implementation
4.1 The header file
#ifndef __MYHEAD_H__
#define __MYHEAD_H__
#include <head.h>
#define N 512
// The structure of chat operation
typedef struct _MSG{
char ch;// be used for 'l' Chat ,'q' sign out ,' Sign in d'
char name[128];// Save your name
char text[N];// Save chat content
}msg_t;
// The structure used to store each user's information
typedef struct _Jilu{
struct sockaddr_in addr;
struct _Jilu *next;
}jilu_t;
int create_head(jilu_t **head);
int input_addr(jilu_t *head,msg_t msg,int sockfd,struct sockaddr_in clientaddr,socklen_t clientaddr_len);
int wx_addr(jilu_t *head,msg_t msg,int sockfd,struct sockaddr_in clientaddr,socklen_t clientaddr_len);
int tuichu_addr(jilu_t *head,msg_t msg,int sockfd,struct sockaddr_in clientaddr,socklen_t clientaddr_len);
#endif4.2 function
#include "myhead.h"
// Create a single chain header
int create_head(jilu_t **head)
{
if(head==NULL){
printf(" Transmission error , Please check \n");
return -1;
}
(*head)=(jilu_t *)malloc(sizeof(jilu_t));
(*head)->next=NULL;
return 0;
}
// Record the information of the logged in user
int input_addr(jilu_t *head,msg_t msg,int sockfd,struct sockaddr_in clientaddr,socklen_t clientaddr_len)
{
if(head==NULL){
printf(" Transmission error , Please check \n");
return -1;
}
// Send the login information of this user to everyone
snprintf(msg.text,sizeof(msg.text),"[%s]%s",msg.name," Log on to the ");
// This is used to record the address of the header
jilu_t *jilu_head=head;
while(jilu_head->next!=NULL){
jilu_head=jilu_head->next;
if(sendto(sockfd,&msg,sizeof(msg),0,(struct sockaddr*)&jilu_head->addr,clientaddr_len)==-1){
ERRLOG(" Failed to send the user login information to everyone ");
}
}
// Create a new node , And put the new user information into the new order list
jilu_t *temp=NULL;
create_head(&temp);
temp->addr=clientaddr;
// Insert the user information into the linked list with header insertion
temp->next=head->next;
head->next=temp;
return 0;
}
int wx_addr(jilu_t *head,msg_t msg,int sockfd,struct sockaddr_in clientaddr,socklen_t clientaddr_len)
{
if(head==NULL){
printf(" Transmission error , Please check \n");
return -1;
}
// Send the received message to everyone except yourself
jilu_t *jilu_head=head;
while(jilu_head->next!=NULL){
jilu_head=jilu_head->next;
if(0!=memcmp(&(jilu_head->addr),&clientaddr,sizeof(clientaddr))){
if(sendto(sockfd,&msg,sizeof(msg),0,(struct sockaddr*)&jilu_head->addr,clientaddr_len)==-1){
ERRLOG(" Failed to send the chat content to everyone ");
}
}
}
return 0;
}
int tuichu_addr(jilu_t *head,msg_t msg,int sockfd,struct sockaddr_in clientaddr,socklen_t clientaddr_len)
{
if(head==NULL){
printf(" Transmission error , Please check \n");
return -1;
}
snprintf(msg.text,sizeof(msg.text),"%s%s",msg.name," Log out ");
jilu_t *jilu_head=head;
jilu_t *pdel=NULL;
while(jilu_head->next!=NULL){
if(0==memcmp(&(jilu_head->next->addr),&clientaddr,sizeof(clientaddr))){
pdel=jilu_head->next;
jilu_head->next=pdel->next;
free(pdel);
pdel=NULL;
}else{
jilu_head=jilu_head->next;
if(sendto(sockfd,&msg,sizeof(msg),0,(struct sockaddr*)&jilu_head->addr,clientaddr_len)==-1){
ERRLOG(" Tell everyone about this exit message and fail ");
}
}
}
return 0;
}4.3 The server
#include "myhead.h"
int main(int argc, char const *argv[])
{
int sockfd=0;
pid_t pid=0;
msg_t msg;// Used for various operations
msg_t faso;// Used to send system messages
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1){
ERRLOG(" Failed to create server socket ");
}
// Put the network information structure into the server
struct sockaddr_in serveraddr;
memset(&serveraddr,0,sizeof(serveraddr));
serveraddr.sin_family=AF_INET;
serveraddr.sin_port=htons(atoi(argv[2]));
serveraddr.sin_addr.s_addr=inet_addr(argv[1]);
socklen_t serveraddr_len=sizeof(serveraddr);
// Bind the socket to the network information structure
if(bind(sockfd,(struct sockaddr*)&serveraddr,serveraddr_len)==-1){
ERRLOG(" Binding socket to network information structure failed ");
}
// Create a new network information structure to store client information
struct sockaddr_in clientaddr;
clientaddr.sin_family=AF_INET;
socklen_t clientaddr_len=sizeof(clientaddr);
// Create a process
pid=fork();
if(pid==-1){
ERRLOG(" Server creation process failed ");
}else if(pid==0){
// Create a single list to save the network information structure
jilu_t *head;
create_head(&head);
memset(&msg,0,sizeof(msg));
while(1){
if(recvfrom(sockfd,&msg,sizeof(msg),0,(struct sockaddr*)&clientaddr,&clientaddr_len)==-1){
ERRLOG(" Failed to accept the information from the client ");
}
switch(msg.ch){
case 'd':// login information
input_addr(head,msg,sockfd,clientaddr,clientaddr_len);
//head->next=NULL; // This is for testing
break;
case 'l':// Chat messages
wx_addr(head,msg,sockfd,clientaddr,clientaddr_len);
break;
case 'q':// Exit message
tuichu_addr(head,msg,sockfd,clientaddr,clientaddr_len);
break;
}
}
}else{
while(1){
// Send system messages
memset(&faso,0,sizeof(faso));
fgets(faso.text,sizeof(faso.text),stdin);
faso.text[strlen(faso.text)-1]='\0';
faso.ch='l';
sprintf(faso.name,"%s"," System message ");
if(sendto(sockfd,&faso,sizeof(faso),0,(struct sockaddr*)&serveraddr,serveraddr_len)==-1){
ERRLOG(" Sending system message failed ");
}
}
}
return 0;
}
4.4 client
#include "myhead.h"
int main(int argc, char const *argv[])
{
// Judge whether the input is correct
if(argc!=3){
printf(" Input format error ,./a.out ip port\n");
exit(EXIT_SUCCESS);
}
int sockfd=0;
pid_t pid=0;
msg_t msg;// Create and send user information
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1){
ERRLOG(" Failed to create client socket ");
}
// Bind the client network information structure
struct sockaddr_in clientaddr;
memset(&clientaddr,0,sizeof(clientaddr));
clientaddr.sin_family=AF_INET;
clientaddr.sin_port=htons(atoi(argv[2]));
clientaddr.sin_addr.s_addr=inet_addr(argv[1]);
socklen_t clientaddr_len=sizeof(clientaddr);
// Enter the user's name to log in
msg.ch='d';
printf(" Please enter the name you use to sign in ");
fgets(msg.name,sizeof(msg.name),stdin);
msg.name[strlen(msg.name)-1]='\0';
// Send the operation that the user has logged in to the server
if(sendto(sockfd,&msg,sizeof(msg),0,(struct sockaddr*)&clientaddr,clientaddr_len)==-1){
ERRLOG(" The login information sent by the client to the server failed ");
}
// Create a process , Subprocesses are used to accept , The parent process is used to send
pid=fork();
if(pid==-1){
ERRLOG(" Client creation process failed ");
}else if(pid==0){
// Used to receive messages from the server
while(1){
memset(&msg,0,sizeof(msg));
if(recvfrom(sockfd,&msg,sizeof(msg),0,NULL,NULL)==-1){
ERRLOG(" The message sent by the receiving server is wrong ");
}
printf("[%s]>>(%s)\n",msg.name,msg.text);
}
}else{
// Write the content to be chatted
while(1){
memset(msg.text,0,sizeof(msg.text));
fgets(msg.text,sizeof(msg.text),stdin);
msg.text[strlen(msg.text)-1]='\0';
if(strncmp("quit",msg.text,5)==0){
msg.ch='q';
}else{
msg.ch='l';
}
// Send the written content to the server
if(sendto(sockfd,&msg,sizeof(msg),0,(struct sockaddr*)&clientaddr,clientaddr_len)==-1){
ERRLOG(" Failed to send chat content to the server ");
}
// When the stop is recognized , Close the process
if(strncmp("quit",msg.text,5)==0){
kill(pid,SIGKILL);
close(sockfd);
exit(EXIT_SUCCESS);
}
}
}
return 0;
}
边栏推荐
- [QNX hypervisor 2.2 user manual]9.1 configuration variables
- QT入门篇(2.1初入QT的开始第一个程序)
- The winverifytrust call returned 80096005 error. The timestamp signature or certificate cannot be verified or is damaged
- Intelligent video monitoring solutions for elderly care institutions, using new technologies to help the intelligent supervision of nursing homes
- Hot 100 dynamic programming
- URL query parameter encoding problem (golang)
- This is a big problem
- NOTICE: PHP message: PHP Warning: PHP Startup: Unable to load dynamic library ‘*****‘
- Hcia-01 initial understanding of the Internet
- Linx link, first level directory, redirection, CP and MV
猜你喜欢

Centernet target detection model and centerfusion fusion target detection model

如何在自动化测试中使用MitmProxy获取数据返回?

Prometheus+node exporter+grafana monitoring server system resources

Seektiger's okaleido has a big move. Will the STI of ecological pass break out?

Introduction to several scenarios involving programming operation of Excel in SAP implementation project

mysql 分支语句case报错

IDEA 热部署(热加载)

Idea hot deployment (hot load)

SAP 电商云 Spartacus UI Store 相关的设计明细

多源文件方式去访问全局变量的方式(extern用法)
随机推荐
暑假第四周总结
Scroll View implémente la mise à jour déroulante (empêche onload d'entrer dans la page de mise à jour initiale du Shell Triggered pour déclencher la mise à jour déroulante pour True)
Case error of MySQL branch statement
Centernet target detection model and centerfusion fusion target detection model
Installation and use of appscan
postman测试接口在URL配置正确的情况下出现404或者500错误
Hot 100 depth first
MySQL's heart index
Analysis of the advantages of the LAAS scheme of elephant swap led to strong performance of ETOKEN
Okaleido tiger NFT is about to log in to the binance NFT platform. Are you looking forward to it?
1000 okaleido tiger launched binance NFT, triggering a rush to buy
For data security reasons, the Dutch Ministry of Education asked schools to suspend the use of Chrome browser
这是一道大水题
Memory forensics nssctf otterctf 2018 (replay)
出于数据安全考虑 荷兰教育部要求学校暂停使用Chrome浏览器
Dark horse programmer - interface test - four day learning interface test - day 4 - postman reads external data files, reads data files, IHRM project practice, employee management module, adds employe
Create a self signed certificate to digitally sign exe files
[STM32] basic knowledge of serial communication
Seektiger's okaleido has a big move. Will the STI of ecological pass break out?
项目场景:nvidia-smi Unable to datemine the device handle for GPU 0000:01:00.0: Unknow Error