#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include “…/include/tpool.h”
#define u_short unsigned int
struct UDPackage
{
int clientSock;
struct sockaddr_in client_addr;
char* buf;
};
char* getFileName(char s[])
{
for (int i = 0 ; i < strlen(s) ; i ++ )
{
if (s[i] == ‘/’)
{
s[i] = ‘-’;
}
}
return s;
}
char* getNowtime()
{
static char s[30]={0};
char YMD[15] = {0};
char HMS[10] = {0};
time_t current_time;
struct tm* now_time;
char *cur_time = (char *)malloc(21*sizeof(char));
time(¤t_time);
now_time = localtime(¤t_time);
strftime(YMD, sizeof(YMD), "%F_", now_time);
strftime(HMS, sizeof(HMS), "%T", now_time);
strncat(cur_time, YMD, 11);
strncat(cur_time, HMS, 8);
// printf("\nCurrent time: %s\n\n", cur_time);
memcpy(s, cur_time, strlen(cur_time)+1);
free(cur_time);
cur_time = NULL;
return s;
}
int tcpRecv(int client_id, char buf[])
{
int len = recv(client_id, buf, maxLen, 0);
if (len < 0)
{
perror(“recv error! “);
return -1;
}
else
{
printf(”%d+++++%d======%d\n”, len, strlen(buf), maxLen);
char recvSuccess[1000] = “Success”;
send(client_id, recvSuccess, sizeof(recvSuccess), 0);
return len;
}
}
void* tcpRequest(void* clientpt)
{
puts(“tcp trans…”);
int client_id = (int)clientpt;
char buf[100024];
bzero(buf, sizeof(buf));
if (!~tcpRecv(client_id)) return NULL;
strcpy(buf, tcpRecv(client_id));
printf("%s\n", buf);
if (!strcmp(buf, "upload"))
{
char fileName[10024];
bzero(fileName, sizeof(fileName));
if (!~tcpRecv(client_id)) return NULL;
strcpy(fileName, tcpRecv(client_id));
char file[10086];
struct sockaddr_in client;
socklen_t client_len = sizeof(client);
getpeername(client_id, (struct sockaddr*)&client, &client_len);
// save
// 文件名:serverIP + filename + time
strncpy(file, "../doc/download/", 1000);
strncat(file, inet_ntoa(client.sin_addr), 1000); // IP
strncat(file, getNowtime(), 1000); // time
strncat(file, fileName, 1000); // filename
printf("%s*****\n", file);
FILE* fp = fopen(file, "w+");
if (fp == NULL)
{
perror("fopen error! ");
if (clientpt) free(clientpt);
close(client_id);
return NULL;
}
bzero(buf, sizeof(buf));
int len = tcpRecv(client_id);
while (len)
{
int writeLen = fwrite(buf, sizeof(char), len, fp);
if (writeLen < len)
{
perror("write error! ");
break;
}
bzero(buf, sizeof(buf));
}
fclose(fp);
}
else if (!strcmp(buf, "download"))
{
bzero(buf, sizeof(buf));
int len = tcpRecv(client_id);
char filerror[30] = "~file_not_exist";
if (!~access(buf, 0))
{
send(client_id, filerror, sizeof(filerror), 0);
close(client_id);
if (clientpt) free(clientpt);
return NULL;
}
FILE* fp = fopen(buf, "r+");
if (fp == NULL)
{
perror("fopen error! ");
if (clientpt) free(clientpt);
close(client_id);
return NULL;
}
bzero(buf, sizeof(buf));
while ((len = fread(buf, sizeof(char), sizeof(buf), fp)) > 0)
{
if (send(client_id, buf, sizeof(buf), 0) < 0)
{
perror("send error! ");
break;
}
bzero(buf, sizeof(buf));
}
}
else
{
puts("参数错误!");
}
// int idx = -1;
// for (int i = 0 ; i < strlen(buf) ; i ++ )
// {
// if (buf[i] == ' ')
// {
// idx = i;
// break;
// }
// }
// char filePath[100024];
// char filerror[30] = "~file_not_exist";
// if (strcmp(strncpy(buf, buf, idx), "download"))
// {
// int j = 0;
// for (int i = idx+1 ; i < strlen(buf) ; i ++ , j ++ )
// {
// filePath[j] = buf[i];
// }
// if (!~access(filePath, 0))
// {
// send(client_id, filerror, sizeof(filerror), 0);
// close(client_id);
// if (clientpt) free(clientpt);
// return NULL;
// }
// FILE* fp = fopen(filePath, "w+");
// if (fp == NULL)
// {
// perror("fopen error! ");
// if (clientpt) free(clientpt);
// close(client_id);
// return NULL;
// }
// int len = fread(file, sizeof(char), sizeof(file), fp);
// strncat(buf, file, 10024);
// if (send(tcp_sock, buf, sizeof(buf), 0) < 0)
// {
// perror("send error! ");
// if (transPt) free(transPt);
// close(tcp_sock);
// fclose(fp);
// return NULL;
// }
// fclose(fp);
// }
// else if (strcmp(strncpy(buf, buf, idx), "upload"))
// {
// }
// else
// {
// perror("参数错误!");
// }
}
void* udpRequest(void* udpPackagePt)
{
puts(“udp trans…”);
struct UDPackage udpPackage = (struct UDPackage)udpPackagePt;
int i;
for (i = 0 ; ; i ++ )
{
if (udpPackage.buf[i] == ’ ’ || udpPackage.buf[i] == ‘\n’) break;
}
char transType[10024];
strncpy(transType, udpPackage.buf, i);
if (!strcmp(transType, "upload"))
{
char file[10086];
strncpy(file, "~/upload/", 1000);
strncat(file, inet_ntoa(udpPackage.client_addr.sin_addr), 1000);
char filename[10086];
i += 1;
for (int j = 0 ; ; i ++ , j ++ )
{
if (udpPackage.buf[i] == '\n') break;
filename[j] = udpPackage.buf[i];
}
strncat(file, getFileName(filename), 1000);
strncat(file, getNowtime(), 1000); // time
FILE* fp = fopen(file, "w+");
if (fp == NULL)
{
perror("fopen error! ");
close(udpPackage.clientSock);
if (udpPackagePt) free(udpPackagePt);
return NULL;
}
char content[10086];
i += 1;
for (int j = 0 ; i < strlen(udpPackage.buf) ; i ++ , j ++ )
{
content[j] = udpPackage.buf[i];
}
int len = strlen(content);
int writeLen = fwrite(content, sizeof(char), len, fp);
if (writeLen < len)
{
perror("write error! ");
}
fclose(fp);
}
else if (!strcmp(transType, "download"))
{
char filePath[10024];
i += 1;
for (int j = 0 ; i < strlen(udpPackage.buf) ; i ++ , j ++ )
{
filePath[j] = udpPackage.buf[i];
}
char filerror[30] = "~file_not_exist";
if (!~access(filePath, 0))
{
close(udpPackage.clientSock);
if (udpPackagePt) free(udpPackagePt);
return NULL;
}
FILE* fp = fopen(filePath, "r+");
if (fp == NULL)
{
perror("fopen error! ");
close(udpPackage.clientSock);
if (udpPackagePt) free(udpPackagePt);
return NULL;
}
char file[10024];
int len = fread(file, sizeof(char), sizeof(file), fp);
if (sendto(udpPackage.clientSock, file, sizeof(file), 0, (struct sockaddr *)&udpPackage.client_addr, sizeof(udpPackage.client_addr)) < 0)
{
perror("send error! ");
if (udpPackagePt) free(udpPackagePt);
close(udpPackage.clientSock);
}
fclose(fp);
}
else
{
perror("参数错误!");
}
}
int main()
{
u_short tcp_port = 55557;
u_short udp_port = 55558;
int tcp_sock = socket(AF_INET, SOCK_STREAM, 0);
int udp_sock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in tcp_server_addr;
struct sockaddr_in udp_server_addr;
struct sockaddr_in udp_client_addr;
bzero(&tcp_server_addr, sizeof tcp_server_addr);
bzero(&udp_server_addr, sizeof udp_server_addr);
bzero(&udp_client_addr, sizeof udp_client_addr);
// https://blog.csdn.net/u012206617/article/details/105851649
int opt = 1;
setsockopt(tcp_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
setsockopt(udp_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
tcp_server_addr.sin_family = AF_INET;
tcp_server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
tcp_server_addr.sin_port = htons(tcp_port);
udp_server_addr.sin_family = AF_INET;
udp_server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
udp_server_addr.sin_port = htons(udp_port);
udp_client_addr.sin_family = AF_INET;
udp_client_addr.sin_addr.s_addr = htonl(INADDR_ANY);
udp_client_addr.sin_port = htons(55555);
// 将服务器地址信息贴到接收器上
if (bind(tcp_sock, (struct sockaddr *)&tcp_server_addr, sizeof tcp_server_addr) < 0) perror("tcp bind error! ");
if (bind(udp_sock, (struct sockaddr *)&udp_server_addr, sizeof udp_server_addr) < 0) perror("udp bind error! ");
// 监听接收器
if (listen(tcp_sock, 128)) perror("listen tcp error! "); // 128: 允许同时向服务器发起请求的数量
char path[] = "../config/tpoolconf.json";
printf("%s\n", path);
void* tpool = tpool_init(path);
if (tpool == NULL) {
puts("create tpool error!");
return 0;
}
while (1)
{
char buf[1024];
bzero(buf, sizeof(buf));
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
// 客户端的连接号 connect之后返回id
int tcp_client = accept(tcp_sock, (struct sockaddr *)&client_addr, &client_addr_len);
// 接受此请求,同时将client端的地址和端口等信息写入client_addr中
int udp_client_addr_len = sizeof(struct sockaddr_in);
int len = recvfrom(udp_sock, buf, sizeof(buf), 0, (struct sockaddr *)&udp_client_addr, (socklen_t*)&udp_client_addr_len);
printf("%d %s\n", len, buf);
if (tcp_client)
{
int* tcp_clientpt = &tcp_client;
if (tpool_add_work(tpool, tcpRequest, tcp_clientpt) < 0)
{
tpool_destroy(tpool, 0);
perror("add work error!");
return 1;
}
}
if (len > 0)
{
struct UDPackage stUDPackage;
stUDPackage.client_addr = udp_client_addr;
stUDPackage.clientSock = udp_sock;
stUDPackage.buf = buf;
struct UDPackage* stUDPackagePt = &stUDPackage;
if (tpool_add_work(tpool, udpRequest, stUDPackagePt) < 0)
{
tpool_destroy(tpool, 0);
perror("add work error!");
return 1;
}
}
}
tpool_destroy(tpool, 1);
close(tcp_sock);
close(udp_sock);
return 0;
}