客户端与服务器通信项目4

11、服务器主要程序

/**
*获取服务器配置信息
*/
int getconfig(server_config *m_config, char *path)
{
	char linebuf[100] = {0};
	char client_num[100] = {0};
	char *p = NULL;
	FILE *fp = NULL;
	fp = fopen(path, "r");
	if(NULL == fp)
	{
		perror("fopen");
		return -1;
	}
	else
	{
		//获取服务器名称
		if(NULL != fgetc(linebuf, 100, fp))
		{
			p = strstr(linebuf, "=");
			strncpy(m_config->name, p+1, sizeof(linebuf) - 5);
		}
		//获取服务器最大可连接数
		if(NULL != fgets(linebuf, 100, fp))
		{
			p =strstr(linebuf, "=");
			strncpy(client_num, p+1, sizeof(linebug) - 10);
			m_config->client_max = atoi(client_num);
		}
	}
	fclose(fp);
	fp = NULL;
	return 0;
}

/*
*设置服务器配置信息
*/
int setconfig(server_config *m_config, char *path)
{
	char temp[200] = {0};
	char *p = NULL;
	file *fp = NULL;
	fp = fopen(path, "w");
	if(NULL == fp)
	{
		perror("fopen");
		return -1;
	}
	else
	{
		//将配置信息写入文件
		sprintf(temp, "%s=%s\n%s=%d", "name", m_config->name, "client_max", m_config->client_max);
		fwrite(temp, sizeof(temp), 1, fp);
		fclose(fp);
		fp = NULL;
	}
	return 0;
}

/*
*线程函数,与多个客户端之间进行通信
*/
void *thread_function(int *argc)
{
	int index = *argv;
	thread_create_flag = 0;		//表示线程创建完毕
	protocol m_protocol;		//协议结构体
	server_config m_config;		//服务器配置结构体
	file_info m_fileinfo;		//传输文件信息结构体
	int fd = -1;
	int sockerr = 0;
	//文件传输相关
	int retval = 0;				//文件传输返回字节数
	int tosend = 0;				//剩余要发送的文件大小
	char buf[BUFFER_SIZE] = {0};
	FILE *fp =NULL;
	//存放服务器配置列表的缓冲区
	char *ls_buf =NULL;
	ls_buf = malloc(BUFFER_SIZE);
	memset(ls_buf, 0, BUFFER_SIZE);
	while(1)
	{
		if(fd_state[index] >= 0)
		{
			fd = fd_state[index];
			{
				fd = fd_state[index];
				while(!sockerr)
				{
					memset(&m_protocol, 0, sizeof(protocol));
					if(read(fd, &m_protocol, sizeof(protocol)) <= 0)
					{
						perror("read");
						sockerr = 1;
					}
					else
					{
						printf("*****************"\n", m_protocol.cmd);
						printf("Receive command:%d\n", m_protocol.cmd);		
						switch(m_protocol.cmd)
						{
							//获取文件列表
							case GET_FILE_LIST:
							memset(ls_buf, 0, BUFFER_SIZE);
							ls_buf = ls_function(".");
							if(-1 == write(fd, ls_buf, BUFFER_SIZE))
							{
								perror("write");
								sockerr = 1;
							}
							break;
							//获取服务器配置参数
							case GET_SER_CONFIG:
							memset(&m_config, 0 ,sizeof(server_config));
							if(-1 == getconfig(&m_config, CONFIG_PATH))
							{
								perror("getconfig");
							}
							printf("name:%sclient_max:%d\n", m_config.name, m_config.client_max);
							if(-1 == write(fd, (char*)&m_config, sizeof(server_config)))
							{
								perror("write");
								sockerr = 1;
							}
							break;
							//设置服务器配置参数
							case SET_SER_CONFIG:
							//将接收到的服务器配置参数存储到m_config结构
							memset(&m_config, 0 ,sizeof(server_config));
							m_config.client_max = m_protocol.length;
							strncpy(m_config.name, m_protocol.name, NAME_LEN);
							//最大可连接数不能大于CLIENT_MAX
							if(m_config.client_max > CLIENT_MAX)
							{
								m_config.client_max = 10;
							}
							//最大可连接数不能小于当连接的客户端数
							if(m_config.client_max < connected)
							{
								m_config.client_max = connected;
								printf("The quantity of connected client is %d.\n", connected);
							}
							//将配置信息写入文件
							if(-1 == setconfig(&m_config, CONFIG_PATH))
							{
								memset(&m_config, 0, sizeof(server_config));
								printf("Set server config failed!\n");
							}
							else
							{
								printf("Set serer config success!\n");
								printf("name:%sclient_max:%d\n", m_config.name, m_config.client_max);
							}
							if(-1 == write(fd, (char*)&m_config, sizeof(server_config)))
							{
								perror("write");
								sockerr = 1;
							}
							break;
							//文件开始传输
							case FILE_TRANS_START:
							fp = fopen(m_protocol.name, "r");
							if(NULL == fp)
							{
								memset(&m_protocol, 0, sizeof(protocol));
								if(-1 == write(fd, (char*)&m_protocol, sizeof(protocol)))
								{
									perror("write");
									sockerr = 1;
								}
							}
							else
							{
								//计算文件大小
								fseek(fp, 0, SEEK_END);
								m_fileinfo.length = ftell(fp);
								printf("File length = %d\n", m_fileinfo.length);
								//将文件指针指向开始传输的起始点
								if(tosend > 0)
								{
									fseek(fp, m_fileinfo.length - tosend, SEEK_SET);
									m_fileinfo.length = tosend;
								}
								else
								{
									fseek(fp, 0, SEEK_SET);
									tosend = m_fileinfo.length;
								}
								printf("tosend:%d\n", tosend);
								//返回文件名与要传的文件大小
								m_protocol.length = m_fileinfo.length;
								if(-1 == write(fd, (char*)&m_protocol, sizeof(protocol)))
								{
									perror("write");
									sockerr = 1;
								}
								printf("Start sending file!\n");
								while((retval = fread(buf, 1, sizeof(buf), fp)) > 0)
								{
									write(fd, buf, retval);
									tosend -= retval;
									memset(buf, 0, BUFFER_SIZE);
									memset(&m_protocol, 0, sizeof(protocol));
									if(-1 == read(fd, (char*)&m_protocol, sizeof(protocol)))
									{
										perror("read");
										sockerr = 1;
										break;
									}
									else
									{
										//判断暂停或停止指令
										if(FILE_TRANS_PAUSE == m_protocol.cmd)
										{
											printf("*****************\n"
												   "FILE_TRANS_PAUSE!\n"
												   "tosend = %d\n", tosend);
											break;
										}
										else if(FILE_TRANS_STOP == m_protocol.cmd)
										{
											tosend = 0;
											printf("****************\n"
												   "FILE_TRANS_STOP!\n");
											break;
										}
									}
								}
								fclose(fp);
								fp = NULL;
								//传输完成
								if0 == retval)
								{
									tosend = 0;
									memset(&m_fileinfo, 0, sizeof(file_info));
									printf("Send file success!\n");
								}
							}
							break;
							//文件传输暂停
							case FILE_TRANS_PAUSE:
							//文件传输过程中接收指令
							break;
							//文件传输停止
							case FILE_TRANS_STOP:
							//文件传输过程中接收指令
							break;
							default:
							break;
						}
					}
				}
			}
			//出错或者通信完成,关闭连接,线程休眠
			printf("Client closed!\n");
			close(fd);
			tosend = 0;
			sockerr = 0;
			memset(&m_fileinfo,  0, sizeof(file_info));
			//该线程的描述符位置-1
			pthread_mutex_lock(&fd_state_lock);
			fd_state[index] = -1;
			pthread_mutex_unlock(&fd_state_lock);
			//连接客户端数-1
			pthread_mutex_lock(&connectde_lock);
			connected--;
			pthread_mutex_unlock(&connected_lock);
		}
		else
		{
			sleep(1);
		}
	}
	free(ls_buf);
}

