Hi All,

I have done a cbuilder translation of Mr. Mestdagh's SpocketSpy 
program. But, I have a problem. When I run the program and try to 
connect with a server, I get this immediate error:

"An unknown response was received in response to a request for 
information from the news server 'SocketSpyC++ Local TCP Proxy'"

Under the details button of the error dialog, I get:

SocketSpyC++ Local TCP Proxy

Configuration:
   Account: SocketSpyC++ Local TCP Proxy
   Server: 127.0.0.1
   Protocol: NNTP
   Port: 119
   Secure(SSL): 0
   Code: 800ccca0

I don't have any idea what the problem is. First, while trying to 
debug, I put a popup messagebox at the end of the 
WSocketServerClientConnect procedure to take a look at a variable. 
What I discovered was that messagebox being open allowed the program 
to actually connect and go further before error. After that, I 
replaced the messagebox with an Application->ProcessMessages() loop 
as a test. I was amazed that after that, the program ran completely 
and reliably--everytime. It works just great, but I haven't tried 
more than 1 connection because of the ProcessMessages loop. Though 
the loop has been helpful, I doubt it is the appropriate fix for this.

I'm wondering if this problem is somehow related to changes made to 
cbuilder in the 2007 edition I'm using. I have already discovered and 
fixed 2 other problems related to the 2007 upgrade.

I'm using Vista64. My source code is included below. I hope you'll 
forgive me if I have included too much code. My program is 257 lines. 
Please have a look.

Regards,

Richard Christman
https://www.quicksilvermail.net

socketwins.h:
//--------------------------------------------------------------------
-------
#ifndef socketwinsH
#define socketwinsH
//--------------------------------------------------------------------
-------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "OverbyteIcsWndControl.hpp"
#include "OverbyteIcsWSocket.hpp"
#include "OverbyteIcsWSocketS.hpp"
#include <ComCtrls.hpp>
//--------------------------------------------------------------------
-------

class TSocketWin : public TForm
{
__published:    // IDE-managed Components
        TRichEdit *TCPLog;
        TStatusBar *StatusBar;
        TEdit *RemoteAddr;
        TLabel *Label3;
        TEdit *RemotePort;
        TLabel *Label2;
        TEdit *LocalPort;
        TLabel *Label1;
        TButton *ListenBtn;
        TWSocketServer *WSocketServer;
        void __fastcall WSocketServerClientConnect(TObject *Sender,
                  TWSocketClient *Client, WORD Error);
        void __fastcall WSocketServerClientDisconnect(TObject *Sender,
                  TWSocketClient *Client, WORD Error);
        void __fastcall WSocketServerSessionClosed(TObject *Sender, WORD 
Error);
        void __fastcall ListenBtnClick(TObject *Sender);
private:        // User declarations
        void __fastcall BgException(TObject *Sender, Exception *E, bool 
&CanClose);
        void __fastcall ClientDataAvailable(TObject *Sender, WORD Error);
        void __fastcall RemoteSessionConnected(TObject *Sender, WORD Error);
        void __fastcall RemoteDataAvailable(TObject *Sender, WORD Error);
        void __fastcall RemoteSessionClosed(TObject *Sender, WORD Error);
        void __fastcall RemoteDnsLookupDone(TObject *Sender, WORD Error);
public:         // User declarations
        bool cancel;
        TStrings *log;
        __fastcall TSocketWin(TComponent* Owner);
        __fastcall virtual ~TSocketWin();
        void __fastcall Log(AnsiString Msg);
};

class TClient : public TWSocketClient
{
public:
        AnsiString rcvd;
        TWSocket *remote;
        __fastcall TClient(TComponent *Owner);
        __fastcall virtual ~TClient();
};

//--------------------------------------------------------------------
-------
extern PACKAGE TSocketWin *SocketWin;
//--------------------------------------------------------------------
-------
#endif

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

