1232 lines
24 KiB
C
1232 lines
24 KiB
C
///////////////////////////////////////////////////////////////////////////////////
|
||
//multi_thread_server.c
|
||
///////////////////////////////////////////////////////////////////////////////////
|
||
//本文件是多线程并发服务器的代码
|
||
#include "common.h"
|
||
|
||
|
||
void myFree(void* ptr)
|
||
{
|
||
if(ptr != NULL)
|
||
{
|
||
free(ptr);
|
||
ptr = NULL;
|
||
}
|
||
}
|
||
|
||
void pritfExit()
|
||
{
|
||
int iExit = 0;
|
||
|
||
printf("Please enter any key to exit:");
|
||
scanf("%d", &iExit);
|
||
|
||
return;
|
||
}
|
||
|
||
|
||
int strTerm(char* str)
|
||
{
|
||
char* tmpstr = (char*) malloc(strlen(str) + 1);
|
||
char* tmpstart = NULL;
|
||
char* tmpend = NULL;
|
||
int len = 0;
|
||
|
||
strcpy(tmpstr, str);
|
||
tmpstart = tmpstr;
|
||
|
||
while((*tmpstart) == ' ')
|
||
{
|
||
tmpstart++;
|
||
}
|
||
|
||
tmpend = tmpstr + strlen(str) -1;
|
||
while((*tmpend) == ' ' || (*tmpend) == '\r' || (*tmpend) == '\n')
|
||
{
|
||
tmpend--;
|
||
}
|
||
|
||
if (tmpend >= tmpstart)
|
||
{
|
||
len = tmpend - tmpstart + 1;
|
||
strncpy(str, tmpstart, len);
|
||
str[len] = '\0';
|
||
}
|
||
else
|
||
{
|
||
strcpy(str, "\0");
|
||
}
|
||
|
||
tmpstart = NULL;
|
||
tmpend = NULL;
|
||
|
||
myFree(tmpstr);
|
||
|
||
return 0;
|
||
}
|
||
|
||
int SetServerPort()
|
||
{
|
||
FILE* fr = NULL;
|
||
char strPort[MAX_CMDLINE_LENGTH];
|
||
char *pPort = NULL;
|
||
|
||
memset(strPort, 0, MAX_CMDLINE_LENGTH);
|
||
|
||
// 读取cmdlist,串行执行所有命令
|
||
fr = fopen(CONFIG, "r");
|
||
if(fr == NULL)
|
||
{
|
||
printf(" Error: Open File of %s Failed!\n", CONFIG);
|
||
return -1;
|
||
}
|
||
|
||
if (fgets(strPort, MAX_CMDLINE_LENGTH, fr) != NULL)
|
||
{
|
||
pPort = strtok(strPort, ":");
|
||
pPort = strtok(NULL, ":");
|
||
|
||
if (pPort != NULL)
|
||
{
|
||
strcpy(strPort, pPort);
|
||
strTerm(strPort);
|
||
g_port = atoi(strPort);
|
||
printf(" Read Server Port : %d Sucess!\n", g_port);
|
||
}
|
||
else
|
||
{
|
||
printf(" Error: File %s is Invaild!", CONFIG);
|
||
printf(" Error: Read Server Port Failed!");
|
||
fclose(fr);
|
||
return -1;
|
||
}
|
||
}
|
||
|
||
fclose(fr);
|
||
return 0;
|
||
}
|
||
|
||
// 执行系统命令
|
||
int thr_fn(char* para)
|
||
{
|
||
int re;
|
||
#ifdef IN_LINUX
|
||
return system(para);
|
||
#else
|
||
PVOID OldValue = NULL;
|
||
Wow64DisableWow64FsRedirection(&OldValue);
|
||
re = system(para);
|
||
Wow64RevertWow64FsRedirection(OldValue);
|
||
return re;
|
||
#endif
|
||
}
|
||
|
||
|
||
#ifdef IN_LINUX
|
||
|
||
int SendDataToClient_linux(int socketID, char *data, int iLen)
|
||
{
|
||
char buffer[MAX_BUFFER_SIZE];
|
||
|
||
if (data == NULL)
|
||
{
|
||
printf("Error: Send Data is NULL!\n");
|
||
return -1;
|
||
}
|
||
|
||
bzero(buffer,MAX_BUFFER_SIZE);
|
||
memcpy(buffer, data, iLen);
|
||
|
||
//向客户端发送buffer中的数据
|
||
if (send(socketID, buffer, iLen, 0) == -1)
|
||
{
|
||
printf("Error: Send Data Failed!\n");
|
||
return -1;
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
|
||
int RecvDataFromClient_linux(int socketID, char *buffer, int iBufLen)
|
||
{
|
||
int length = 0;
|
||
if (buffer == NULL)
|
||
{
|
||
printf("Error: Buffer is NULL!\n");
|
||
return -1;
|
||
}
|
||
|
||
memset(buffer, 0, iBufLen);
|
||
|
||
//接收客户端发送来的信息到buffer中
|
||
length = recv(socketID, buffer, iBufLen, 0);
|
||
if (length < 0 || length == 0)
|
||
{
|
||
printf("Client Exit!\n");
|
||
return 0;
|
||
}
|
||
|
||
return length;
|
||
}
|
||
|
||
void * TalkToClient_linux(void *data)
|
||
{
|
||
//void* 数据可以转换为任意类型数据
|
||
int new_server_socket = (int)data;
|
||
char buffer[MAX_CMDLINE_LENGTH];
|
||
char *pCmdType = NULL;
|
||
int iRet = 0;
|
||
|
||
//将线程状态改为unjoinable,确保线程退出后资源释放
|
||
pthread_detach(pthread_self());
|
||
memset(buffer, 0, MAX_CMDLINE_LENGTH);
|
||
|
||
//接收来自客户端的数据
|
||
if (RecvDataFromClient_linux(new_server_socket, buffer, MAX_CMDLINE_LENGTH) > 0)
|
||
{
|
||
iRet = ParseCmd((void*)new_server_socket, buffer);
|
||
if (iRet == -1)
|
||
{
|
||
printf("Error: Parse Cmd Failed!\n");
|
||
}
|
||
}
|
||
|
||
close(new_server_socket);
|
||
pthread_exit(NULL);
|
||
}
|
||
|
||
int FindProcess_linux(char *pProcessName)
|
||
{
|
||
char szCmd[MAX_BUFFER_SIZE];
|
||
char *pProc = NULL;
|
||
FILE *fr = NULL;
|
||
|
||
memset(szCmd, 0, MAX_BUFFER_SIZE);
|
||
|
||
//构造查询进程系统命令
|
||
strcpy(szCmd, "pidof ");
|
||
strcat(szCmd, pProcessName);
|
||
strcat(szCmd, " >tmp_process.txt");
|
||
|
||
//执行命令
|
||
thr_fn(szCmd);
|
||
|
||
fr = fopen("tmp_process.txt", "r");
|
||
if (fr == NULL)
|
||
{
|
||
printf("Error: Open file tmp_process.txt Failed!\n");
|
||
return -1;
|
||
}
|
||
|
||
memset(szCmd, 0, MAX_BUFFER_SIZE);
|
||
|
||
while (fgets(szCmd, MAX_BUFFER_SIZE, fr) != NULL)
|
||
{
|
||
if (strcmp("\n",szCmd) != 0)
|
||
{
|
||
fclose(fr);
|
||
return RET_OK;
|
||
}
|
||
}
|
||
|
||
fclose(fr);
|
||
return RET_FAILED;
|
||
}
|
||
|
||
int InitServer_linux()
|
||
{
|
||
int server_socket = -1;
|
||
|
||
//设置一个socket地址结构server_addr,代表服务器internet地址, 端口
|
||
struct sockaddr_in server_addr;
|
||
bzero(&server_addr,sizeof(server_addr)); //把一段内存区的内容全部设置为0
|
||
server_addr.sin_family = AF_INET;
|
||
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||
server_addr.sin_port = htons(g_port);
|
||
|
||
//创建用于internet的流协议(TCP)socket,用server_socket代表服务器socket
|
||
server_socket = socket(AF_INET, SOCK_STREAM, 0);
|
||
if (server_socket == -1)
|
||
{
|
||
printf(" Error: Create Socket Failed!\n");
|
||
close(server_socket);
|
||
return -1;
|
||
}
|
||
printf(" Create Socket Success!\n");
|
||
|
||
//把socket和socket地址结构联系起来
|
||
if( bind(server_socket,(struct sockaddr*)&server_addr,sizeof(server_addr)))
|
||
{
|
||
printf(" Error: Server Bind Port : %d Failed!\n", g_port);
|
||
close(server_socket);
|
||
return -1;
|
||
}
|
||
|
||
printf(" Server Bind Port : %d Success!\n", g_port);
|
||
|
||
//server_socket用于监听
|
||
if ( listen(server_socket, LENGTH_OF_LISTEN_QUEUE) )
|
||
{
|
||
printf(" Error: Server Listen Failed!\n");
|
||
close(server_socket);
|
||
return -1;
|
||
}
|
||
|
||
return server_socket;
|
||
}
|
||
|
||
|
||
int StartService_linux(int server_socket)
|
||
{
|
||
int new_server_socket;
|
||
int err = 0;
|
||
pthread_t child_thread;
|
||
|
||
pthread_attr_t child_thread_attr;
|
||
pthread_attr_init(&child_thread_attr);
|
||
pthread_attr_setdetachstate(&child_thread_attr,PTHREAD_CREATE_DETACHED);
|
||
|
||
if (server_socket == -1)
|
||
{
|
||
printf(" Error: Server socket ID invalid!\n");
|
||
return -1;
|
||
}
|
||
|
||
while(1) //服务器端要一直运行
|
||
{
|
||
//定义客户端的socket地址结构client_addr
|
||
struct sockaddr_in client_addr;
|
||
socklen_t length = sizeof(client_addr);
|
||
|
||
//接受一个到server_socket代表的socket的一个连接
|
||
//如果没有连接请求,就等待到有连接请求--这是accept函数的特性
|
||
//accept函数返回一个新的socket,这个socket(new_server_socket)用于同连接到的客户的通信
|
||
//new_server_socket代表了服务器和客户端之间的一个通信通道
|
||
//accept函数把连接到的客户端信息填写到客户端的socket地址结构client_addr中
|
||
new_server_socket = accept(server_socket,(struct sockaddr*)&client_addr, &length);
|
||
if (new_server_socket == -1)
|
||
{
|
||
printf("Error: Server Accept Failed!\n");
|
||
return -1;
|
||
}
|
||
|
||
printf("\nServer accept a new connect!\n");
|
||
|
||
err = 0;
|
||
if( (err = pthread_create(&child_thread, &child_thread_attr, TalkToClient_linux, (void*)new_server_socket)) != 0 )
|
||
{
|
||
printf("Error: pthread_create Failed : %s\n",strerror(errno));
|
||
return -1;
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
#else
|
||
|
||
|
||
ULONG __stdcall TalkToClient_windows(void* lpParameter)
|
||
{
|
||
SOCKET new_server_socket = ((SOCKET)lpParameter);
|
||
char buffer[MAX_CMDLINE_LENGTH];
|
||
char *pCmd = NULL;
|
||
int iRet = 0;
|
||
memset(buffer, 0, MAX_CMDLINE_LENGTH);
|
||
|
||
//接收来自客户端的数据
|
||
if (RecvDataFromClient_windows(new_server_socket, buffer, MAX_CMDLINE_LENGTH) > 0)
|
||
{
|
||
printf("Recv Data From Client: %s\n", buffer);
|
||
|
||
iRet = ParseCmd((void*)new_server_socket, buffer);
|
||
if (iRet == -1)
|
||
{
|
||
printf("Error: Parse Cmd Failed!\n");
|
||
closesocket(new_server_socket);
|
||
return -1;
|
||
}
|
||
|
||
}
|
||
|
||
|
||
closesocket(new_server_socket);
|
||
return 0;
|
||
}
|
||
|
||
int SendDataToClient_windows(SOCKET socketID, char *data, int iDataLen)
|
||
{
|
||
int iRet = 0;
|
||
char szBuffer[MAX_BUFFER_SIZE];
|
||
|
||
if (data == NULL)
|
||
{
|
||
printf("Error: Send Data is NULL!\n");
|
||
return -1;
|
||
}
|
||
|
||
memset(szBuffer, 0, MAX_BUFFER_SIZE);
|
||
memcpy(szBuffer, data, iDataLen);
|
||
|
||
//向客户端发送buffer中的数据
|
||
iRet = send(socketID, szBuffer, iDataLen, 0);
|
||
if (iRet == SOCKET_ERROR)
|
||
{
|
||
printf("Error: Send error: %d!\n", GetLastError());
|
||
return -1;
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
|
||
|
||
int RecvDataFromClient_windows(SOCKET socketID, char *buffer, int iBufLen)
|
||
{
|
||
int length = 0;
|
||
|
||
if (buffer == NULL)
|
||
{
|
||
printf("Error: Malloc Failed!\n");
|
||
return -1;
|
||
}
|
||
|
||
memset(buffer, 0, iBufLen);
|
||
//接收客户端发送来的信息到buffer中
|
||
length = recv(socketID, buffer, iBufLen, 0);
|
||
if (length == 0 || length == SOCKET_ERROR)
|
||
{
|
||
printf("Client Exit!\n");
|
||
return 0;
|
||
}
|
||
|
||
return length;
|
||
}
|
||
|
||
int InitServer_windows(SOCKET *pServer_socket)
|
||
{
|
||
WSADATA Ws;
|
||
SOCKET server_socket;
|
||
int iRet = 0;
|
||
struct sockaddr_in server_addr;
|
||
|
||
*pServer_socket = INVALID_SOCKET;
|
||
|
||
//初始化windows socket
|
||
if ( WSAStartup(MAKEWORD(2,2), &Ws) != 0 )
|
||
{
|
||
printf(" Error: Init Windows Socket Failed: %d \n", GetLastError());
|
||
return -1;
|
||
}
|
||
|
||
//设置一个socket地址结构server_addr,代表服务器internet地址, 端口
|
||
server_addr.sin_family = AF_INET;
|
||
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||
server_addr.sin_port = htons(g_port);
|
||
memset(server_addr.sin_zero, 0x00, 8);
|
||
|
||
server_socket = socket(AF_INET, SOCK_STREAM, 0);
|
||
if (server_socket == INVALID_SOCKET)
|
||
{
|
||
printf(" Error: Create Socket Failed: %d\n", GetLastError());
|
||
closesocket(server_socket);
|
||
return -1;
|
||
}
|
||
|
||
printf(" Create Socket Success!\n");
|
||
|
||
//把socket和socket地址结构联系起来
|
||
iRet = bind(server_socket,(struct sockaddr*)&server_addr,sizeof(server_addr));
|
||
if( iRet != 0)
|
||
{
|
||
printf(" Error: Server Bind Port : %d Failed!\n", g_port);
|
||
closesocket(server_socket);
|
||
return -1;
|
||
}
|
||
|
||
printf(" Server Bind Port : %d Success!\n", g_port);
|
||
|
||
//server_socket用于监听
|
||
iRet = listen(server_socket, LENGTH_OF_LISTEN_QUEUE);
|
||
if (iRet != 0)
|
||
{
|
||
printf(" Error: Server Listen Failed!\n");
|
||
closesocket(server_socket);
|
||
return -1;
|
||
}
|
||
|
||
*pServer_socket = server_socket;
|
||
return 0;
|
||
}
|
||
|
||
|
||
int StartService_windows(SOCKET server_socket)
|
||
{
|
||
//定义客户端的socket地址结构client_addr
|
||
struct sockaddr_in client_addr;
|
||
int length = sizeof(client_addr);
|
||
SOCKET new_server_socket;
|
||
HANDLE hThread = 0;
|
||
|
||
if (server_socket == INVALID_SOCKET)
|
||
{
|
||
printf("Error: Server socket ID invalid!\n");
|
||
return -1;
|
||
}
|
||
|
||
while(1) //服务器端要一直运行
|
||
{
|
||
//接受一个到server_socket代表的socket的一个连接
|
||
//如果没有连接请求,就等待到有连接请求--这是accept函数的特性
|
||
//accept函数返回一个新的socket,这个socket(new_server_socket)用于同连接到的客户的通信
|
||
//new_server_socket代表了服务器和客户端之间的一个通信通道
|
||
//accept函数把连接到的客户端信息填写到客户端的socket地址结构client_addr中
|
||
new_server_socket = accept(server_socket, (struct sockaddr*)&client_addr, &length);
|
||
if (new_server_socket == INVALID_SOCKET)
|
||
{
|
||
printf("Error: Server Accept Failed!\n");
|
||
return -1;
|
||
}
|
||
|
||
printf("\nServer accept a new connect: %s\n", inet_ntoa(client_addr.sin_addr));
|
||
|
||
hThread = CreateThread(NULL, 0, TalkToClient_windows, (void*)new_server_socket, 0, NULL);
|
||
|
||
if( hThread == 0)
|
||
{
|
||
printf("Error: CreateThread Failed\n");
|
||
return -1;
|
||
}
|
||
|
||
CloseHandle(hThread);
|
||
}
|
||
}
|
||
|
||
#endif
|
||
|
||
|
||
int CopyFileToClient(void* socketID, char *pFile)
|
||
{
|
||
FILE *fr = NULL;
|
||
int iRet = 0;
|
||
int iLen = 0;
|
||
int iTotal = 0;
|
||
char strBuffer[MAX_BUFFER_SIZE];
|
||
|
||
#ifdef IN_LINUX
|
||
int socket;
|
||
#else
|
||
SOCKET socket;
|
||
#endif
|
||
|
||
#ifdef IN_LINUX
|
||
socket = (int)socketID;
|
||
if (socket == -1)
|
||
{
|
||
printf("Error: Socket ID Error!\n");
|
||
return -1;
|
||
}
|
||
#else
|
||
socket = (SOCKET)socketID;
|
||
if (socket == INVALID_SOCKET)
|
||
{
|
||
printf("Error: Socket ID Error!\n");
|
||
return -1;
|
||
}
|
||
#endif
|
||
|
||
if (pFile == NULL)
|
||
{
|
||
printf("Error: File Name is NULL!\n");
|
||
return -1;
|
||
}
|
||
|
||
//打开文件
|
||
fr = fopen(pFile, "rb");
|
||
if(fr == NULL)
|
||
{
|
||
printf("Error: Open File : %s Failed!\n", pFile);
|
||
return -1;
|
||
}
|
||
|
||
memset(strBuffer, 0, MAX_BUFFER_SIZE);
|
||
|
||
//开始读文件的内容
|
||
while ((iLen = fread(strBuffer, sizeof(char), MAX_BUFFER_SIZE-1, fr)) > 0)
|
||
{
|
||
#ifdef IN_LINUX
|
||
iRet = SendDataToClient_linux(socket, strBuffer, iLen);
|
||
#else
|
||
iRet = SendDataToClient_windows(socket, strBuffer, iLen);
|
||
#endif
|
||
|
||
iTotal += iLen;
|
||
|
||
if (iRet == -1)
|
||
{
|
||
printf("Error: Send Data To Client Failed!\n");
|
||
fclose(fr);
|
||
return -1;
|
||
}
|
||
|
||
memset(strBuffer, 0, MAX_BUFFER_SIZE);
|
||
|
||
iRet = WaitRspFromClient(socketID);
|
||
|
||
if (iRet != RET_OK)
|
||
{
|
||
printf("Error: Wait Rsp From Client Failed!\n");
|
||
fclose(fr);
|
||
return -1;
|
||
}
|
||
}
|
||
|
||
printf("Copy File Total length: %d\n", iTotal);
|
||
|
||
fclose(fr);
|
||
return 0;
|
||
}
|
||
|
||
|
||
int RunSystemCmd(void* socketID, char *pCmd, int iWaitFlag)
|
||
{
|
||
char buf[BUFSIZE];
|
||
char cmd[MAX_CMDLINE_LENGTH];
|
||
#ifdef IN_LINUX
|
||
int filename;
|
||
#else
|
||
FILE *filename;
|
||
#endif
|
||
int iRet = 0;
|
||
char strCmd[MAX_CMDLINE_LENGTH];
|
||
|
||
#ifdef IN_LINUX
|
||
int socket;
|
||
#else
|
||
SOCKET socket;
|
||
#endif
|
||
|
||
#ifdef IN_LINUX
|
||
socket = (int)socketID;
|
||
if (socket == -1)
|
||
{
|
||
printf("Error: Socket ID Error!\n");
|
||
return -1;
|
||
}
|
||
#else
|
||
socket = (SOCKET)socketID;
|
||
if (socket == INVALID_SOCKET)
|
||
{
|
||
printf("Error: Socket ID Error!\n");
|
||
return -1;
|
||
}
|
||
#endif
|
||
|
||
if (pCmd == NULL)
|
||
{
|
||
printf("Error: Cmd is NULL!\n");
|
||
return -1;
|
||
}
|
||
|
||
memset(strCmd, 0, MAX_CMDLINE_LENGTH);
|
||
strncpy(strCmd, pCmd, MAX_CMDLINE_LENGTH);
|
||
|
||
if (iWaitFlag == CMD_ECHO)
|
||
{
|
||
memset(cmd,0,sizeof(cmd));
|
||
strcat(cmd,strCmd);
|
||
strcat(cmd," 1>fileecho 2>&1");
|
||
iRet = system(cmd);
|
||
#ifdef IN_LINUX
|
||
filename = open("fileecho",O_RDONLY );
|
||
#else
|
||
filename = fopen("fileecho","r");
|
||
#endif
|
||
if (filename == NULL)
|
||
{
|
||
iRet = -1;
|
||
return 0;
|
||
}
|
||
#ifdef IN_LINUX
|
||
if (read(filename, buf, BUFSIZE) == NULL)
|
||
#else
|
||
if (fread(buf, 1, BUFSIZE, filename) == NULL)
|
||
#endif
|
||
{
|
||
iRet = -1;
|
||
return 0;
|
||
}
|
||
#ifdef IN_LINUX
|
||
system("rm fileecho");
|
||
#endif
|
||
printf("%s\n", buf);
|
||
printf("%d\n", strlen(buf));
|
||
send(socketID, buf, BUFSIZE, 0);
|
||
if (iRet == 0)
|
||
{
|
||
iRet == CMD_ECHO;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
//执行系统命令
|
||
iRet = thr_fn(strCmd);
|
||
|
||
//需要给客户端应答
|
||
if (iWaitFlag == CMD_FALG_WAIT)
|
||
{
|
||
if (iRet == 0)
|
||
{
|
||
printf("Run %s Success!\n", pCmd);
|
||
iRet = SendRspToClient(socketID, RSP_OK);
|
||
}
|
||
else
|
||
{
|
||
iRet = SendRspToClient(socketID, RSP_FAILED);
|
||
}
|
||
|
||
//返回执行结果
|
||
if ( iRet == -1)
|
||
{
|
||
printf("Error: Send Rsp To Client Failed!\n");
|
||
}
|
||
|
||
printf("Send Rsp To Client Success!\n");
|
||
|
||
}
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
|
||
int RecvFileFromClient(void* socketID, char *pFile)
|
||
{
|
||
char strBuffer[MAX_BUFFER_SIZE];
|
||
FILE *fr = NULL;
|
||
int iRet = 0;
|
||
int iTotal = 0;
|
||
|
||
#ifdef IN_LINUX
|
||
int socket;
|
||
#else
|
||
SOCKET socket;
|
||
#endif
|
||
|
||
#ifdef IN_LINUX
|
||
socket = (int)socketID;
|
||
if (socket == -1)
|
||
{
|
||
printf("Error: Socket ID Error!\n");
|
||
return -1;
|
||
}
|
||
#else
|
||
socket = (SOCKET)socketID;
|
||
if (socket == INVALID_SOCKET)
|
||
{
|
||
printf("Error: Socket ID Error!\n");
|
||
return -1;
|
||
}
|
||
#endif
|
||
|
||
if (pFile == NULL)
|
||
{
|
||
printf("Error: FileName is NULL!\n");
|
||
return -1;
|
||
}
|
||
|
||
memset(strBuffer, 0, MAX_BUFFER_SIZE);
|
||
|
||
//打开准备写的文件
|
||
fr = fopen(pFile, "wb");
|
||
if(fr == NULL)
|
||
{
|
||
printf("Error: Open File :%s Failed!\n", pFile);
|
||
|
||
//告诉客户端:服务端出错,不用发送文件了
|
||
if (SendRspToClient(socketID, RSP_FAILED) == -1)
|
||
{
|
||
printf("Error: Send Rsp To Client Failed!\n");
|
||
}
|
||
|
||
return -1;
|
||
}
|
||
|
||
//告诉客户端:服务端准备好了,可以发送文件了
|
||
if (SendRspToClient(socketID, RSP_OK) == -1)
|
||
{
|
||
printf("Error: Send Rsp To Client Failed!\n");
|
||
}
|
||
|
||
printf("Send Rsp To Client Success!\n");
|
||
|
||
//接收来自客户端的文件数据
|
||
#ifdef IN_LINUX
|
||
iRet = RecvDataFromClient_linux(socket, strBuffer, MAX_BUFFER_SIZE);
|
||
#else
|
||
iRet = RecvDataFromClient_windows(socket, strBuffer, MAX_BUFFER_SIZE);
|
||
#endif
|
||
|
||
while ( iRet > 0)
|
||
{
|
||
iTotal += iRet;
|
||
|
||
//发送收到文件数据应答给客户端
|
||
if (SendRspToClient(socketID, RSP_OK) == -1)
|
||
{
|
||
printf("Error: Send Rsp To Client Failed!\n");
|
||
fclose(fr);
|
||
return -1;
|
||
}
|
||
|
||
if (fwrite(strBuffer, sizeof(char), iRet, fr) != iRet)
|
||
{
|
||
printf("Error: Write File Failed : %s\n", pFile);
|
||
fclose(fr);
|
||
return -1;
|
||
}
|
||
|
||
memset(strBuffer, 0, MAX_BUFFER_SIZE);
|
||
|
||
#ifdef IN_LINUX
|
||
iRet = RecvDataFromClient_linux(socket, strBuffer, MAX_BUFFER_SIZE);
|
||
#else
|
||
iRet = RecvDataFromClient_windows(socket, strBuffer, MAX_BUFFER_SIZE);
|
||
#endif
|
||
}
|
||
|
||
printf("Send File Length: %d\n", iTotal);
|
||
|
||
fclose(fr);
|
||
return 0;
|
||
}
|
||
|
||
|
||
int ParseCmd(void* socketID, char *data)
|
||
{
|
||
char *pCmdType = NULL;
|
||
char strCmd[MAX_CMDLINE_LENGTH];
|
||
char *pPara = NULL;
|
||
int iRet = 0;
|
||
|
||
memset(strCmd, 0, MAX_CMDLINE_LENGTH);
|
||
strncpy(strCmd, data, MAX_CMDLINE_LENGTH);
|
||
|
||
printf("Recv CMD: %s\n", strCmd);
|
||
|
||
//解析命令类型
|
||
pCmdType = strtok(strCmd, "[");
|
||
if (pCmdType == NULL)
|
||
{
|
||
printf("Error: Cmdlist para %s error!\n", strCmd);
|
||
return -1;
|
||
}
|
||
|
||
strTerm(pCmdType);
|
||
|
||
//解析命令
|
||
pPara = strtok(NULL, "]");
|
||
if (pPara == NULL)
|
||
{
|
||
printf("Error: Cmdlist para %s error!\n", strCmd);
|
||
return -1;
|
||
}
|
||
strTerm(pPara);
|
||
|
||
//为拷贝文件类型的命令
|
||
if (strcmp(COPYFILE, pCmdType) == 0)
|
||
{
|
||
if (CopyFileToClient(socketID, pPara) == -1)
|
||
{
|
||
printf("Error: Copy File To Client Failed!\n");
|
||
return -1;
|
||
}
|
||
|
||
printf("Copy File To Client Success!\n");
|
||
}
|
||
//为客户端发送文件类型的命令
|
||
else if (strcmp(SENDFILE, pCmdType) == 0)
|
||
{
|
||
if (RecvFileFromClient(socketID, pPara) == -1)
|
||
{
|
||
printf("Error: Receive File From Client Failed!\n");
|
||
return -1;
|
||
}
|
||
|
||
printf("Receive File From Client Success!\n");
|
||
|
||
}
|
||
else if(strcmp(CMDECHO,pCmdType) == 0)
|
||
{
|
||
|
||
if (RunSystemCmd(socketID, pPara, CMD_ECHO) == -1)
|
||
{
|
||
return -1;
|
||
}
|
||
}
|
||
//为系统命令
|
||
else if(strcmp(CMD, pCmdType) == 0)
|
||
{
|
||
//执行命令
|
||
if (RunSystemCmd(socketID, pPara, CMD_FALG_NOWAIT) == -1)
|
||
{
|
||
return -1;
|
||
}
|
||
}
|
||
//为系统命令,并等待执行完系统命令再给客户端应答
|
||
else if(strcmp(CMDWAIT, pCmdType) == 0)
|
||
{
|
||
//执行命令
|
||
if (RunSystemCmd(socketID, pPara, CMD_FALG_WAIT) == -1)
|
||
{
|
||
return -1;
|
||
}
|
||
}
|
||
//为查看某个进程是否存在
|
||
else if(strcmp(FINDPROC, pCmdType) == 0)
|
||
{
|
||
if (FindProcess(socketID, pPara) == -1)
|
||
{
|
||
printf("Error: Find Process Failed!\n");
|
||
return -1;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
printf("Error: No Support : %s\n", pCmdType);
|
||
return -1;
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
|
||
int FindProcess_windows(char *pProcessName)
|
||
{
|
||
char szCmd[MAX_BUFFER_SIZE];
|
||
char *pProc = NULL;
|
||
FILE *fr = NULL;
|
||
|
||
memset(szCmd, 0, MAX_BUFFER_SIZE);
|
||
|
||
//构造查询进程系统命令
|
||
strcpy(szCmd, "tasklist /FI \"IMAGENAME eq ");
|
||
strcat(szCmd, pProcessName);
|
||
strcat(szCmd, "\" /NH>tmp_process.txt");
|
||
|
||
//执行命令
|
||
thr_fn(szCmd);
|
||
|
||
fr = fopen("tmp_process.txt", "r");
|
||
if (fr == NULL)
|
||
{
|
||
printf("Error: Open file tmp_process.txt Failed!\n");
|
||
return -1;
|
||
}
|
||
|
||
memset(szCmd, 0, MAX_BUFFER_SIZE);
|
||
|
||
while (fgets(szCmd, MAX_BUFFER_SIZE, fr) != NULL)
|
||
{
|
||
if (strlen(szCmd) > 0)
|
||
{
|
||
pProc = strtok(szCmd, " ");
|
||
strTerm(pProc);
|
||
|
||
//找到了
|
||
if (strcmp(pProcessName, pProc) == 0)
|
||
{
|
||
fclose(fr);
|
||
return RET_OK;
|
||
}
|
||
}
|
||
|
||
memset(szCmd, 0, MAX_BUFFER_SIZE);
|
||
}
|
||
|
||
fclose(fr);
|
||
return RET_FAILED;
|
||
}
|
||
|
||
|
||
int FindProcess(void* socketID, char *data)
|
||
{
|
||
int iRet = 0;
|
||
|
||
#ifdef IN_LINUX
|
||
iRet = FindProcess_linux(data);
|
||
#else
|
||
iRet = FindProcess_windows(data);
|
||
#endif
|
||
|
||
if (iRet == -1)
|
||
{
|
||
return -1;
|
||
}
|
||
|
||
if (iRet == RET_OK)
|
||
{
|
||
iRet = SendRspToClient(socketID, RSP_OK);
|
||
}
|
||
else
|
||
{
|
||
iRet = SendRspToClient(socketID, RSP_FAILED);
|
||
}
|
||
|
||
if (iRet == -1)
|
||
{
|
||
printf("Error: Send Rsp To Client Failed!\n");
|
||
return -1;
|
||
}
|
||
|
||
printf("Send Rsp To Client Success!\n");
|
||
|
||
return 0;
|
||
}
|
||
|
||
int SendRspToClient(void* socketID, char *result)
|
||
{
|
||
int iRet = 0;
|
||
char strRsp[MAX_RSP_LENGTH];
|
||
|
||
#ifdef IN_LINUX
|
||
int socket;
|
||
#else
|
||
SOCKET socket;
|
||
#endif
|
||
|
||
#ifdef IN_LINUX
|
||
socket = (int)socketID;
|
||
if (socket == -1)
|
||
{
|
||
printf("Error: Socket ID Error!\n");
|
||
return -1;
|
||
}
|
||
#else
|
||
socket = (SOCKET)socketID;
|
||
if (socket == INVALID_SOCKET)
|
||
{
|
||
printf("Error: Socket ID Error!\n");
|
||
return -1;
|
||
}
|
||
#endif
|
||
|
||
if (result == NULL)
|
||
{
|
||
printf("Error: Send data is NULL!\n");
|
||
return -1;
|
||
}
|
||
|
||
memset(strRsp, 0, MAX_RSP_LENGTH);
|
||
|
||
//构造应答消息
|
||
strcpy(strRsp, RSP);
|
||
strcat(strRsp, "#");
|
||
strcat(strRsp, result);
|
||
|
||
#ifdef IN_LINUX
|
||
iRet = SendDataToClient_linux(socket, strRsp, strlen(strRsp));
|
||
#else
|
||
iRet = SendDataToClient_windows(socket, strRsp, strlen(strRsp));
|
||
#endif
|
||
|
||
if (iRet == -1)
|
||
{
|
||
printf("Error: Send Data To Client Failed!\n");
|
||
return -1;
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
|
||
|
||
int WaitRspFromClient(void* socketID)
|
||
{
|
||
char strRsp[MAX_RSP_LENGTH];
|
||
int iRet;
|
||
|
||
#ifdef IN_LINUX
|
||
int socket;
|
||
#else
|
||
SOCKET socket;
|
||
#endif
|
||
|
||
#ifdef IN_LINUX
|
||
socket = (int)socketID;
|
||
if (socket == -1)
|
||
{
|
||
printf("Error: Socket ID Error!\n");
|
||
return -1;
|
||
}
|
||
#else
|
||
socket = (SOCKET)socketID;
|
||
if (socket == INVALID_SOCKET)
|
||
{
|
||
printf("Error: Socket ID Error!\n");
|
||
return -1;
|
||
}
|
||
#endif
|
||
|
||
memset(strRsp, 0, MAX_RSP_LENGTH);
|
||
|
||
//接收来自客户端的应答
|
||
#ifdef IN_LINUX
|
||
iRet = RecvDataFromClient_linux(socket, strRsp, MAX_RSP_LENGTH);
|
||
#else
|
||
iRet = RecvDataFromClient_windows(socket, strRsp, MAX_RSP_LENGTH);
|
||
#endif
|
||
|
||
if (iRet <= 0)
|
||
{
|
||
return -1;
|
||
}
|
||
|
||
iRet = ParseRsp(strRsp);
|
||
|
||
return iRet;
|
||
}
|
||
|
||
int ParseRsp(char* pRsp)
|
||
{
|
||
char *pPara = NULL;
|
||
char strRsp[MAX_RSP_LENGTH];
|
||
|
||
memset(strRsp, 0, MAX_RSP_LENGTH);
|
||
|
||
if (strRsp == NULL)
|
||
{
|
||
printf("Error: strRsp is NULL!\n");
|
||
return -1;
|
||
}
|
||
|
||
strncpy(strRsp, pRsp, MAX_RSP_LENGTH);
|
||
|
||
pPara = strtok(strRsp, "#");
|
||
if (pRsp == NULL)
|
||
{
|
||
printf("Error: Rsp %s error!\n", pRsp);
|
||
return -1;
|
||
}
|
||
|
||
pPara = strtok(NULL, "#");
|
||
if (pRsp == NULL)
|
||
{
|
||
printf("Error: Rsp %s error!\n", pRsp);
|
||
return -1;
|
||
}
|
||
|
||
// 服务端应答消息为OK
|
||
if (strcmp(pPara, RSP_OK) == 0)
|
||
{
|
||
return RET_OK;
|
||
}
|
||
else if (strcmp(pPara, RSP_FAILED) == 0)
|
||
{
|
||
return RET_FAILED;
|
||
}
|
||
|
||
return -1;
|
||
|
||
}
|
||
|
||
int Init()
|
||
{
|
||
//从配置文件中获取端口
|
||
if (SetServerPort() == -1)
|
||
{
|
||
return -1;
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
|
||
|
||
int Run()
|
||
{
|
||
#ifdef IN_LINUX
|
||
int server_socket = -1;
|
||
int iRet = 0;
|
||
|
||
//初始化socket
|
||
server_socket = InitServer_linux();
|
||
if (server_socket == -1)
|
||
{
|
||
printf(" Error: InitServer Failed! Exit...\n");
|
||
return -1;
|
||
}
|
||
|
||
printf(" InitServer Success!\n");
|
||
printf(" Server is running...\n\n");
|
||
printf("**************************************\n");
|
||
|
||
iRet = StartService_linux(server_socket);
|
||
if (iRet == -1)
|
||
{
|
||
printf(" Error: StartService Failed! Exit...\n");
|
||
close(server_socket);
|
||
return -1;
|
||
}
|
||
|
||
//关闭监听用的socket
|
||
close(server_socket);
|
||
|
||
#else
|
||
|
||
SOCKET server_socket;
|
||
int iRet = 0;
|
||
|
||
//初始化socket
|
||
iRet = InitServer_windows(&server_socket);
|
||
if (iRet == -1)
|
||
{
|
||
printf(" Error: InitServer Failed! Exit...\n");
|
||
return -1;
|
||
}
|
||
|
||
printf(" InitServer Success!\n");
|
||
printf(" Server is running...\n");
|
||
printf("**************************************\n");
|
||
|
||
iRet = StartService_windows(server_socket);
|
||
if (iRet == -1)
|
||
{
|
||
printf("Error: StartService Failed! Exit...\n");
|
||
closesocket(server_socket);
|
||
WSACleanup();
|
||
return -1;
|
||
}
|
||
|
||
//关闭监听用的socket
|
||
closesocket(server_socket);
|
||
WSACleanup();
|
||
|
||
#endif
|
||
|
||
return 0;
|
||
}
|
||
|
||
|
||
int main(int argc, char **argv)
|
||
{
|
||
int iRet = 0;
|
||
|
||
printf("======================================\n");
|
||
printf(" server-tool\n");
|
||
printf(" Version: %s\n", VERSION);
|
||
printf("======================================\n");
|
||
|
||
//初始化
|
||
iRet = Init();
|
||
if (iRet == -1)
|
||
{
|
||
printf(" Error: Init Failed! Server Exiting...\n");
|
||
|
||
#ifndef IN_LINUX
|
||
pritfExit();
|
||
#endif
|
||
|
||
return -1;
|
||
}
|
||
|
||
iRet = Run();
|
||
|
||
|
||
return 0;
|
||
}
|