Hi,

Well I thought that I can attach files.

In the mean time I found a work around but I do not like it.

Here is the code including the work around:

In general the problem is in the Execute function. For some reason after
assigning port and address 
to the socket, calling connect fails ?. The exception is:
   Connect:  No IP address specified

Assigning port and address again and reconnecting works.

Actually I am using the same port and only the IP address changes. When
I set port as part of the initializing 
code and do not assign it just before calling connect I sometimes also
get an exception on the port not specified.

Any ideas are welcomed.

BR,
Noam.


//----------------------------------------------------------------------
-----

#include <vcl.h>
#pragma hdrstop

#include "TAuthHandling.h"
#include "Main.h"

#pragma package(smart_init)

#pragma link "OverbyteIcsWndControl"
#pragma link "OverbyteIcsWSocket"
#pragma link "OverbyteIcsWSocketS"

//----------------------------------------------------------------------
-----

//   Important: Methods and properties of objects in VCL can only be
//   used in a method called using Synchronize, for example:
//
//      Synchronize(&UpdateCaption);
//
//   where UpdateCaption could look like:
//
//      void __fastcall TAuthHandling::UpdateCaption()
//      {
//        Form1->Caption = "Updated in a thread";
//      }
//----------------------------------------------------------------------
-----

__fastcall TAuthHandling::TAuthHandling(bool CreateSuspended)
  : TThread(CreateSuspended)
{
  AuthItem = NULL;

  Priority = tpHigher;

  SendingAuthStack = new TStack();

  AuthCriticalSection = new TCriticalSection();

  AuthSendSocket = new TWSocket(NULL);
  AuthSendSocket->FlushTimeout = 60;
  AuthSendSocket->KeepAliveInterval = 1000;
  AuthSendSocket->KeepAliveTime = 10000;
  AuthSendSocket->LocalAddr = "0.0.0.0";
  AuthSendSocket->Proto = "tcp";
  AuthSendSocket->LingerOnOff = wsLingerOff;
  AuthSendSocket->ListenBacklog = 5;
  AuthSendSocket->ReuseAddr = false;
  AuthSendSocket->SendFlags = wsSendNormal;
  AuthSendSocket->SocketFamily = sfIPv4;
  AuthSendSocket->SocksLevel = 5;
  AuthSendSocket->Port = MainForm->AuthSendToPort;
  AuthSendSocket->MultiThreaded = true;

  AuthSendSocket->OnSessionConnected = AuthSendSocketSessionConnected;
  AuthSendSocket->OnBgException = AuthSendSocketBgException;
  AuthSendSocket->OnDataSent = AuthSendSocketDataSent;
}

//----------------------------------------------------------------------
-----

__fastcall TAuthHandling::~TAuthHandling()
{
}

//----------------------------------------------------------------------
-----

void __fastcall TAuthHandling::Execute()
{
  int Retry, i = 0;


while(Terminated == false)
{
//{ wsInvalidState, wsOpened, wsBound, wsConnecting, wsSocksConnected,
wsConnected, wsAccepting, wsListening, wsClosed };
    if(SendingAuthStack->Count() > 0)
    {
      if(AuthSendSocket->State == wsClosed)
      {
        AuthCriticalSection->Acquire();
        {
          AuthItem = (TAuthItem*)SendingAuthStack->Pop();
        }
        AuthCriticalSection->Release();

        Retry = AUTH_RETRY_COUNT;

        while(1)
        {
          try
          {
            if((AuthItem != NULL) && (AuthItem->State == AuthNewItem))
            {
              if(AuthItem->L_ToIP.Length() > 0)
              {
                AuthSendSocket->Addr = AuthItem->L_ToIP;
                AuthSendSocket->Port = MainForm->AuthSendToPort;
                AuthItem->State = AuthWaitingForConnect;

                AuthSendSocket->Connect();

                AuthSendSocket->ProcessMessages();

              i++;
              }
            }
          }
          catch(Exception &exception)
          {
          //            Application->ShowException(&exception);

            // if we got an exception for IP or port settings not
defined
            // try again.
            //
            // This looks like an ICS bug ???
            if((Retry--) > 0)
            {
              AuthSendSocket->Addr = AuthItem->L_ToIP;
              AuthSendSocket->Port = MainForm->AuthSendToPort;
              AuthItem->State = AuthNewItem;

              if(AuthSendSocket->State != wsClosed)
              {
                AuthSendSocket->Close();
              }

              continue;
            }
          }

          break;
        }
      }
    }
    else
    {
      // no need to run forever if nothing is in list, we may change it
later on
      Sleep(20);
    }

   Application->ProcessMessages();

    if(SendingAuthStack->Count() == 0)
    {
      Sleep(10);
    }
}

  //--------------------------------------------------------------

  // clear the list if there are unsent items in it
  while(SendingAuthStack->Count() > 0)
  {
    AuthItem = (TAuthItem*)SendingAuthStack->Pop();

    delete AuthItem;
    AuthItem = NULL;
  }

  delete SendingAuthStack;
  SendingAuthStack = false;

  AuthSendSocket->Close();
  delete AuthSendSocket;
  AuthSendSocket = NULL;

  delete AuthCriticalSection;
  AuthCriticalSection = NULL;
}

//----------------------------------------------------------------------
-----

