网络聊天室

服务器端

#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;
}
相关推荐
__雨夜星辰__15 分钟前
Linux 学习笔记__Day2
linux·服务器·笔记·学习·centos 7
大耳朵土土垚16 分钟前
【Linux】日志设计模式与实现
linux·运维·设计模式
学问小小谢18 分钟前
第26节课:内容安全策略(CSP)—构建安全网页的防御盾
运维·服务器·前端·网络·学习·安全
一ge科研小菜鸡1 小时前
网络安全实战指南:攻防技术与防御策略
网络
yaoxin5211231 小时前
第十二章 I 开头的术语
运维·服务器
ProgramHan1 小时前
1992-2025年中国计算机发展状况:服务器、电脑端与移动端的演进
运维·服务器·电脑
马立杰4 小时前
H3CNE-33-BGP
运维·网络·h3cne
Mason Lin5 小时前
2025年1月22日(网络编程 udp)
网络·python·udp
字节全栈_rJF5 小时前
概述、 BGP AS 、BGP 邻居、 BGP 更新源 、BGP TTL 、BGP路由表、 BGP 同步
网络·智能路由器·php
EchoToMe5 小时前
电信传输基本理论/5G网络层次架构——超三万字详解:适用期末考试/考研/工作
网络·5g·架构