Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

TServerSocket

Posted on 2000-05-07
2
Medium Priority
?
289 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 300 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

885 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