服务器和客户端发送的数据用中括号括起来[]
while(1)
{
iRecv = recv(sockConn,recvstr,sizeof(recvstr),0);
if(iRecv <= 0)
{
LoadStringA(NULL,IDS_SERVER_DISCONN,strMsg,64);
printf("%s %d",strMsg,GetLastError());
break;
}
strcat_s(gstrRecvbuf,recvstr);
for(i=0;i<(int)strlen(gstrRecvbuf);i++)
{
if(gstrRecvbuf[i] == '[') sPos = i;
if(gstrRecvbuf[i] == ']' && sPos >= 0) ePos = i;
if(sPos>=0 && ePos>sPos)
{
char* compstr = new char[ePos - sPos];
for(j=sPos+1,k=0;j<ePos;j++,k++)
{
compstr[k] = gstrRecvbuf[j];
}
compstr[k]='\0';
LoadStringA(NULL,IDS_RECV_FROM_SERVER,strMsg,64);
printf("%s%s\n",strMsg,compstr); // 找到可以用的串
int iStrPos = -1;
if((iStrPos = SubstrFind(compstr,"size:")) > 0) // 如果接受的是size:
{
Size = atoi(compstr+iStrPos);
}
if((iStrPos = SubstrFind(compstr,"path:")) > 0) // 如果接受的是path:
{
strcpy_s(UsbPath,compstr+iStrPos);
}
if((iStrPos = SubstrFind(compstr,"start:")) >= 0) // 如果接收的是start
{
Type = *(compstr+iStrPos);
TCHAR w_UsbPath[256]=_T("");
DWORD dwNum = MultiByteToWideChar(CP_ACP,0,UsbPath,-1,NULL,0);
MultiByteToWideChar(CP_ACP,0,UsbPath,-1,w_UsbPath,dwNum);
if(send(sockConn,"[start]",8,0)<=0)
{
LoadStringA(NULL,IDS_SEND_FAILED_START,strMsg,64);
printf("%s\n",strMsg);
}
StartPipeServer(Size,Type,w_UsbPath);
bStart = true;
}
if(strcmp(compstr,"stop") == 0) // 如果接受的是stop
{
WriteToPipe("stop\n",hStdInRead);
}
delete[] compstr;
lpos = ePos;
sPos = ePos = -1;
}
}
if(lpos > 0)
{
// 调整串
int rlen = strlen(gstrRecvbuf+lpos+1);
for(i=0;i<rlen;i++)
{
gstrRecvbuf[i] = gstrRecvbuf[lpos+i+1];
}
gstrRecvbuf[i]='\0';
lpos = 0;
}
}