Solved

TServerSocket

Posted on 2000-05-07
2
266 Views
Last Modified: 2010-04-02
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
Comment
Question by:rdno
2 Comments
 
LVL 14

Accepted Solution

by:
AlexVirochovsky earned 100 total points
ID: 2789310
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
 

Author Comment

by:rdno
ID: 2790378
yeah it was just enough to help me remove my crashes!!! Tnx
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now