Help me! 我的Socket怎么了?看看这段代码...
我的一段程序在WinNT4.0上运行正常,但在Win2k上不对。
CMySock继承了CSocket
void CMySock::OnReceive(int nErrorCode)
{
CBkfjApp *pApp=(CBkfjApp*)AfxGetApp();
unsigned int port;
GetPeerName(strIp,port);
pApp->strIp=strIp;
pApp->m_pMainWnd->Invalidate();
if(nErrorCode==0)
{
ReadInStream();
}
CSocket::OnReceive(nErrorCode);
Close();
}
void CMySock::OnAccept(int nErrorCode)
{
CSocket::OnAccept(nErrorCode);
CBkfjApp *pApp=(CBkfjApp*)AfxGetApp();
// Accept(pApp->Server);
// pApp->Server.AsyncSelect(FD_READ);
CMySock *pServer=new CMySock();
Accept(*pServer);
pServer->AsyncSelect(FD_READ);
// AfxMessageBox("as;djas;d");
}
char CMySock::ReadCmd(void **value,short *length)
{
char cmd;
U_SHORT len;
if(Receive(&cmd,1)>0)
{
switch(cmd)
{
case 'u':
case 'p':
case 'd':
case 'c':
case 'q':
if(Receive(len.ch+1,1)<=0) return 0;
if(Receive(len.ch,1)<=0) return 0;
*length=len.i;
*value=malloc(*length*2+1);
int kkkk;
Sleep(3); //在Win2k下加上此句则可正常运行,但在NT4下不要此句也可正常运行.不正常的现象是下一句的Receive的返回值为1,而*length=2
kkkk=Receive(*value,*length*2);
if(kkkk!=*length*2)
// kkkk=Receive(*(value+1),*length*2-1);
return 0;
*((char *)(*value)+*length*2)=0;
break;
case 'E':
break;
default:
return 0;
break;
}
}
else return cmd;
return cmd;
}
void CMySock::ReadInStream()
{
char ch;
short length;
void *value;
CBkfjApp *pApp=(CBkfjApp*)AfxGetApp();
LPWSTR lpszW;
char ch1;
int i;
char *str;
int len;
ch=ReadCmd(&value,&length);
str=NULL;
while(ch!=0&&ch!='E')
{
for(i=0;i<length;i++)
{
ch1=((char *)value)[i*2];
((char*)value)[i*2]=((char*)value)[i*2+1];
((char*)value)[i*2+1]=ch1;
}
lpszW=(LPWSTR)value;
if(str!=NULL) delete str;
str=new char[length*2+1];
len=WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK|WC_SEPCHARS, lpszW, length, str,length*2+1,NULL,NULL);
str[len]=0;
switch(ch)
{
case 'u': //用户名
user=str;
break;
case 'p': //口令
password=str;
break;
case 'd':
database=str;
break;
case 'c':
flag=0;
sql=str;
break;
case 'q':
flag=1;
sql=str;
break;
case 'E':
case 0:
free(value);
// AfxMessageBox("as;das");
// str=user+password+database;
// AfxMessageBox("kkkkkkk");
// AfxMessageBox(str);
return;
default:
errmsg="ErrorAAA";
break;
}
free(value);
ch=ReadCmd(&value,&length);
}
// if(value) free(value);
// string=user+password+database+sql;
// string="user="+user;
//+";password="+password+";database="+database"+"sql="+sql;
delete str;
}
这种现象为什么?怎么能不要Sleep(3)一句?