2020-01-13 10:27:00 -05:00

1300 lines
25 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

///////////////////////////////////////////////////////////////////////////////////
// multi_thread_client.c
///////////////////////////////////////////////////////////////////////////////////
//本文件是客户机多线程多次重复与服务交互的代码
#include "common.h"
void myFree(void* ptr)
{
if(ptr != NULL)
{
free(ptr);
ptr = NULL;
}
}
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;
}
void pritfExit()
{
int iExit = 0;
printf("Please enter any key to exit:");
scanf("%d", iExit);
return;
}
#ifdef IN_LINUX
long send_all(int fd, char *message, int len)
{
int sended = 0;
int this_time = len;
char *p = message;
while (sended < len)
{
this_time = send(fd, p + sended, this_time, 0);
if (this_time < 0)
if (errno == EINTR)
{
continue;
}
else
{
printf("send error.(%s)", strerror(errno));
return -1;
}
sended += this_time;
}
return sended;
}
int SendDataToServer_linux(int socketID, char *data, int iLen)
{
int iRet = 0;
if (data == NULL)
{
printf("Error: Send Data is NULL!\n");
return -1;
}
char buffer[MAX_BUFFER_SIZE];
bzero(buffer,MAX_BUFFER_SIZE);
memcpy(buffer, data, iLen);
//向服务端发送buffer中的数据
if ((iRet = send_all(socketID, buffer, iLen)) != iLen)
{
printf("Error: Send Data Failed!\n");
return -1;
}
return 0;
}
int RecvDataFromServer_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)
{
printf("Error: Client Recieve Data Failed!\n");
return -1;
}
else if (length == 0)
{
printf("Server Exit!\n");
return 0;
}
return length;
}
int ConnectServer_linux(const char* pServerIP, int iServerPort, int iTimeout)
{
int iReConnectTimes = 0;
if (pServerIP == NULL)
{
printf("Error: ServerIP is NULL!\n");
return -1;
}
//设置一个socket地址结构client_addr,代表客户机internet地址, 端口
struct sockaddr_in client_addr;
bzero(&client_addr,sizeof(client_addr)); //把一段内存区的内容全部设置为0
client_addr.sin_family = AF_INET; //internet协议族
client_addr.sin_addr.s_addr = htons(INADDR_ANY);//INADDR_ANY表示自动获取本机地址
client_addr.sin_port = htons(0); //0表示让系统自动分配一个空闲端口
//创建用于internet的流协议(TCP)socket,用client_socket代表客户机socket
int client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1)
{
printf("Error: Create Socket Failed!\n");
close(client_socket);
return -1;
}
//把客户机的socket和客户机的socket地址结构联系起来
if( bind(client_socket, (struct sockaddr*)&client_addr, sizeof(client_addr)))
{
printf("Error: Client Bind Port Failed!\n");
close(client_socket);
return -1;
}
//设置一个socket地址结构server_addr,代表服务器的internet地址, 端口
struct sockaddr_in server_addr;
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family = AF_INET;
//inet_aton比起inet_addr来可以通过返回值检测IP是否有效
if (inet_aton(pServerIP, &server_addr.sin_addr) == 0) //服务器的IP地址来自程序的参数
{
printf("Error: Server IP Address Error!\n");
return -1;
}
server_addr.sin_port = htons(iServerPort);
socklen_t server_addr_length = sizeof(server_addr);
//向服务器发起连接,连接成功后client_socket代表了客户机和服务器的一个socket连接
//支持失败后重连
while (connect(client_socket, (struct sockaddr*)&server_addr, server_addr_length) < 0)
{
iReConnectTimes ++;
if (iReConnectTimes > iTimeout)
{
printf("Error: Has Reconnect %d Times! Connect To %s:%d Failed!\n", iReConnectTimes, pServerIP, iServerPort);
return -1;
}
printf("Error: Can Not Connect To %s:%d ! Reconnect : %d ...\n", pServerIP, iServerPort, iReConnectTimes);
sleep(2);
}
return client_socket;
}
void* Thread_linux(void* para)
{
int iRet = 0;
iRet = TalkToServer(para, MAX_CONNECT_TIMEOUT);
if (iRet == -1)
{
printf("Error: Talk To Server Failed!\n");
}
pthread_exit(NULL);
}
#else
int SendDataToServer_windows(int socketID, char *data, int iBufLen)
{
char buffer[MAX_BUFFER_SIZE];
int iRet = 0;
if (data == NULL)
{
printf("Error: Send Data is NULL!\n");
return -1;
}
memset(buffer, 0, MAX_BUFFER_SIZE);
memcpy(buffer, data, iBufLen);
//向客户端发送buffer中的数据
iRet = send(socketID, buffer, iBufLen, 0);
if (iRet != iBufLen)
{
printf("Error: send error: %d!\n", GetLastError());
return -1;
}
return 0;
}
int RecvDataFromServer_windows(int socketID, char *buffer, int iBufLen)
{
int length = 0;
if (buffer == NULL)
{
printf("Error: Malloc Failed!\n");
return -1;
}
//接收客户端发送来的信息到buffer中
length = recv(socketID, buffer, iBufLen, 0);
if (length == 0 || length == SOCKET_ERROR)
{
printf("Socket Close!\n");
return 0;
}
return length;
}
ULONG __stdcall Thread_windows(void *para)
{
int iRet = 0;
iRet = TalkToServer(para, MAX_CONNECT_TIMEOUT);
if (iRet == -1)
{
printf("Error: Talk To Server Failed!\n");
return -1;
}
return 0;
}
SOCKET ConnectServer_windows(const char* pServerIP, int iServerPort, int iTimeout)
{
WSADATA Ws;
struct sockaddr_in server_addr;
SOCKET client_socket;
int iRet;
int iReConnectTimes = 0;
if (pServerIP == NULL)
{
printf("Error: ServerIP is NULL!\n");
return -1;
}
//初始化windows socket
if (WSAStartup(MAKEWORD(2, 2), &Ws) != 0)
{
printf("Error: Init Windows Socket Failed: %d !\n", GetLastError());
return -1;
}
//创建用于internet的流协议(TCP)socket,用client_socket代表客户机socket
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == INVALID_SOCKET)
{
printf("Error: Create Socket Failed!\n");
closesocket(client_socket);
return -1;
}
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(pServerIP);
server_addr.sin_port = htons(iServerPort);
memset(server_addr.sin_zero, 0x00, 8);
//向服务器发起连接,连接成功后client_socket代表了客户机和服务器的一个socket连接
//支持失败后重连
while (connect(client_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == SOCKET_ERROR)
{
iReConnectTimes ++;
if (iReConnectTimes >= iTimeout)
{
printf("Error: Has Reconnect %d Times! Connect To %s:%d Failed!\n", iReConnectTimes, pServerIP, iServerPort);
return -1;
}
printf("Error: Can Not Connect To %s:%d ! Reconnect : %d ...\n", pServerIP, iServerPort, iReConnectTimes);
Sleep(2000);
}
return client_socket;
}
#endif
int TalkToServer(void* para, int iTimeout)
{
int iRet = 0;
int iPort = 0;
char strIP[MAX_IP_LENGTH];
char strCmd[MAX_CMDLINE_LENGTH];
char *pCmdType = NULL;
char *pPara = NULL;
#ifdef IN_LINUX
int client_socket;
#else
SOCKET client_socket;
#endif
memset(strIP, 0, MAX_IP_LENGTH);
memset(strCmd, 0, MAX_CMDLINE_LENGTH);
iRet = ParseCmd((char*)para, strIP, &iPort, strCmd);
if (iRet == -1)
{
printf("Error: Parse CMD Failed!\n");
return -1;
}
#ifdef IN_LINUX
client_socket = ConnectServer_linux(strIP, iPort, iTimeout);
if (client_socket == -1)
{
printf("Error: ConnectServer Failed!\n");
return -1;
}
#else
client_socket = ConnectServer_windows(strIP, iPort, iTimeout);
if (client_socket == INVALID_SOCKET)
{
printf("Error: ConnectServer Failed!\n");
return -1;
}
#endif
printf("Client Connect Server %s Success!\n", strIP);
//解析发送给服务端的命令类型
pCmdType = strtok(strCmd, "[");
if (pCmdType == NULL)
{
printf("Error: Cmdlist para %s error!\n", para);
#ifdef IN_LINUX
close(client_socket);
#else
closesocket(client_socket);
#endif
return -1;
}
strTerm(pCmdType);
//解析发送给服务端的命令参数
pPara = strtok(NULL, "]");
if (pPara == NULL)
{
printf("Error: Cmdlist para %s error!\n", para);
#ifdef IN_LINUX
close(client_socket);
#else
closesocket(client_socket);
#endif
return -1;
}
strTerm(pPara);
//为拷贝文件类型的命令
if (strcmp(COPYFILE, pCmdType) == 0)
{
if (CopyFileFromServer((void*)client_socket, pPara) == -1)
{
printf("Error: Copy File From Server Failed!\n");
#ifdef IN_LINUX
close(client_socket);
#else
closesocket(client_socket);
#endif
return -1;
}
}
//只负责下发系统命令,不管执行的情况
else if(strcmp(CMD, pCmdType) == 0)
{
iRet = SendCmdToServer((void*)client_socket, pPara, CMD_FALG_NOWAIT);
if (iRet == -1)
{
printf("Error: Send Data To Server Failed!\n");
#ifdef IN_LINUX
close(client_socket);
#else
closesocket(client_socket);
#endif
return -1;
}
}
else if(strcmp(CMDECHO,pCmdType) == 0)
{
iRet = SendCmdToServer((void*)client_socket, pPara, CMD_ECHO);
if (iRet == -1)
{
printf("Error: Send Data To Server Failed!\n");
#ifdef IN_LINUX
close(client_socket);
#else
closesocket(client_socket);
#endif
return -1;
}
}
//为等待客户端执行完系统命令
else if(strcmp(CMDWAIT, pCmdType) == 0)
{
iRet = SendCmdToServer((void*)client_socket, pPara, CMD_FALG_WAIT);
if (iRet == -1)
{
printf("Error: Send Data To Server Failed!\n");
#ifdef IN_LINUX
close(client_socket);
#else
closesocket(client_socket);
#endif
return -1;
}
}
//为发送文件类型的命令
else if (strcmp(SENDFILE, pCmdType) == 0)
{
if (SendFileToServer((void*)client_socket, pPara) == -1)
{
printf("Error: Send File To Server Failed!\n");
#ifdef IN_LINUX
close(client_socket);
#else
closesocket(client_socket);
#endif
return -1;
}
}
//为发送文件类型的命令
else if (strcmp(FINDPROC, pCmdType) == 0)
{
if (FindProcessFromServer((void*)client_socket, pPara) == -1)
{
printf("Error: Find Process From Server Failed!\n");
#ifdef IN_LINUX
close(client_socket);
#else
closesocket(client_socket);
#endif
return -1;
}
}
else
{
printf("Error: No Support : %s\n", pCmdType);
#ifdef IN_LINUX
close(client_socket);
#else
closesocket(client_socket);
#endif
return -1;
}
#ifdef IN_LINUX
close(client_socket);
#else
closesocket(client_socket);
#endif
return 0;
}
int SendCmdToServer(void* socketID, char* para, int isWaitFlag)
{
int iRet = 0;
char buf[BUFSIZE];
char strCmd[MAX_CMDLINE_LENGTH];
memset(strCmd, 0, MAX_CMDLINE_LENGTH);
if (CMD_FALG_NOWAIT == isWaitFlag)
{
strcpy(strCmd, CMD);
}
else if(CMD_ECHO == isWaitFlag)
{
strcpy(strCmd, CMDECHO);
}
else
{
strcpy(strCmd, CMDWAIT);
}
strcat(strCmd, "[");
strcat(strCmd, para);
strcat(strCmd, "]");
iRet = strlen(strCmd);
#ifdef IN_LINUX
iRet = SendDataToServer_linux((int)socketID, strCmd, iRet);
#else
iRet = SendDataToServer_windows((SOCKET)socketID, strCmd, iRet);
#endif
if ( iRet == -1)
{
printf("Error: Send Cmd To Server Failed: %s\n", para);
return -1;
}
printf("Send Cmd To Server Success: %s\n", para);
//不等待
if (isWaitFlag == CMD_FALG_NOWAIT)
{
return 0;
}
//等待服务端的应答
else if(isWaitFlag == CMD_ECHO)
{
memset(buf, 0, BUFSIZE);
recv((int)socketID, buf, BUFSIZE, 0);
printf("%s",buf);
while(recv((int)socketID, buf, BUFSIZE, 0) != 0)
{
printf("%s",buf);
}
}
else
{
iRet = WaitRspFromServer(socketID);
if ( iRet == -1)
{
printf("Error: Wait for Rsp From Server Failed!\n");
return -1;
}
else if (iRet == RET_OK)
{
printf("---------\n");
printf("Response: OK!\n");
}
else if (iRet == RET_FAILED)
{
printf("---------\n");
printf("Response: Failed!\n");
printf(" Server Run Cmd Failed! No such file or directory, or permission denied ...\n Please Check!\n");
}
printf("---------\n");
}
return iRet;
}
int ParseCmd(char *data, char *ip, int *port, char *cmd)
{
//解析命令参数
unsigned char str[MAX_CMDLINE_LENGTH];
char * pServerIP = NULL;
char * pServerPort = NULL;
char * pCmd = NULL;
memset(str, 0, MAX_CMDLINE_LENGTH);
strncpy(str, data, MAX_CMDLINE_LENGTH);
//解析服务端IP
pServerIP = strtok(str, "#");
if (pServerIP == NULL)
{
return -1;
}
strTerm(pServerIP);
strncpy(ip, pServerIP, MAX_IP_LENGTH);
//解析服务端PORT
pServerPort = strtok(NULL, "#");
if (pServerPort == NULL)
{
return -1;
}
strTerm(pServerPort);
*port = atoi(pServerPort);
//解析发送给服务端的命令
pCmd = strtok(NULL, "#");
if (pCmd == NULL)
{
return -1;
}
strTerm(pCmd);
strncpy(cmd, pCmd, MAX_CMDLINE_LENGTH);
return 0;
}
int CopyFileFromServer(void* socketID, char *data)
{
char strPara[MAX_CMDLINE_LENGTH];
char strBuffer[MAX_BUFFER_SIZE];
char *pFrom = NULL;
char *pTo = NULL;
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 (data == NULL)
{
printf("Error: Send Data is NULL!\n");
return -1;
}
memset(strPara, 0, MAX_CMDLINE_LENGTH);
memset(strBuffer, 0, MAX_BUFFER_SIZE);
//解析要拷贝的服务端文件(全路径)
pFrom = strtok(data, ",");
if (pFrom == NULL)
{
printf("Cmdlist para %s error!\n", strPara);
return -1;
}
strTerm(pFrom);
//解析文件在客户端的存放路径(全路径)
pTo = strtok(NULL, ",");
if (pTo == NULL)
{
printf("Cmdlist para %s error!\n", strPara);
return -1;
}
strTerm(pTo);
//构造发送命令给服务端
strcpy(strPara, COPYFILE);
strcat(strPara, "[");
strcat(strPara, pFrom);
strcat(strPara, "]");
iRet = strlen(strPara);
//发送给服务端
#ifdef IN_LINUX
iRet = SendDataToServer_linux(socket, strPara, iRet);
#else
iRet = SendDataToServer_windows(socket, strPara, iRet);
#endif
if ( iRet == -1)
{
printf("Error: Send Data To Server Failed: %s\n", strPara);
return -1;
}
printf("Send Data To Server Success: %s\n", strPara);
//打开准备写的文件
fr = fopen(pTo, "wb");
if(fr == NULL)
{
printf("Error: Open File :%s Failed!\n", pTo);
return -1;
}
//接收来自服务端的文件数据
#ifdef IN_LINUX
iRet = RecvDataFromServer_linux(socket, strBuffer, MAX_BUFFER_SIZE);
#else
iRet = RecvDataFromServer_windows(socket, strBuffer, MAX_BUFFER_SIZE);
#endif
//出错直接关闭socket
if (iRet == -1)
{
#ifdef IN_LINUX
close(socket);
#else
closesocket(socket);
#endif
}
//接收到文件数据,则写入文件
while ( iRet > 0)
{
iTotal += iRet;
if (fwrite(strBuffer, sizeof(char), iRet, fr) != iRet)
{
printf("Error: Write File Failed : %s\n", pTo);
fclose(fr);
return -1;
}
//发送应答消息给服务器
//发送出错
if (SendRspToSever(socketID, RSP_OK) == -1)
{
printf("Error: Send Rsp To Server Failed!\n");
fclose(fr);
return -1;
}
memset(strBuffer, 0, MAX_BUFFER_SIZE);
#ifdef IN_LINUX
iRet = RecvDataFromServer_linux(socket, strBuffer, MAX_BUFFER_SIZE);
#else
iRet = RecvDataFromServer_windows(socket, strBuffer, MAX_BUFFER_SIZE);
#endif
}
printf("Copy File length: %d\n", iTotal);
fclose(fr);
return 0;
}
int SendFileToServer(void* socketID, char *data)
{
char strPara[MAX_CMDLINE_LENGTH];
char strBuffer[MAX_BUFFER_SIZE];
char *pFrom = NULL;
char *pTo = NULL;
FILE *fr = NULL;
int iRet = 0;
int iLen = 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 (data == NULL)
{
printf("Error: Send Data is NULL!\n");
return -1;
}
memset(strPara, 0, MAX_CMDLINE_LENGTH);
memset(strBuffer, 0, MAX_BUFFER_SIZE);
//解析要发送的客户端的文件(全路径)
pFrom = strtok(data, ",");
if (pFrom == NULL)
{
printf("Cmdlist para %s error!\n", strPara);
return -1;
}
strTerm(pFrom);
//解析文件在服务端的存放路径(全路径)
pTo = strtok(NULL, ",");
if (pTo == NULL)
{
printf("Cmdlist para %s error!\n", strPara);
return -1;
}
strTerm(pTo);
//构造发送命令给服务端
strcpy(strPara, SENDFILE);
strcat(strPara, "[");
strcat(strPara, pTo);
strcat(strPara, "]");
iRet = strlen(strPara);
//发送给服务端
#ifdef IN_LINUX
iLen = SendDataToServer_linux(socket, strPara, iRet);
#else
iLen = SendDataToServer_windows(socket, strPara, iRet);
#endif
if ( iLen == -1)
{
printf("Error: Send Data To Server Failed: %s\n", strPara);
return -1;
}
printf("Send Data To Server Success: %s\n", strPara);
//等待应答消息
iRet = WaitRspFromServer(socketID);
if ( iRet == -1)
{
printf("Error: Wait for Rsp From Server Failed!\n");
return -1;
}
else if (iRet == RET_FAILED)
{
printf("Error: Server Error!\n");
return -1;
}
printf("Server Read To Receive File!\n");
//打开文件
fr = fopen(pFrom, "rb");
if(fr == NULL)
{
printf("Error: Open File :%s Failed!\n", pFrom);
return -1;
}
printf("Sending File to Server...\n");
//开始读文件的内容
while ((iLen = fread(strBuffer, sizeof(char), MAX_BUFFER_SIZE-1, fr)) > 0)
{
iTotal += iLen;
#ifdef IN_LINUX
iRet = SendDataToServer_linux(socket, strBuffer, iLen);
#else
iRet = SendDataToServer_windows(socket, strBuffer, iLen);
#endif
if (iRet == -1)
{
printf("Error: Send Data To Server Failed!\n");
fclose(fr);
return -1;
}
memset(strBuffer, 0, MAX_BUFFER_SIZE);
//等待服务端的应答
if (WaitRspFromServer(socketID) == -1)
{
printf("Error: Wait for Rsp From Server Failed!\n");
return -1;
}
}
printf("Send File Length: %ld\n", iTotal);
fclose(fr);
return 0;
}
int FindProcessFromServer(void* socketID, char *data)
{
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
memset(strCmd, 0, MAX_CMDLINE_LENGTH);
strcpy(strCmd, FINDPROC);
strcat(strCmd, "[");
strcat(strCmd, data);
strcat(strCmd, "]");
iRet = strlen(strCmd);
#ifdef IN_LINUX
iRet = SendDataToServer_linux((int)socketID, strCmd, iRet);
#else
iRet = SendDataToServer_windows((SOCKET)socketID, strCmd, iRet);
#endif
if ( iRet == -1)
{
printf("Error: Send Data To Server Failed: %s\n", strCmd);
return -1;
}
printf("Send Data To Server Success: %s\n", strCmd);
//等待应答消息
iRet = WaitRspFromServer(socketID);
if ( iRet == -1)
{
printf("Error: Wait for Rsp From Server Failed!\n");
return -1;
}
if (iRet == RET_OK)
{
printf("The Process %s Run Status: YES\n", data);
}
else if (iRet == RET_FAILED)
{
printf("The Process %s Run Status: NO\n", data);
}
return iRet;
}
int WaitRspFromServer(void* socketID)
{
int iRet;
char strPara[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
memset(strPara, 0, MAX_CMDLINE_LENGTH);
#ifdef IN_LINUX
iRet = RecvDataFromServer_linux(socket, strPara, MAX_CMDLINE_LENGTH);
#else
iRet = RecvDataFromServer_windows(socket, strPara, MAX_CMDLINE_LENGTH);
#endif
if (iRet > 0)
{
iRet = ParseRsp(strPara);
if (iRet == -1)
{
printf("Error: Parse Rsp %s Failed!\n", strPara);
return -1;
}
return iRet;
}
else
{
return -1;
}
}
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 SendRspToSever(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 = SendDataToServer_linux(socket, strRsp, strlen(strRsp));
#else
iRet = SendDataToServer_windows(socket, strRsp, strlen(strRsp));
#endif
if (iRet == -1)
{
printf("Error: Send Data To Client Failed!\n");
return -1;
}
return 0;
}
int CreateSubThread(char *para)
{
#ifdef IN_LINUX
pthread_t child_thread;
#else
HANDLE child_thread;
#endif
if (para == NULL)
{
printf("Error: CreateThread func para is NULL\n");
return -1;
}
#ifdef IN_LINUX
if( pthread_create(&child_thread, NULL, Thread_linux, (void *)para) < 0 )
{
printf("Error: Create Thread Failed : %s\n", strerror(errno));
return -1;
}
//等待线程退出
if(child_thread != 0)
{
pthread_join(child_thread, NULL);
}
#else
child_thread = CreateThread(NULL, 0, Thread_windows, (void *)para, 0, NULL);
if (child_thread == NULL)
{
printf("Error: Create Thread Failed!\n");
return -1;
}
WaitForSingleObject(child_thread, INFINITE);
#endif
return 0;
}
int RunCmdlist()
{
FILE* fr = NULL;
char strCmdLine[MAX_BUFFER_SIZE];
int iCmdNum = 1;
memset(strCmdLine, 0, MAX_BUFFER_SIZE);
// 读取cmdlist串行执行所有命令
fr = fopen(CMDLIST, "r");
if(fr == NULL)
{
printf("open the file of %s failed!\n", CMDLIST);
return -1;
}
while (fgets(strCmdLine, MAX_BUFFER_SIZE, fr) != NULL)
{
printf("=============== begin %d ===============\n", iCmdNum);
//创建线程,由线程来发送操作
CreateSubThread(strCmdLine);
printf("=============== end %d ===============\n", iCmdNum);
iCmdNum++;
}
fclose(fr);
return 0;
}
int RunCmd(int argc, char **argv)
{
int iRet = 0;
int iTimeout = MAX_CONNECT_TIMEOUT;
char szCmd[MAX_CMDLINE_LENGTH];
printf("=============== begin ===============\n");
memset(szCmd, 0, MAX_CMDLINE_LENGTH);
strncpy(szCmd, argv[1], MAX_CMDLINE_LENGTH);
if (argc >= 3)
{
iTimeout = atoi(argv[2]);
}
iRet = TalkToServer(szCmd, iTimeout);
if (iRet == -1)
{
printf("Error: Talk To Server Failed!\n");
}
printf("=============== end ===============\n");
return 0;
}
int main(int argc, char **argv)
{
//执行命令行
if (argc >= 2)
{
RunCmd(argc, argv);
}
//执行cmdlist中的命令
else
{
RunCmdlist();
}
return 0;
}