网络聊天室

服务器端

复制代码
#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;
}
相关推荐
孙克旭_3 小时前
PXE_Kickstart_无人值守自动化安装系统
linux·运维·自动化
π大星星️5 小时前
HAProxy + Keepalived + Nginx 高可用负载均衡系统
运维·nginx·负载均衡
菜菜why6 小时前
AutoDL租用服务器教程
服务器
IT专业服务商6 小时前
联想 SR550 服务器,配置 RAID 5教程!
运维·服务器·windows·microsoft·硬件架构
firshman_start7 小时前
第十五章,SSL VPN
网络
Johnstons7 小时前
AnaTraf:深度解析网络性能分析(NPM)
前端·网络·安全·web安全·npm·网络流量监控·网络流量分析
落——枫7 小时前
路由交换实验
网络
Johny_Zhao7 小时前
K8S+nginx+MYSQL+TOMCAT高可用架构企业自建网站
linux·网络·mysql·nginx·网络安全·信息安全·tomcat·云计算·shell·yum源·系统运维·itsm
小诸葛的博客7 小时前
华为ensp实现跨vlan通信
网络·华为·智能路由器
稳联技术7 小时前
Ethercat转Profinet网关如何用“协议翻译术“打通自动化产线任督二脉
linux·服务器·网络