#include "socketwins.h"
//--------------------------------------------------------------------
-------
#pragma package(smart_init)
#pragma link "OverbyteIcsWndControl"
#pragma link "OverbyteIcsWSocket"
#pragma link "OverbyteIcsWSocketS"
#pragma resource "*.dfm"
TSocketWin *SocketWin;
const AnsiString lineend = "\r\n";
const bool linemode = true;
//--------------------------------------------------------------------
-------

__fastcall TSocketWin::TSocketWin(TComponent* Owner)
        : TForm(Owner)
{
   log = TCPLog->Lines;
}
//--------------------------------------------------------------------
-------

__fastcall TSocketWin::~TSocketWin()
{
}
//--------------------------------------------------------------------
-------

void __fastcall TSocketWin::ListenBtnClick(TObject *Sender)
{
   if (ListenBtn->Tag == 0)
   {
          NNTPLog->Clear();
          WSocketServer->Banner = "Welcome to OverByte ICS TcpSrv";
          WSocketServer->BannerTooBusy = "Sorry, too many connections!";
          WSocketServer->ClientClass = __classid(TClient);
          WSocketServer->Port = LocalPort->Text;
          WSocketServer->Addr = "127.0.0.1";
          WSocketServer->Listen();

          LocalPort->Enabled  = false;
          RemotePort->Enabled = false;
          RemoteAddr->Enabled = false;
          ListenBtn->Caption  = "Cancel";
          ListenBtn->Tag = 1;
          cancel = false;
   }
   else
   {
          cancel = true;
          for (int i = 0; i < WSocketServer->ClientCount; i++)
                 WSocketServer->Client[i]->Close();
          WSocketServer->Close();
   }
}
//--------------------------------------------------------------------
-------

void __fastcall TSocketWin::WSocketServerClientConnect(TObject 
*Sender,
          TWSocketClient *Client, WORD Error)
{
   if (Error)
   {
          Log("Error at WSocketServerClientConnect: " + IntToStr(Error));
          return;
   }

   Log("Remote connection opened");
   TClient *client = dynamic_cast<TClient*>(Client);
   assert(client != NULL);

   client->rcvd = "";
   client->OnBgException = BgException;
   client->OnDataAvailable = ClientDataAvailable;
   client->LineMode = linemode;
   client->LineEnd = lineend;
   client->remote = new TWSocket(client);
   client->remote->Port = RemotePort->Text;
   client->remote->LineMode = linemode;
   client->remote->LineEnd = lineend;
   client->remote->OnBgException = BgException;
   client->remote->OnSessionConnected = RemoteSessionConnected;
   client->remote->OnDataAvailable = RemoteDataAvailable;
   client->remote->OnSessionClosed = RemoteSessionClosed;
   client->remote->OnDnsLookupDone = RemoteDnsLookupDone;
   client->remote->DnsLookup(RemoteAddr->Text);

   StatusBar->Panels->Items[1]->Text = WSocketServer->ClientCount;
   StatusBar->Panels->Items[3]->Text = client->GetPeerAddr();

   while (!cancel && !Application->Terminated)
          Application->ProcessMessages();
}
//--------------------------------------------------------------------
-------

void __fastcall TSocketWin::WSocketServerClientDisconnect(TObject 
*Sender,
          TWSocketClient *Client, WORD Error)
{
   if (Error)
   {
          Log("Error at WSocketServerClientDisconnect: " + IntToStr(Error));
          return;
   }

   int clientcount = WSocketServer->ClientCount;
   StatusBar->Panels->Items[1]->Text = IntToStr(clientcount - 1);
   if (clientcount > 1)
          StatusBar->Panels->Items[3]->Text =
                 WSocketServer->Client[clientcount - 2]->GetPeerAddr();
   else
          StatusBar->Panels->Items[3]->Text = "";
}
//--------------------------------------------------------------------
-------

void __fastcall TSocketWin::WSocketServerSessionClosed(TObject 
*Sender,
          WORD Error)
{
   LocalPort->Enabled = true;
   RemotePort->Enabled = true;
   RemoteAddr->Enabled = true;
   ListenBtn->Caption = "Listen";
   ListenBtn->Tag = 0;
   cancel = true;
}
//--------------------------------------------------------------------
----------

