?
Hallo,
ich möchte mir als Übungsprojekt einen Pop3Client programmieren, der mails grabt und dann ins Pickupverzeichnis des Exchanges kopiert, wenn ich alle Schritte einzel durchführe klappt es auch wunderbar (z.B.):
Button1
> Connect
Button2
> User
> Pass
Button3
> List
Button4
> Retr
Button5
> Dele
Button6
> Close
Wenn ich das ganze aber versuche irgendwie dynamisch zu gestalten bekomme ich Probleme beim OnReceive, da der Socket ja nicht blockt und ich irgendwann nichtmehr weiss ob die Pakete vom RETR oder DELE oder LIST kommen.
Ich habe hier leichte Designschwierigkeiten, Bsp.
OnReceive:
while (recv=receive(data,size))
Rufe solange ab, bis nixmehr da ist.
Nixmehr da, dann nächstes Kommando
So jetzt ist es allerdings so, wenn ich gössere Mails abrufe, durchläuft er z.b. 3 mal die Schleife dann ist nix mehr da also denke ich das er alles abgerufen hat und ich schicke z.B. dele, ne Sekunde später kommen allerdings noch Restdaten vom RETR die ich jetzt natürlich ins DELE reinparse, sprich ich weiss nicht wann welche aktion abgeschlossen ist.
void CMySocket::OnAccept(int nErrorCode)
{
int nres=0;
}
void CMySocket::OnConnect(int nErrorCode)
{
int nres=0;
}
void CMySocket::OnSend(int nErrorCode)
{
int nres=0;
}
void CMySocket::OnClose(int nErrorCode)
{
int nres=0;
}
void CMySocket::OnReceive(int nErrorCode)
{
int nrec=0;
int nsize=1025;
char *pBuf=new char [1026];
char * pFileName=new char [255];
FILE *fHandle;
CTime tnow;
int nslen=0;
this->nStateRec=1;
nslen=sprintf(pFileName,"%s","C:\\");
tnow.GetCurrentTime();
nslen+=sprintf(pFileName+nslen,"%i",tnow.Format("%D%H%M%S"));
nslen+=sprintf(pFileName+nslen,".txt");
if (this->nCommand==4)
{
fHandle=fopen(pFileName,"a+b");
}
// Receive Data
while ((nrec=this->Receive(pBuf,nsize)) >0)
{
m_nread+=nrec;
pBuf[nrec]=0;
if (this->nCommand==4)
{
fputs(pBuf,fHandle);
}
}
if (this->nCommand==4)
{
fclose(fHandle);
this->SendDeleteCommand();
}
if (this->nCommand==3)
{
this->nListRemain=this->ParseList(pBuf);
}
if (this->nCommand==5)
{
this->SendListCommand();
}
TRACE ("%i",this->nCommand);
TRACE("\r\n");
this->nStateRec=0;
this->nResponse=0;
// TRACE("%s",pBuf);
TRACE("\r\n");
delete [] pBuf;
delete [] pFileName;
}
int CMySocket::ParseList(char *pdata)
{
int nresult=0;
char *psep= new char[3];
char *currptr;
char *endptr;
char *pBuf= new char[255];
int nres;
int nsend=0;
currptr=pdata;
strcpy(psep,"\r\n");
for (endptr=strstr(currptr,psep);endptr!=NULL;endptr=strstr(currptr,psep))
{
*endptr=0;
currptr=strstr(endptr,psep);
currptr=endptr +strlen(psep);
if (currptr[0]!='.')
{
nresult=1;
break;
}
}
if (nresult==1)
{
char *pspace= new char[2];
char *msgno= new char[64];
strcpy(pspace," ");
if ((endptr=strstr(currptr,pspace))!=NULL)
{
nsend=0;
int ncpysize=(strlen(currptr) -strlen(endptr));
strncpy(msgno,currptr,ncpysize);
msgno[ncpysize]=0;
this->nMessageNumber=atoi(msgno);
nsend=sprintf(pBuf,"%s%i%s","RETR ",this->nMessageNumber,"\r\n");
pBuf[nsend]=0;
this->Send(pBuf,nsend);
this->nCommand=4;
}
}
delete [] psep;
delete [] pBuf;
return nresult;
}
void CMySocket::SendListCommand()
{
char *pBuf= new char[255];
int nsend=0;
nsend=0;
nsend=sprintf(pBuf,"%s","LIST \r\n");
pBuf[nsend]=0;
this->Send(pBuf,nsend);
this->nCommand=3;
delete [] pBuf;
}
void CMySocket::SendDeleteCommand()
{
char *pBuf= new char[255];
int nsend=0;
nsend=0;
nsend=sprintf(pBuf,"%s%i%s","DELE ",this->nMessageNumber,"\r\n");
pBuf[nsend]=0;
this->Send(pBuf,nsend);
this->nCommand=5;
delete [] pBuf;
}