linux下多人聊天室

客户端

一个在Linux下可以使用的聊天软件,要求至少实现如下功能:
1. 采用Client/Server架构
2. Client A 登陆聊天服务器前,需要注册自己的ID和密码
3. 注册成功后,Client A 就可以通过自己的ID和密码登陆聊天服务器
4. 多个Client X 可以同时登陆聊天服务器之后,与其他用户进行通讯聊天
5. Client A成功登陆后可以查看当前聊天室内其他在线用户Client x
6. Client A可以选择发消息给某个特定的Client X,即”邮件”功能
7. Client A 可以选择发消息全部的在线用户,即”群发消息”功能


可以选择实现的附加功能:
送欢迎你来到咱们的聊天室”

附加功能:

文件传输

部分主要代码:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <memory.h>
#define SIZE sizeof(X)
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include<time.h>

typedef struct xinxi
{
	char id[20];
	char passwd[20];
	char name[50];
	char hy[100][20];
	int hys;
	int online;//0不在线,1在线
	int fd;//
	struct xinxi *next;
	
}X;
typedef struct message
{
	char id[20];//收的人
	char id1[20];//发的人
	int type;//好友请求1,私聊2,好友请求回复信息3
	char mess[1024];
	struct message * next;
	
}M;
int count=0;
X* head=NULL;
int count1=0;
M* head1=NULL;
int zhzx(char id[20]);
void baocun()
{
	FILE *fp;
	fp=fopen("123","w");
	if(fp==NULL)
	{
		printf("open error\n");
		return;
	}
	printf("count=%d",count);
	fwrite(&count,sizeof(int),1,fp);
	
	X* p;
	p=head;

	if(p->next==NULL)
	{
		
		fclose(fp);
		return ;
	}
	
	p=p->next;
	while(p)
	{
		fwrite(p,sizeof(X),1,fp);
		p=p->next;
		
	}

	printf("保存成功\n");
	fclose(fp);
	

}
void baocun1()
{
	FILE *fp;
	fp=fopen("1234","w");
	if(fp==NULL)
	{
		printf("open error\n");
		return;
	}
	printf("count=%d",count1);
	fwrite(&count1,sizeof(int),1,fp);
	
	M* p;
	p=head1;

	if(p->next==NULL)
	{
		
		fclose(fp);
		return ;
	}
	
	p=p->next;
	while(p)
	{
		fwrite(p,sizeof(X),1,fp);
		p=p->next;
		
	}

	printf("xx保存成功\n");
	fclose(fp);
	

}
X * create()
{
	X * x;
	x=(X*)malloc(SIZE);
	if(x==NULL)
	{
		printf("create error\n");
		return NULL;
	}
	x->next=NULL;
	FILE* fp;
	fp=fopen("123","a+");
	if(fp==NULL)
	{
		printf("open error\n");
		return NULL;
	}
	if(fgetc(fp)==EOF)
	{
		fclose(fp);
		return x;
	}
	rewind(fp);
	int n;
	fread(&n,sizeof(int),1,fp);
	printf("n=%d\n",n);
	count=n;
	X t;
	X *p,*p1;
	int i;
	for(i=0;i<n;i++)
	{
		fread(&t,sizeof(X),1,fp);
		p1=x;
		while(p1->next)
		{
			p1=p1->next;
		}
		p=(X*)malloc(sizeof(X));
		p->next=NULL;
		strcpy(p->id,t.id);
		strcpy(p->name,t.name);	
		strcpy(p->passwd,t.passwd);
		
		p->hys=t.hys;
		int j;
		for(j=0;j<p->hys;j++)
			strcpy(p->hy[j],t.hy[j]);
		p->fd=-5;
		p->online=0;
		p1->next=p;
		
		
	}
	fclose(fp);
	return x;
}
M * create1()
{
	M * x;
	x=(M*)malloc(sizeof(M));
	if(x==NULL)
	{
		printf("create error\n");
		return NULL;
	}
	x->next=NULL;
	FILE* fp;
	fp=fopen("1234","a+");
	if(fp==NULL)
	{
		printf("open error\n");
		return NULL;
	}
	if(fgetc(fp)==EOF)
	{
		fclose(fp);
		return x;
	}
	rewind(fp);
	int n;
	fread(&n,sizeof(int),1,fp);
	printf("n=%d\n",n);
	count1=n;
	M t;
	M *p,*p1;
	int i;
	for(i=0;i<n;i++)
	{
		fread(&t,sizeof(M),1,fp);
		p1=x;
		while(p1->next)
		{
			p1=p1->next;
		}
		p=(M*)malloc(sizeof(M));
		p->next=NULL;
		strcpy(p->id,t.id);
		strcpy(p->id1,t.id1);
		p->type=t.type;
		strcpy(p->mess,t.mess);
		p1->next=p;
		
	}
	fclose(fp);
	return x;
}
int isnum1(char s[20])
{
	int i=0;
	while(s[i])
	{
		if(!isdigit(s[i]))
			return 0;
		i++;
		
	}
	return 1;
	
}
void add(int fd)//注册
{
	X *p1,*p,*p2;
	int leap=0;
	p=(X*)malloc(SIZE);
	if(p==NULL)
		return;
	char str[256];
	char str1[256];
	memset(str,0,256);
	memset(str1,0,256);
	strcpy(str,"请输入你想注册的账号");
	send(fd,str,strlen(str),0);
	memset(str,0,256);
	
	recv(fd,str,256,0);
	strcpy(str1,str);
//	strcpy(p->id,str);
	if(!isnum1(str))
	{
		memset(str,0,256);
		strcpy(str,"请输入纯数字账号!\n");
		send(fd,str,strlen(str),0);

		return;
	}
	p1=head;
	while(p1->next)
	{
		if(strcmp(p1->next->id,str)==0)
		{	
			leap=1;
			break;
		}
		p1=p1->next;
	}
	if(leap==1)
	{
		memset(str,0,256);
		strcpy(str,"账号重复\n");
		send(fd,str,strlen(str),0);		

		return;
	}
	strcpy(p->id,str1);
	memset(str,0,256);	
	strcpy(str,"请输入密码");
	send(fd,str,strlen(str),0);			
	

	memset(str,0,256);	
	recv(fd,str,256,0);
	strcpy(p->passwd,str);

	me
  • 6
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值