Solved

TServerSocket

Posted on 2000-05-07
2
278 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

705 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