void __fastcall TSocketWin::BgException(TObject *Sender, Exception 
*e, bool &CanClose)
{
   Log("Exception occured: the error reported was: " + e->Message);
   CanClose = true;
}
//--------------------------------------------------------------------
-------

void __fastcall TSocketWin::ClientDataAvailable(TObject *Sender, WORD 
Error)
{
   if (Error)
   {
          Log("Error at ClientDataAvailable: " + IntToStr(Error));
          return;
   }

   TClient *client = dynamic_cast<TClient*>(Sender);
   assert(client != NULL);

   client->rcvd += client->ReceiveStr();
   if (client->remote->State == wsConnected && client->rcvd != "")
   {
          client->remote->SendStr(client->rcvd);
          Log("> " + client->rcvd);
          client->rcvd = "";
   }
}
//--------------------------------------------------------------------
-------

void __fastcall TSocketWin::RemoteDataAvailable(TObject *Sender, WORD 
Error)
{
   if (Error)
   {
          Log("Error at RemoteDataAvailable: " + IntToStr(Error));
          return;
   }

   TWSocket *remote = dynamic_cast<TWSocket*>(Sender);
   TClient *client = dynamic_cast<TClient*>(remote->Owner);
   assert(client != NULL);

   AnsiString rcvd = remote->ReceiveStr();
   if (rcvd != "")
   {
          if (client->State == wsConnected)
          {
                 client->SendStr(rcvd);
                 Log("< " + rcvd);
          }
          else
          {
                 Log("Error: Local has closed");
                 remote->CloseDelayed();
          }
   }
}
//--------------------------------------------------------------------
-------

void __fastcall TSocketWin::RemoteSessionConnected(TObject *Sender, 
WORD Error)
{
   if (Error)
   {
          Log("Error at RemoteSessionConnected: " + IntToStr(Error));
          return;
   }

   Log("Remote connection opened");
   TWSocket *remote = dynamic_cast<TWSocket*>(Sender);
   TClient *client = dynamic_cast<TClient*>(remote->Owner);
   assert(client != NULL);

   if (client->rcvd != "")
   {
          remote->SendStr(client->rcvd);
          client->rcvd = "";
   }
}
//--------------------------------------------------------------------
-------

void __fastcall TSocketWin::RemoteSessionClosed(TObject *Sender, WORD 
Error)
{
   if (Error)
   {
          Log("Error at RemoteSessionClosed: " + IntToStr(Error));
          return;
   }

   TClient *client = dynamic_cast<TClient*>(((TWSocket*)Sender)-
>Owner);
   assert(client != NULL);

   client->Shutdown(1);
}
//--------------------------------------------------------------------
-------

void __fastcall TSocketWin::RemoteDnsLookupDone(TObject *Sender, WORD 
Error)
{
   if (Error)
   {
          Log("Error at RemoteDnsLookupDone: " + IntToStr(Error));
          return;
   }

   TWSocket *remote = dynamic_cast<TWSocket*>(Sender);
   assert(remote != NULL);

   remote->Addr = remote->DnsResult;
   remote->Connect();
}
//--------------------------------------------------------------------
-------

void __fastcall TSocketWin::Log(AnsiString Msg)
{
   while (Msg.Length() && Msg[Msg.Length()] == '\n' || 
Msg[Msg.Length()] == '\r')
          Msg.Delete(Msg.Length(), 1);
   log->Add(Msg);
}

//--------------------------------------------------------------------
-------
// TClient
//--------------------------------------------------------------------
-------

__fastcall TClient::TClient(TComponent *Owner)
   : TWSocketClient(Owner)
{
}
//--------------------------------------------------------------------
-------

__fastcall TClient::~TClient()
{
   remote->CloseDelayed();
   delete remote;
}
//--------------------------------------------------------------------
-------
--
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

Reply via email to