• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 290
  • Last Modified:

TServerSocket

Can someone tell how to create a serversocket manually without using the IDE, so by entering

#include <mainsckt.hpp>

TServerSocket *TCPServer;

When I use the TCPServer it wil crash immediatly! in the help was written that it's possible to use new to create a custom made ServerSocket, but when I use new it will give me an error...

Any help?

Tnx,
Raymond
0
rdno
Asked:
rdno
1 Solution
 
AlexVirochovskyCommented:
Next is small working (but not so good)
example, that you can use as starting point to you apps:

SmtpServer.h
//---------------------------------------------------------------------------
#ifndef SmtpServerH
#define SmtpServerH
//---------------------------------------------------------------------------
#include <scktcomp.hpp>
//---------------------------------------------------------------------------
class SmtpServerClientThread : public TServerClientThread
{
private:
enum SMTPStates
{stateInit,
stateMail,
stateRcpt,
stateExit}
SMTPState;
AnsiString Sender;
AnsiString Recipient;
AnsiString Data;
int nNumberOFRecipients;

bool __fastcall GetCommand(TWinSocketStream *pStream, AnsiString
&Buffer);
void __fastcall ReplyCommand(TWinSocketStream *pStream,
AnsiString s);
void __fastcall ResetSession(void);
void __fastcall HandleCmd_HELO(TWinSocketStream *pStream);
void __fastcall HandleCmd_QUIT(TWinSocketStream *pStream);
void __fastcall HandleCmd_MAIL(TWinSocketStream *pStream,const
AnsiString &Command);
void __fastcall HandleCmd_RCPT(TWinSocketStream *pStream,const
AnsiString &Command);
bool __fastcall HandleCmd_DATA(TWinSocketStream *pStream);
bool __fastcall SaveEmail(AnsiString &Data);
protected:
void __fastcall ClientExecute();
public:
__fastcall SmtpServerClientThread(bool CreateSuspended,
TServerClientWinSocket* ASocket);
};
//---------------------------------------------------------------------------
#endif