/*
*主函数,创建socket,创建线程
*/
int main(int argc, char *argv[])
{
	//socket文件标识符
	int server_fd = -1;
	int client_fd = -1;
	//socket地址结构
	struct sockaddr_in server_addr;
	struct sockaddr_in client_addr;
	int retval = -1;			//收发消息返回值
	int reuse_flag = 1;			//端口重用标志位
	int socklen = 0;			//socket地址结构长度
	socklen = sizeof(struct sockadddr_in);
	server_config m_config;		//服务器配置
	int i = 0;
	pthread_t thread_handle[CLIENT_MAX] = {0};	//线程句柄
	//创建一个套接字
	server_fd = socket(AF_INET, SOCK_STREAM, 0);
	if(server_fd == -1)
	{
		perror("socker");
		return -1;
	}
	//开启端口复用
	if(-1 == setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &reuse_flag, sizeof(reuse_flag)))
	{
		perror("setsockopt");
		close(server_fd);
		return -1;
	}
	//配置服务器地址与端口
	bzero(&server_addr, sizeof(struct sockaddr_in));
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(SOCKET_PORT);
	server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
	//服务器端口绑定
	if(- == bind(server_fd, (struct sockaddr*)&server_addr, socklen))
	{
		perror("bind");
		close(server_fd);
		return -1;
	}
	//开始监听
	if(-1 == listen(server_fd, CLIENT_MAX))
	{
		perror("listen");
		close(server_fd");
		return -1;
	}
	printf("Server start!\n");
	//初始化socket描述数组
	memset(fd_state, -1, sizeof(fd_state));
	while(1)
	{
		//监听到客户端的连接
		client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &socklen);
		if(client_fd == -1)
		{
			perror("accept");
			close(client_fd);
			return -1;
		}
		else
		{
			printf("IP:%s,Port:%d!\n", inet_ntoa(client_addr.sin_addr), ntos(client_addr.sin_port));
			pthread_mutex_lock(&connected_lock);
			connected++;
			pthread_mutex_unlock(&connected_lock);
		}
		//获取服务器配置
		if(-1 == getconfig(&m_config, CONFIG_PATH))
		{
			perror("getconfig");
			return -1;
		}
		//已达到最大连接数量,关闭连接
		if(connected > m_config.client_max)
		{
			printf("The max quantity of client is %d,can't connect more client,closing it!\n", m_config.client_max);
			close(client_fd);
			pthread_mutex_lock(&connected_lock);
			connected--;
			pthread_mutex_unlock(&connected_lock);
			continue;
		}
		//创建一个客户端线程
		for(i=0;i<CLIENT_MAX;i++)
		{
			if(fd_state[i] < 0)
			{
				thread_create_flag = 1;
				if(pthread_create(&thread_handle[i], NULL, (void*)thread_function, &i) != 0)
				{
					printf("thread %d create failed!\n", i);
					return -1;
				}
				//等待线程创建完毕
				while(thread_create_flag)
				//修改socket描述数组
				pthread_mutex_lock(&fd_state_lock);
				fd_state[i] - client_fd;
				pthread_mutex_unlock(&fd_state_lock);
				break;
			}
		}
	}
	close(server_fd);
	//销毁互斥
	pthread_mutex_destroy(&fd_state_lock);
	pthread_mutex_destroy(&connected_lock);
	return 0;
}
	
	
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值