Page 2 of 2

Re: Tsocket Bug

Posted: Mon May 09, 2011 7:15 pm
by Daniel Garcia-Gil
Daniel Garcia-Gil wrote:With a nonblocking socket, the connection attempt cannot be completed immediately. In this case, connect will return SOCKET_ERROR, and WSAGetLastError will return WSAEWOULDBLOCK. ( you can call after connect, will see )


Yes is a normal error For use nonblocking socket... Windows doc api explain it

Re: Tsocket Bug

Posted: Tue May 10, 2011 6:45 am
by Horizon
Daniel,

ıs it possible to set a timeout to give alert client that the connection has not been established when there is no message "ok"

Thanks,

Re: Tsocket Bug

Posted: Tue May 10, 2011 12:56 pm
by Daniel Garcia-Gil
Hello

you can use a timer to call Connect() again if you got not a "ok", with max Retry options

1- initiate Retry variable
2- open timer
3- connect
4- if you get "ok" deactivate the timer, else try connect again until max retry is take (deactivate timer )

is only a idea

Re: Tsocket Bug

Posted: Tue May 10, 2011 3:12 pm
by Horizon
Thanks,

I trying now.

Re: Tsocket Bug

Posted: Tue May 10, 2011 7:02 pm
by Horizon
Hi Daniel,

This is my last SockCli.prg.

Code: Select all | Expand

// Socket server connection sample

#include "FiveWin.ch"

#define nWaitTime       10000

static oWnd, oSocket, oTimer, nTry

function Main()

   local oBar

   DEFINE WINDOW oWnd TITLE "Client socket"

   DEFINE BUTTONBAR oBar OF oWnd _3D

   DEFINE BUTTON OF oBar ACTION Client() TOOLTIP "Connect"
   
   DEFINE BUTTON OF oBar ;
      ACTION oSocket:SendData( "MSG This is a test" ) ;
      TOOLTIP "Send data"
     
   DEFINE BUTTON OF oBar ;
      ACTION SendFile() TOOLTIP "Send file"

   ACTIVATE WINDOW oWnd

return nil

function Client()
   
   if oSocket != NIL
      oSocket:End()
   endif
   oSocket = TSocket():New( 5000 )
   
   if oTimer != NIL
      oTimer:End()
   endif

     nTry := 1

   oSocket:bRead    = { | oSocket | HandleRecived( oSocket, oWnd ) }

   // Never use a MsgInfo() here because it hangs Windows!!!
   oSocket:bConnect = { || oWnd:SetText( "Socked Opened!" ) }

   oSocket:bClose   = { || MsgInfo( "Server has closed!" ) }

     Timer_Connect()   
   
     Define Timer oTimer Interval nWaitTime Action Timer_Connect() OF oWnd
   Activate Timer oTimer  

   
return nil

function Timer_Connect()
    IF nTry<4
        oWnd:SetText("Connection Try : "+ALLTRIM(STR(nTry)))
        oSocket:Connect( "127.0.0.1" ) 
        nTry++
    ELSE
        oTimer:End()
        oSocket:End()
        oWnd:SetText( "Socket Closed" )
        MsgInfo("Connection can NOT be ESTABLISHED")
  ENDIF
return

function SendFile()

   local cFileName := cGetFile( "*.*", "Select a file to send by Internet" )

   if ! Empty( cFileName ) .and. File( cFileName )
      oSocket:SendData( "SENDFILE " + cFileName( cFileName ) )
      oSocket:SendFile( cFileName )
      MsgInfo( "File sent" )
   endif

return nil

function HandleRecived( oSocket, oWnd )

   local cData := oSocket:GetData()
   
   
   if cData = "ok"
      oWnd:SetText( "Connected!!" )
      oTimer:End()
   else
      MsgInfo( cData )
   endif


return nil
 

Re: Tsocket Bug

Posted: Tue May 10, 2011 7:28 pm
by Daniel Garcia-Gil
Hello

what's mean? all is working fine now?

Re: Tsocket Bug

Posted: Tue May 10, 2011 8:02 pm
by Horizon
Yes. I also think to use cargo if its really connected.

Thank you very much.



Code: Select all | Expand

function HandleRecived( oSocket, oWnd )

   local cData := oSocket:GetData()
   
   
   if cData = "ok"
      oWnd:SetText( "Connected!!" )
      oTimer:End()
      oSocket:cargo:=.t.   // it is connected.
   else
      MsgInfo( cData )
   endif


return nil