void __fastcall TAuthHandling::AuthSendSocketSessionConnected(TObject
*Sender, WORD ErrCode)
{
  TWSocket *Socket = (TWSocket*)Sender;

  if((AuthItem != NULL) && (AuthItem->L_ToIP == Socket->Addr))
  {
    if(ErrCode == 0)
    {
      // data is copied to the internal string. It should be ok up to
64K ?
      AuthSendSocket->SendStr(AuthItem->L_Data);

     DebugWinDlg->AddStringToMemoAndToFile("Sent this: " +
AuthItem->L_Data + ", To IP: " + AuthItem->L_ToIP);

      AuthSendSocket->ProcessMessages();
    }
    else
    {
      // connection timed out
      if(ErrCode == 10060)
      {
        DebugWinDlg->AddStringToMemoAndToFile("");
            DebugWinDlg->AddStringToMemoAndToFile("** From
TAuthHandling::AuthSendSocketSessionConnected, Could not send Auth data
to device at IP: " + AuthItem->L_ToIP);
        DebugWinDlg->AddStringToMemoAndToFile("");
      }
    }


    delete AuthItem;
    AuthItem = NULL;

    // close the socket in order to let other devices get their data.
    AuthSendSocket->CloseDelayed(); //Close();
//    AuthSendSocket->Close();
  }
}

//----------------------------------------------------------------------
-----

void __fastcall TAuthHandling::AuthSendSocketBgException(TObject
*Sender, Exception *E, bool &CanClose)
{
  TWSocket *Socket = (TWSocket*)Sender;

  if((AuthItem != NULL) && (AuthItem->L_ToIP == Socket->Addr))
  {
    DebugWinDlg->AddStringToMemoAndToFile("");
    DebugWinDlg->AddStringToMemoAndToFile("** From
TAuthHandling::AuthSendSocketBgException function, Exception: " +
E->Message);
    DebugWinDlg->AddStringToMemoAndToFile("");
  }
}

//----------------------------------------------------------------------
-----

void __fastcall TAuthHandling::AddAuthFileToList(String ToIp, String
Data)
{
  TAuthItem *NewAuthItem = new TAuthItem(ToIp, Data);
//  TAuthItem *NewAuthItem = new TAuthItem("192.168.0.42", Data);

  AuthCriticalSection->Acquire();
  {
    SendingAuthStack->Push(NewAuthItem);
  }
  AuthCriticalSection->Release();
}

//----------------------------------------------------------------------
-----

void __fastcall TAuthHandling::AuthSendSocketDataSent(TObject *Sender,
WORD ErrCode)
{
}

//----------------------------------------------------------------------
-----




-----Original Message-----
From: TWSocket [mailto:twsocket-boun...@lists.elists.org] On Behalf Of
Michael Gasser
Sent: Tuesday, January 28, 2014 4:25 PM
To: twsocket@lists.elists.org
Subject: Re: [twsocket] TWSocket Digest, Vol 554, Issue 1

Hi Noam

>>  you wrote: I have attached the code I am using.

Where is your code? I didn't receive any attachment.

Best regards
Michael



----- Original Message ----- 
From: <twsocket-requ...@lists.elists.org>
To: <twsocket@lists.elists.org>
Sent: Tuesday, January 28, 2014 1:00 PM
Subject: TWSocket Digest, Vol 554, Issue 1


> Send TWSocket mailing list submissions to
> twsocket@lists.elists.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
> http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
> or, via email, send a message with subject or body 'help' to
> twsocket-requ...@lists.elists.org
>
> You can reach the person managing the list at
> twsocket-ow...@lists.elists.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of TWSocket digest..."
>
>
> Today's Topics:
>
>   1. TWSocket strange problem (Noam weissman)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Tue, 28 Jan 2014 10:21:16 +0200
> From: "Noam weissman" <n...@silrd.com>
> To: <twsocket@lists.elists.org>
> Subject: [twsocket] TWSocket strange problem
> Message-ID:
> <e44d48019612554d82017d1e60a164d5027da...@sil-exchange.silora.co.il>
> Content-Type: text/plain; charset="us-ascii"
>
> Hi,
>
>
>
>
>
> First of all let me say a great thank you for a great TCP/IP
components
> set.
>
>
>
> I have a strange problem.
>
>
>
> I have a thread that has one TWSocket and a TStack.
>
> The socket is tcp and used as a client.
>
>
>
> I add messages to this Stack from another thread.
>
>
>
> In the thread that uses the TWSocket I read  one item at a time from
the
> stack.
>
>
>
> The item has a string and a destination IP.
>
>
>
> Every time I read one item from the stack I check that my socket is
> closed, connect to the IP in
>
> the message and wait for the SocketSessionConnected event.
>
>
>
> When the SocketSessionConnected is triggered I send the data and call
> CloseDelayed function
>
>
>
> Everything works just fine except that once in a few restarts to my
> application I get an exception
>
> that IP address is not defined. My IP is defined and has no problems
> that I can see ?
>
>
>
> What am I doing wrong or is there a bug in TWSocket ?
>
>
>
> I have attached the code I am using.
>
>
>
> Any assistance will be appreciated.
>
>
>
> BR,
>
> Noam.
>
>
>
>
>
************************************************************************
************
> This footnote confirms that this email message has been scanned by
> PineApp Mail-SeCure for the presence of malicious code, vandals &
computer 
> viruses.
>
************************************************************************
************
>
>
>
>
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> 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
>
> ------------------------------
>
> End of TWSocket Digest, Vol 554, Issue 1
> **************************************** 

-- 
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

 
 
************************************************************************
************
This footnote confirms that this email message has been scanned by
PineApp Mail-SeCure for the presence of malicious code, vandals &
computer viruses.
************************************************************************
************




 
 
************************************************************************************
This footnote confirms that this email message has been scanned by
PineApp Mail-SeCure for the presence of malicious code, vandals & computer 
viruses.
************************************************************************************



-- 
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