Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Vfp app acting as a server to a vfp client app
Message
From
08/08/2003 12:06:14
 
 
To
08/08/2003 10:56:03
Gerry Schmitz
GHS Automation Inc.
Calgary, Alberta, Canada
General information
Forum:
Visual FoxPro
Category:
COM/DCOM and OLE Automation
Miscellaneous
Thread ID:
00817662
Message ID:
00818289
Views:
17
Gerry,

This looks sweet. I think I need to look at it over a cup of coffee. I will get back to you after the weekend.

Thanx ever so much,
Neil

>>I have a lot of reading to do. I never worked with winsock. While I am reading up on it, can you plase give a little example?
>>
>>Let's say the server uses the pick a color dialog to select a color. How does the client app know that the server picked a color and how does the server pass the result to the client?
>
>I've included a Client and a Server program below. You can run them from 2 sessions on the same machine. Insure that the .RemoteHost (name) property in the Client program matches the Server name (ie. computer name).
>
>The recommended sequence of events is:
>
>- Start the Client and Server
>- Connect the Client to the Server
>- Send data beween the Client and Server, and visa-versa
>- Disconnect the Client
>- Disconnect the Server
>
>After that you can fool around and see what happens if you use a different sequence. In general, the Winsock control operates asyncronously; it's not "slow", but its status doesn't always change instantly (eg. from "closed", to "resolving host", to "connecting", to "connected", etc.). Just so you're aware. It works under WinME and W2000 with VFP 7.0 and Winsock SP5. Have fun !
>
>
>*///////////////////////////////////////////////////////////////////////
>*  SOCKSERV.PRG: Winsock Server Program.
>*///////////////////////////////////////////////////////////////////////
>   #DEFINE sckClosed             0
>   #DEFINE sckOpen               1
>   #DEFINE sckListening          2
>   #DEFINE sckConnectionPending  3
>   #DEFINE sckResolvingHost      4
>   #DEFINE sckHostResolved       5
>   #DEFINE sckConnecting         6
>   #DEFINE sckConnected          7
>   #DEFINE sckClosing            8
>   #DEFINE sckError              9
>
>   CLEAR
>
>   _VFP.AutoYield = .T.
>   _SCREEN.Caption = "Server"
>
>   DECLARE Sleep IN KERNEL32.DLL ;
>      INTEGER dwMilliseconds
>
>   DECLARE INTEGER FlashWindow IN USER32.DLL ;
>      INTEGER hWnd, ;
>      INTEGER bInvert
>
>   m.LO_Server = CREATEOBJECT( "Server_Side" )
>   m.LO_Server.Show()
>
>   READ EVENTS
>
>*///////////////////////////////////////////////////////////////////////
>DEFINE CLASS Server_Side AS Form
>*///////////////////////////////////////////////////////////////////////
>   Caption = "Winsock Server Program"
>   AutoCenter = .T.
>
>   ADD OBJECT Listener_01 AS Server_Socket
>
>   ADD OBJECT Socket_01 AS Server_Socket
>
>   ADD OBJECT cmd_Disconnect AS CommandButton WITH ;
>      Caption = "Disconnect", ;
>      Top = 20
>
>   ADD OBJECT cmd_Send AS CommandButton WITH ;
>      Caption = "Send Data", ;
>      Top = 40
>
>   ADD OBJECT cmd_State AS CommandButton WITH ;
>      Caption = "Current State", ;
>      Top = 70
>
>*-----------------------------------------------------------------------
>PROCEDURE Init
>*-----------------------------------------------------------------------
>   WITH THISFORM.Listener_01
>      .LocalPort = 1007
>      .Listen()
>
>      ACTIVATE SCREEN
>      CLEAR
>
>      ?  "Host Name: ", .LocalHostName
>      ?  "IP: ", .LocalIP
>      ?  "Port: ", .LocalPort
>   ENDWITH
>
>*-----------------------------------------------------------------------
>PROCEDURE QueryUnload
>*-----------------------------------------------------------------------
>   IF THISFORM.Socket_01.State <> sckClosed
>      THISFORM.Socket_01.Close()
>   ENDIF
>
>   CLEAR EVENTS
>
>*-----------------------------------------------------------------------
>PROCEDURE cmd_Disconnect.Click
>*-----------------------------------------------------------------------
>   IF THISFORM.Socket_01.State <> sckClosed
>      THISFORM.Socket_01.Close()
>   ENDIF
>
>*-----------------------------------------------------------------------
>PROCEDURE cmd_Send.Click
>*-----------------------------------------------------------------------
>   IF THISFORM.Socket_01.State = sckConnected
>      THISFORM.Socket_01.SendData( "Data from Server" )
>   ELSE
>      = MESSAGEBOX( "No active connection", "Server" )
>   ENDIF
>
>*-----------------------------------------------------------------------
>PROCEDURE cmd_State.Click
>*-----------------------------------------------------------------------
>   = MESSAGEBOX( "Current State of connection: " + ;
>      IIF( TYPE( "THISFORM.Socket_01" ) <> "O", "U", ;
>           STR( THISFORM.Socket_01.State ) ), "Client" )
>
>*-----------------------------------------------------------------------
>ENDDEFINE                              && Server_Side
>*-----------------------------------------------------------------------
>
>*///////////////////////////////////////////////////////////////////////
>DEFINE CLASS Server_Socket AS OleControl
>*///////////////////////////////////////////////////////////////////////
>   OleClass = "MSWinsock.Winsock.1"
>
>*-----------------------------------------------------------------------
>PROCEDURE ConnectionRequest         && Event.
>*-----------------------------------------------------------------------
>   LPARAMETER PN_RequestID
>
>   IF THISFORM.Socket_01.State <> sckClosed
>      THISFORM.Socket_01.Close()
>   ENDIF
>
>   THISFORM.Socket_01.Accept( m.PN_RequestID )
>
>   THIS.Flash_VFP()
>
>   = MESSAGEBOX( "Connection accepted: " + STR( m.PN_RequestID ), "Server" )
>
>*-----------------------------------------------------------------------
>PROCEDURE Close                  && Event and Method.
>*-----------------------------------------------------------------------
>   = DODEFAULT()
>
>*-----------------------------------------------------------------------
>PROCEDURE DataArrival            && Event.
>*-----------------------------------------------------------------------
>   LPARAMETERS PN_BytesTotal
>
>   LOCAL LC_Data
>
>   m.LC_Data = SPACE( m.PN_BytesTotal ) + CHR( 0 )
>
>   THIS.GetData( @m.LC_Data, 8 )          && 8 = String Type
>
>   THIS.Flash_VFP()
>
>   = MESSAGEBOX( m.LC_Data, "Server: Data Arrived !" )
>
>*-----------------------------------------------------------------------
>PROCEDURE Error                  && Event.
>*-----------------------------------------------------------------------
>   LPARAMETERS PN_Number, PC_Description, PN_Scode, PN_Source, ;
>               PC_HelpFile, PN_HelpContext, PL_CancelDisplay
>
>   THIS.Flash_VFP()
>
>   = MESSAGEBOX( ;
>      "Error #: " + LTRIM( STR( m.PN_Number ) ) + CHR( 10 ) + ;
>      "Desc: " + m.PC_Description, "Server" )
>
>*-----------------------------------------------------------------------
>PROCEDURE Flash_VFP
>*-----------------------------------------------------------------------
>   FOR i = 1 TO 10                           && Flash on, then off.
>      = FlashWindow( _VFP.HWND, 1 )
>      = Sleep( 50 )
>   NEXT
>
>*-----------------------------------------------------------------------
>ENDDEFINE                              && Server_Socket
>*-----------------------------------------------------------------------
>*///////////////////////////////////////////////////////////////////////
>*  EOF.
>*///////////////////////////////////////////////////////////////////////
>
>*///////////////////////////////////////////////////////////////////////
>*  SOCKCLNT.PRG: Winsock Client Program.
>*///////////////////////////////////////////////////////////////////////
>*
>*  .RemoteHost is specified as the machine where the server is running.
>*
>*  Change the RemoteHost property to connect to any machine with a
>*  Point-to-Point connection.
>*
>*///////////////////////////////////////////////////////////////////////
>   #DEFINE sckClosed             0
>   #DEFINE sckOpen               1
>   #DEFINE sckListening          2
>   #DEFINE sckConnectionPending  3
>   #DEFINE sckResolvingHost      4
>   #DEFINE sckHostResolved       5
>   #DEFINE sckConnecting         6
>   #DEFINE sckConnected          7
>   #DEFINE sckClosing            8
>   #DEFINE sckError              9
>
>   CLEAR
>
>   _VFP.AutoYield = .T.
>   _SCREEN.Caption = "Client"
>
>   DECLARE Sleep IN KERNEL32.DLL ;
>      INTEGER dwMilliseconds
>
>   DECLARE INTEGER FlashWindow IN USER32.DLL ;
>      INTEGER hWnd, ;
>      INTEGER bInvert
>
>   m.LO_Client = CREATEOBJECT( "Client_Side" )
>   m.LO_Client.Show()
>
>   READ EVENTS
>
>*///////////////////////////////////////////////////////////////////////
>DEFINE CLASS Client_Side AS Form
>*///////////////////////////////////////////////////////////////////////
>   Caption = "Winsock Client Program"
>   AutoCenter = .T.
>
>   ADD OBJECT Socket_01 AS Client_Socket
>
>   ADD OBJECT cmd_Connect AS CommandButton WITH ;
>      Caption = "Connect"
>
>   ADD OBJECT cmd_Disconnect AS CommandButton WITH ;
>      Caption = "Disconnect", ;
>      Top = 20
>
>   ADD OBJECT cmd_Send AS CommandButton WITH ;
>      Caption = "Send Data", ;
>      Top = 40
>
>   ADD OBJECT cmd_State AS CommandButton WITH ;
>      Caption = "Current State", ;
>      Top = 70
>
>*-----------------------------------------------------------------------
>PROCEDURE QueryUnload
>*-----------------------------------------------------------------------
>   CLEAR EVENTS
>
>*-----------------------------------------------------------------------
>PROCEDURE cmd_Connect.Click
>*-----------------------------------------------------------------------
>   IF THISFORM.Socket_01.State <> sckClosed
>      = MESSAGEBOX( "Connection is not closed", "Client" )
>      RETURN
>   ENDIF
>
>   WITH THISFORM.Socket_01
>      .RemoteHost = "gerry1a"
>      .RemotePort = 1007
>      .Connect()                 && Asyncronous !
>   ENDWITH
>
>*-----------------------------------------------------------------------
>PROCEDURE cmd_Disconnect.Click
>*-----------------------------------------------------------------------
>   IF THISFORM.Socket_01.State <> sckClosed
>      THISFORM.Socket_01.Close()
>   ENDIF
>
>*-----------------------------------------------------------------------
>PROCEDURE cmd_Send.Click
>*-----------------------------------------------------------------------
>   IF THISFORM.Socket_01.State = sckConnected
>      THISFORM.Socket_01.SendData( "Data from Client" )
>   ELSE
>      = MESSAGEBOX( "No active connection", "Client" )
>   ENDIF
>
>*-----------------------------------------------------------------------
>PROCEDURE cmd_State.Click
>*-----------------------------------------------------------------------
>   = MESSAGEBOX( "Current State of connection: " + ;
>      STR( THISFORM.Socket_01.State ), "Client" )
>*-----------------------------------------------------------------------
>ENDDEFINE                              && Client_Side
>*-----------------------------------------------------------------------
>
>*///////////////////////////////////////////////////////////////////////
>DEFINE CLASS Client_Socket AS OleControl
>*///////////////////////////////////////////////////////////////////////
>   OleClass = "MSWinsock.Winsock.1"
>
>   N_Connects = 0
>
>*-----------------------------------------------------------------------
>PROCEDURE Connect                && Method AND Event
>*-----------------------------------------------------------------------
>   = DODEFAULT()
>
>   THIS.N_Connects = THIS.N_Connects + 1
>
>   IF THIS.N_Connects > 50
>      THIS.Flash_VFP()
>
>      = MESSAGEBOX( "Connection attemps > 50; giving up", "Client" )
>
>      THIS.Close()
>
>      THIS.N_Connects = 0
>   ENDIF
>
>*-----------------------------------------------------------------------
>PROCEDURE Close                  && Method AND Event
>*-----------------------------------------------------------------------
>   = DODEFAULT()
>
>*-----------------------------------------------------------------------
>PROCEDURE DataArrival            && Event.
>*-----------------------------------------------------------------------
>   LPARAMETERS PN_BytesTotal
>
>   LOCAL LC_Data
>
>   m.LC_Data = SPACE( m.PN_BytesTotal ) + CHR( 0 )
>
>   THIS.GetData( @m.LC_Data, 8 )          && 8 = String Type
>
>   THIS.Flash_VFP()
>
>   = MESSAGEBOX( m.LC_Data, "Client: Data Arrived !" )
>
>*-----------------------------------------------------------------------
>PROCEDURE Error                  && Event.
>*-----------------------------------------------------------------------
>   LPARAMETERS PN_Number, PC_Description, PN_Scode, PN_Source, ;
>               PC_HelpFile, PN_HelpContext, PL_CancelDisplay
>
>   THIS.Flash_VFP()
>
>   = MESSAGEBOX( ;
>      "Error #: " + LTRIM( STR( m.PN_Number ) ) + CHR( 10 ) + ;
>      "Desc: " + m.PC_Description, "Client" )
>
>*-----------------------------------------------------------------------
>PROCEDURE Flash_VFP
>*-----------------------------------------------------------------------
>   FOR i = 1 TO 10                           && Flash on, then off.
>      = FlashWindow( _VFP.HWND, 1 )
>      = Sleep( 50 )
>   NEXT
>
>*-----------------------------------------------------------------------
>ENDDEFINE                              && Client_Socket
>*-----------------------------------------------------------------------
>*///////////////////////////////////////////////////////////////////////
>*  EOF.
>*///////////////////////////////////////////////////////////////////////
>
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform