FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Tsocket Bug
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 PM
Re: Tsocket Bug
Posted: Mon May 09, 2011 07:15 PM
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
Posts: 1387
Joined: Fri May 23, 2008 01:33 PM
Re: Tsocket Bug
Posted: Tue May 10, 2011 06:45 AM

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,

Regards,



Hakan ONEMLI



Harbour & MSVC 2022 & FWH 23.06
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 PM
Re: Tsocket Bug
Posted: Tue May 10, 2011 12:56 PM

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

Posts: 1387
Joined: Fri May 23, 2008 01:33 PM
Re: Tsocket Bug
Posted: Tue May 10, 2011 03:12 PM

Thanks,

I trying now.

Regards,



Hakan ONEMLI



Harbour & MSVC 2022 & FWH 23.06
Posts: 1387
Joined: Fri May 23, 2008 01:33 PM
Re: Tsocket Bug
Posted: Tue May 10, 2011 07:02 PM
Hi Daniel,

This is my last SockCli.prg.

Code (fw): Select all Collapse
// 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
Regards,



Hakan ONEMLI



Harbour & MSVC 2022 & FWH 23.06
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 PM
Re: Tsocket Bug
Posted: Tue May 10, 2011 07:28 PM

Hello

what's mean? all is working fine now?

Posts: 1387
Joined: Fri May 23, 2008 01:33 PM
Re: Tsocket Bug
Posted: Tue May 10, 2011 08:02 PM
Yes. I also think to use cargo if its really connected.

Thank you very much.



Code (fw): Select all Collapse
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
Regards,



Hakan ONEMLI



Harbour & MSVC 2022 & FWH 23.06

Continue the discussion