网络聊天室

服务器端

复制代码
#include <my.h>
#define SER_PORT 8888
#define SER_IP "192.168.125.27"
typedef struct lianbiao
{
	struct sockaddr_in addr;
	struct lianbiao*next;
}biao;
//客户端信息
typedef struct lian
{
	int type;
	char name[32];
	char text[128];
}xinxi;
biao *fun()
{
	biao*p=(biao *)malloc(sizeof(biao));
	if(p==NULL)
	{
		perror("error");
		return NULL;
	}
	p->next=NULL;
	return p;
}
int main(int argc,const char*argv[])
{
	//创建套接字文件
	int sfd=socket(AF_INET,SOCK_DGRAM,0);
	if(sfd==-1)
	{
		perror("socket error");
		return -1;
	}
	printf("socket success sfd=%d\n",sfd);
	struct sockaddr_in sin, cin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(SER_PORT);
	sin.sin_addr.s_addr=inet_addr(SER_IP);
	int len=sizeof(cin);
	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
	{
		perror("bind error");
		return -1;
	}
	printf("bind success\n");
	biao *p=fun();
	biao *temp=NULL;
	temp=p;
	xinxi data;
	while(1)
	{
		recvfrom(sfd,&data,sizeof(data),0,(struct sockaddr*)&cin,&len);
		if(data.type==0) // 登录
		{
			p=temp;
			sprintf(data.text,"%s上线",data.name);
			// 打印客户端登录的信息
			printf("%s\n",data.text);
			while(p->next!=NULL)
			{
				p=p->next;
				sendto(sfd,&data,sizeof(data),0,(struct sockaddr*)&data,sizeof(data));
			}
			biao*pnew=(biao*)malloc(sizeof(biao));
			pnew->addr=cin;
			pnew->next=NULL;
		}

		else if(data.type==2)// 想说的话
		{
			p=temp;
			while(p->next!=NULL)
			{
				p=p->next;
				sendto(sfd,&data,sizeof(data),0,(struct sockaddr*)&data,sizeof(data));
			}
		}
			else if(data.type==1) //退出
			{
				puts("quit");
				biao*pdel=NULL;
				p=temp;
				sprintf(data.text,"%s下线",data.name);
				while(p->next!=NULL)
				{
					//先判断当前节点的下一个节点是不是要删除的客户端
					if(memcmp(&(p->next->addr),&cin,sizeof(cin))==-1)
					{
						pdel=p->next;
						p->next=pdel->next;
						//节点释放
						free(pdel);
						pdel=NULL;
					}
					else
					{
						p=p->next;
						sendto(sfd,&data,sizeof(data),0,(struct sockaddr*)&sin,sizeof(data));
					}
				}
			}
		}
	return 0;
}

客户端

复制代码
#include <my.h>
#define SER_PORT 8888
#define SER_IP "192.168.125.27"
typedef struct lianbiao
{
	struct sockaddr_in addr;
	struct lianbiao*next;
}biao;
//客户端信息
typedef struct lian
{
	int type;
	char name[32];
	char text[128];
}xinxi;
biao *fun()
{
	biao*p=(biao *)malloc(sizeof(biao));
	if(p==NULL)
	{
		perror("error");
		return NULL;
	}
	p->next=NULL;
	return p;
}
int main(int argc,const char*argv[])
{
	//创建套接字文件
	int sfd=socket(AF_INET,SOCK_DGRAM,0);
	if(sfd==-1)
	{
		perror("socket error");
		return -1;
	}
	printf("socket success sfd=%d\n",sfd);
	struct sockaddr_in sin, cin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(SER_PORT);
	sin.sin_addr.s_addr=inet_addr(SER_IP);
	int len=sizeof(cin);
	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
	{
		perror("bind error");
		return -1;
	}
	printf("bind success\n");
	biao *p=fun();
	biao *temp=NULL;
	temp=p;
	xinxi data;
	while(1)
	{
		recvfrom(sfd,&data,sizeof(data),0,(struct sockaddr*)&cin,&len);
		if(data.type==0) // 登录
		{
			p=temp;
			sprintf(data.text,"%s上线",data.name);
			// 打印客户端登录的信息
			printf("%s\n",data.text);
			while(p->next!=NULL)
			{
				p=p->next;
				sendto(sfd,&data,sizeof(data),0,(struct sockaddr*)&data,sizeof(data));
			}
			biao*pnew=(biao*)malloc(sizeof(biao));
			pnew->addr=cin;
			pnew->next=NULL;
		}

		else if(data.type==2)// 想说的话
		{
			p=temp;
			while(p->next!=NULL)
			{
				p=p->next;
				sendto(sfd,&data,sizeof(data),0,(struct sockaddr*)&data,sizeof(data));
			}
		}
			else if(data.type==1) //退出
			{
				puts("quit");
				biao*pdel=NULL;
				p=temp;
				sprintf(data.text,"%s下线",data.name);
				while(p->next!=NULL)
				{
					//先判断当前节点的下一个节点是不是要删除的客户端
					if(memcmp(&(p->next->addr),&cin,sizeof(cin))==-1)
					{
						pdel=p->next;
						p->next=pdel->next;
						//节点释放
						free(pdel);
						pdel=NULL;
					}
					else
					{
						p=p->next;
						sendto(sfd,&data,sizeof(data),0,(struct sockaddr*)&sin,sizeof(data));
					}
				}
			}
		}
	return 0;
}
相关推荐
鹿鸣天涯23 分钟前
关于进一步做好网络安全等级保护有关工作的问题释疑-【二级以上系统重新备案】、【备案证明有效期三年】
网络·安全·web安全
周杰伦_Jay30 分钟前
【网络核心协议全景解析】IP、TCP、UDP与HTTP(多表格深度对比)
网络·tcp/ip·udp
duration~1 小时前
TCP 首部格式
网络·网络协议·tcp/ip
www.021 小时前
linux服务器升级显卡驱动(笔记)
linux·运维·服务器·笔记·ubuntu·服务器环境
Linux-palpitate1 小时前
基于Prometheus和Grafana的MySQL监控,服务器监控
服务器·grafana·prometheus
晓oi1 小时前
基于华为设备的 OSPF+MSTP+DHCP+NAT 综合网络架构实现
网络·华为
博睿谷IT99_1 小时前
华为 USG 防火墙 NAT 配置
网络·华为·acl·nat·nat 环路
Yeats_Liao1 小时前
Go Web 编程快速入门 13 - 部署与运维:Docker容器化、Kubernetes编排与CI/CD
运维·前端·后端·golang
せいしゅん青春之我1 小时前
【JavaEE初阶】TCP核心机制10——异常情况的处理
java·网络·笔记·网络协议·tcp/ip·java-ee
lin张2 小时前
一文读懂计算机网络参考模型(二)
网络·计算机网络