//SmtpServer.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "SmtpServer.h"
#include <stdio.h>
#pragma package(smart_init)
//---------------------------------------------------------------------------
__fastcall SmtpServerClientThread::SmtpServerClientThread(bool
CreateSuspended,

TServerClientWinSocket* ASocket)
: TServerClientThread(CreateSuspended, ASocket)
{
}
//---------------------------------------------------------------------------
void __fastcall SmtpServerClientThread::ClientExecute()
{ TWinSocketStream *pStream;
AnsiString Command;
AnsiString Buffer;

ResetSession();
while (!Terminated && ClientSocket->Connected)
{
try
{
pStream = new TWinSocketStream(ClientSocket, 60000);
try
{
ReplyCommand(pStream,"220 Simple Mail Transfer Service Ready");

while(GetCommand(pStream,Buffer))
{
Command = Buffer;
Buffer = Command.UpperCase();
Buffer = Buffer.SubString(1,5);
if (Buffer == "HELO ")
{
HandleCmd_HELO(pStream);
}
else if(Buffer == "QUIT\r")
{
HandleCmd_QUIT(pStream);
break;
}
else if(Buffer == "NOOP\r")
{
ReplyCommand(pStream,"250 Requested mail action ok");
}
else if(Buffer == "RSET\r")
{
ResetSession();
ReplyCommand(pStream,"250 Requested mail action ok");
}
else if(Buffer == "MAIL ")
{
HandleCmd_MAIL(pStream,Command);
}
else if(Buffer == "RCPT ")
{
HandleCmd_RCPT(pStream,Command);
}
else if(Buffer == "DATA\r")
{
HandleCmd_DATA(pStream);
}
else
{
ReplyCommand(pStream,"500 Syntax error, command
unrecognized");
}


}
if(ClientSocket->Connected)ClientSocket->Close();

}
__finally
{
if(ClientSocket->Connected)ClientSocket->Close();
if(pStream){delete pStream;pStream = NULL;}
}
}
catch (Exception &E)
{
// HandleException();
if(ClientSocket->Connected)ClientSocket->Close();
if(pStream){delete pStream;pStream = NULL;}
}
}


}
//---------------------------------------------------------------------------
bool __fastcall SmtpServerClientThread::GetCommand(TWinSocketStream
*pStream, AnsiString &Buffer)
{ char szBuffer[1024];
Buffer = "";
int t;
bool bResult = true;

if (pStream->WaitForData(60000))
{ while(1)
{
if ((t=pStream->Read(szBuffer, sizeof(szBuffer))) == 0)
{bResult=false;break;}
Buffer += AnsiString(szBuffer,t);
if((t = Buffer.Pos("\r\n")))
{Buffer.SetLength(t+1);break;}
else continue;
}

}
else
bResult = false;
return bResult;
}
//---------------------------------------------------------------------------
void __fastcall SmtpServerClientThread::ReplyCommand(TWinSocketStream
*pStream, AnsiString s)
{
s = s+"\r\n";
pStream->Write(s.c_str(),s.Length());
}
//---------------------------------------------------------------------------
void __fastcall SmtpServerClientThread::ResetSession()
{
SMTPState = stateInit ;
Sender = "";
Recipient = "";
Data = "";
nNumberOFRecipients = 0;
}
//---------------------------------------------------------------------------
void __fastcall SmtpServerClientThread::HandleCmd_HELO(TWinSocketStream
*pStream)
{
if(SMTPState != stateInit)
{ResetSession();
ReplyCommand(pStream, "503 Bad sequence of commands");
}
else
{
ReplyCommand(pStream, "250 hello pleased to meet you");
}
}
//---------------------------------------------------------------------------
void __fastcall SmtpServerClientThread::HandleCmd_QUIT(TWinSocketStream
*pStream)
{
ResetSession();
SMTPState = stateExit;
ReplyCommand(pStream, "221 Service closing transmission channel");
}
//---------------------------------------------------------------------------
void __fastcall SmtpServerClientThread::HandleCmd_MAIL(TWinSocketStream
*pStream,const AnsiString &Command)
{
AnsiString s = Command.SubString(6,Command.Length()-5);
s = s.UpperCase();
s = s.Trim();
s = s.SubString(1,5);
if(s != "FROM:")
{
ReplyCommand(pStream,"500 Syntax error, command unrecognized");
return;
}
if(SMTPState != stateInit)
{ResetSession();
ReplyCommand(pStream, "503 Bad sequence of commands");
}
else
{ //check from: here
Sender = Command;
SMTPState = stateMail;
ReplyCommand(pStream,"250 Sender accepted");
}
}
//---------------------------------------------------------------------------
void __fastcall SmtpServerClientThread::HandleCmd_RCPT(TWinSocketStream
*pStream,const AnsiString &Command)
{
AnsiString s = Command.SubString(6,Command.Length()-5);
s = s.UpperCase();
s = s.Trim();
s = s.SubString(1,3);
if(s != "TO:")
{
ReplyCommand(pStream,"500 Syntax error, command unrecognized");
return;
}
if ((SMTPState != stateMail) && (SMTPState != stateRcpt))
{ResetSession();
ReplyCommand(pStream, "503 Bad sequence of commands");}
else
{ //check to: here
Recipient += Command;
nNumberOFRecipients++ ;
SMTPState = stateRcpt;
ReplyCommand(pStream, "250 recipient OK");
}
}
//---------------------------------------------------------------------------
// if socket error return fasle , else return true
bool __fastcall SmtpServerClientThread::HandleCmd_DATA(TWinSocketStream
*pStream)
{ int t ;
AnsiString sBuffer;
char szBuffer[10240];

if(SMTPState != stateRcpt)
{ResetSession();
ReplyCommand(pStream, "503 Bad sequence of commands");
return true;}
ReplyCommand(pStream, "354 Start mail input; end with <CRLF>.<CRLF>");
Data="";
if(pStream->WaitForData(60000))
{ while(1)
{
if ((t=pStream->Read(szBuffer, sizeof(szBuffer))) != 0)
{
Data += AnsiString(szBuffer,t);
sBuffer = Data.SubString(Data.Length()-4,5);
if(sBuffer == "\r\n.\r\n")
{
if(SaveEmail(Data))ReplyCommand(pStream, "250 ok");
else ReplyCommand(pStream, "452 Requested action not
taken: local error in processing");
ResetSession();
return true;
}
else continue; //sBuffer == "\r\n.\r\n"
}
else return false ;
} //end while(1)
}
else return false;
}
//---------------------------------------------------------------------------
//return true if success
bool __fastcall SmtpServerClientThread::SaveEmail(AnsiString &Data)
{
SYSTEMTIME tt;
char ss[100];
int i ;

for( i = 1 ;; i++)
{
Sleep(2);
GetLocalTime(&tt);
sprintf(ss,"%x%x%x%x%x%x%x%x%x%s",

tt.wYear,tt.wMonth,tt.wDay,tt.wHour,tt.wMinute,tt.wSecond,tt.wMilliseconds,ThreadID,i,
".eml");

if(FileExists(ss))
{
Sleep(0);
if(i<500)continue;
else return false;
}
else break;
}

FILE *f = fopen(ss,"wb");

if(f != NULL)
{
if(1 > 
fwrite(Sender.c_str(),Sender.Length(),1,f)){fclose(f);return false;}
AnsiString sBuffer = AnsiString(nNumberOFRecipients)+"\r\n";
if(1 > 
fwrite(sBuffer.c_str(),sBuffer.Length(),1,f)){fclose(f);return false;}
if(1 > 
fwrite(Recipient.c_str(),Recipient.Length(),1,f)){fclose(f);return
false;}
if(1 > fwrite(Data.c_str(),Data.Length()-3,1,f)){fclose(f);return
false;}
fclose(f);
return true;
}
else
{
return false;
}
}
//---------------------------------------------------------------------------
I hope, it helps. Alex
0
 
rdnoAuthor Commented:
yeah it was just enough to help me remove my crashes!!! Tnx
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now