网络聊天室

服务器端

复制代码
#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 分钟前
Python 自动化办公神器|一键转换所有文档为 PDF
运维·python
有趣的我28 分钟前
wireshark介绍和使用
网络·测试工具·wireshark
Fanmeang32 分钟前
OSPF高级特性之FRR
运维·网络·华为·ip·ospf·spf·frr
小张不嚣张꒰ঌ(˚ᆺ˚)໒꒱32 分钟前
Centos7在yum当中遇到Could not resolve host: mirrorlist.centos.org解决方案
linux·运维·服务器·centos
Sally璐璐34 分钟前
CentOS企业级文件服务器终极部署指南
linux·服务器·centos
Sca_杰38 分钟前
centos指令
linux·运维·centos
几道之旅40 分钟前
centos 7 安装NVIDIA Container Toolkit
linux·运维·centos
DuelCode41 分钟前
Windows VMWare Centos Docker部署Nginx并配置对Springboot应用的访问代理
linux·运维·服务器
myloveasuka1 小时前
信号操作集函数
linux·运维·服务器·c语言·c++·vscode
亦舒.1 小时前
宝塔面板Nginx手动配置负载均衡实战指南
运维·nginx·负载均衡