目录
1. 需求项目需求:1. 如果有用户登录,其他用户可以收到这个人的登录信息2. 如果有人发送信息,其他用户可以收到这个人的群聊信息3. 如果有人下线,其他用户可以收到这个人的下线信息4. 服务器可以发送系统信息
代码:
头文件myhead.h
#ifndef __MYHEAD_H__
#define __MYHEAD_H__
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<string.h>
#include<stdlib.h>
#include<wait.h>
#include<signal.h>
//打印错误信息
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__",__LINE__); \
perror(msg);}while(0)
typedef struct Node
{
struct sockaddr_in addr;
struct Node *next;
}node;
typedef struct Msg
{
int code;
char user[32];
char text[128];
}msg_t;
#endif
服务器server.c
#include"myhead.h"
//创建节点函数
int create_node(node **phead)
{
*phead = (node *)malloc(sizeof(node));
if(*phead == NULL)
{
pr