SSL client / server

Download source code

SSL Client:

class TMySSLClient : public TclTcpClient {
public:
  virtual __fastcall TMySSLClient(Classes::TComponent* AOwner):TclTcpClient(AOwner){};
  virtual int __fastcall GetDefaultPort(void);
  void __fastcall SendData(TStream *data);
  void __fastcall ReceiveData(TStream *data);
};
 
...
 
int __fastcall TMySSLClient::GetDefaultPort(void) {
  return 9002;
}
 
void __fastcall TMySSLClient::SendData(TStream *data) {
  Connection->WriteData(data);
}
 
void __fastcall TMySSLClient::ReceiveData(TStream *data) {
  TStream *stream = new TMemoryStream();
  __try {
    //read size of incoming data
    while(stream->Size < 8) {
      Connection->ReadData(stream);
    }
    stream->Position = 0;
 
    DWORD len = 0;
    stream->Read(&len, 8);
 
    //copy the first block of data
    if(stream->Size > 8) {
      data->CopyFrom(stream, stream->Size - 8);
    }
 
    //receive remaining data from server
    while(data->Size < len) {
      Connection->ReadData(data);
    }
  }
  __finally {
      delete stream;
  }
}

SSL Server:

class TMySslCommandConnection : public TclUserConnection {};
 
class TMySslServer : public TclTcpServer {
protected:
  virtual TclUserConnection* __fastcall CreateDefaultConnection();
  virtual void __fastcall DoReadConnection(TclUserConnection* AConnection, 
  Classes::TStream* AData);
public:
  __fastcall TMySslServer(TComponent* Owner) : TclTcpServer(Owner) {};
};
 
...
 
TclUserConnection* __fastcall TMySslServer::CreateDefaultConnection() {
    return new TMySslCommandConnection();
}
 
void __fastcall TMySslServer::DoReadConnection(TclUserConnection* AConnection, 
  Classes::TStream* AData) {
  if(AData->Size == 0)
    return;
 
    TclTcpServer::DoReadConnection(AConnection, AData);
 
  //send the size of data to the client
  TStream *stream = new TMemoryStream();
  __try {
    DWORD len = AData->Size;
    stream->Write(&len, 8);
    stream->Position = 0;
    AConnection->WriteData(stream);
  }
  __finally {
      delete stream;
  }
 
  //send data to the client
  AData->Position = 0;
  AConnection->WriteData(AData);
}

Add Feedback