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:
memset(&m_config, 0 ,sizeof(server_config));
m_config.client_max = m_protocol.length;
strncpy(m_config.name, m_protocol.name, NAME_LEN);
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;
if(0 == 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));
pthread_mutex_lock(&fd_state_lock);
fd_state[index] = -1;
pthread_mutex_unlock(&fd_state_lock);
pthread_mutex_lock(&connectde_lock);
connected--;
pthread_mutex_unlock(&connected_lock);
}
else
{
sleep(1);
}
}
free(ls_buf);
}
int main(int argc, char *argv[])
{
int server_fd = -1;
int client_fd = -1;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
int retval = -1;
int reuse_flag = 1;
int socklen = 0;
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");
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)
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;
}