INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Networking

VFP Chat Client by craigsboyd
Posted: 12 Jul 03 (Edited 18 Feb 05)

Slighthaze = NULL

This FAQ is a work in progress, but I wanted to get it up here since it shows how to communicate via UDP between two forms.  It could just as easily have been two processes or computers communicating, but I wanted an example that you could run from a prg file inside VFP for now.  The reason I started this project was because I was looking for a way to communicate between all the instances of an application in a multi-user setting without having to create some sort of message queue and check it with a timer.  That seem pretty inefficient to me.  Things I thought I could do with the ability to send and receive messages in an application was allow system administrators to send out messages to users that a proposed action was about to take place such as "Backing Up the Data in One Minute!".  I thought they could even send a message to the running applications on the remote workstations telling them to shutdown gracefully if a user wasn't present to respond.  I also thought that it would be an easy way to keep track of all the users currently logged into the application or how many copies were currently running (a sort of LAN strobe if you will)  There are other possibilities for this such as sending data back and forth, notification of a change in application state, etc.  So, as you can see, though the program is entitled VFP CHAT CLIENT, it is just a simple example that opens doors to many possibilities.

This code uses the MS Winsock control.  I am assuming that you have it registered on your computer - if not then do so before running the example.  The ports being bound to are 1234 and 1235 - if either of these conflict with something already running on your computer, change them before running.

Possible future changes for this FAQ include:
  • Choosing protocol TCP/UDP
  • Binding to different ports
  • Chat Server to act as relay
  • HTML and/or XML support
  • Compression and Encryption of data sent
  • Sending Files across
  • Blocking
  • Who's Online/Offline List
Special Thanks to Aircon for his code revisions - see Thread184-541348 (I have incorporated some of them here and will incorporate more of his ideas in the future additions to this FAQ).

*CUT-N-PASTE the code below into a prg and run it from within VFP*

PUBLIC oForm, oForm2

oForm = CREATEOBJECT("clsChat",1)
oForm.visible = .t.

oForm2 = CREATEOBJECT("clsChat",2)
oForm2.visible = .t.

READ EVENTS

Store Null to oForm, oForm2
Release oForm, oForm2

DEFINE CLASS clschat AS form

    Top = 0
    Left = 0
    Height = 268
    Width = 376
    DoCreate = .T.
    Caption = "VFP CHAT"
    Name = "clschat"

    ADD OBJECT txtchatname AS textbox WITH ;
        Height = 23, ;
        Left = 216, ;
        Top = 12, ;
        Width = 132, ;
        Name = "txtChatname"

    ADD OBJECT edtchat AS editbox WITH ;
        Height = 134, ;
        Left = 24, ;
        ReadOnly = .T., ;
        Top = 48, ;
        Width = 324, ;
        DisabledBackColor = RGB(255,255,255), ;
        DisabledForeColor = RGB(0,0,0), ;
        Name = "edtChat"
        
    ADD OBJECT txtmessage AS textbox WITH ;
        Height = 23, ;
        Left = 84, ;
        Top = 194, ;
        Width = 264, ;
        Name = "txtMessage"

    ADD OBJECT command1 AS commandbutton WITH ;
        Top = 221, ;
        Left = 298, ;
        Height = 25, ;
        Width = 50, ;
        Caption = "Send", ;
        Name = "Command1"

    ADD OBJECT winsock1 AS WSControl

    ADD OBJECT label2 AS label WITH ;
        AutoSize = .T., ;
        BackStyle = 0, ;
        Caption = "MESSAGE", ;
        Height = 17, ;
        Left = 24, ;
        Top = 197, ;
        Width = 59, ;
        Name = "Label2"

    ADD OBJECT label1 AS label WITH ;
        AutoSize = .T., ;
        BackStyle = 0, ;
        Caption = "CHAT NAME", ;
        Height = 17, ;
        Left = 144, ;
        Top = 15, ;
        Width = 70, ;
        Name = "Label1"

    PROCEDURE Init
        LPARAMETERS plForm
        IF plform = 1
            thisform.winsock1.object.bind(1234)
            thisform.txtChatname.value = "likestochat"
        ELSE
            thisform.winsock1.object.bind(1235)
            thisform.txtChatname.value = "sirchatalot"
            thisform.AutoCenter = .t. &&So they don't overlap
        ENDIF
        thisform.txtMessage.setfocus()
    ENDPROC

    Procedure Destroy
       ThisForm.RemoveObject('WinSock1')
    EndProc

    PROCEDURE Unload
        CLEAR events
    ENDPROC

    PROCEDURE txtmessage.KeyPress
        LPARAMETERS nKeyCode, nShiftAltCtrl
        IF nKeycode = 13 AND nShiftAltCtrl = 0
            IF !EMPTY(thisform.txtMessage.value)
                thisform.command1.Click()
            ENDIF
        ENDIF
    ENDPROC

    PROCEDURE command1.Click
        LOCAL sChatName, sMessageSent
        WITH thisform
            sChatName = ALLTRIM(thisform.txtChatname.value)
            sMessageSent = sChatName +" says:   " + ALLTRIM(thisform.txtMessage.value)
            WITH .winsock1.object
                .RemotePort = 1235
                .RemoteHost = "127.0.0.1"
                .SendData(sMessageSent)
                .RemotePort = 1234
                .RemoteHost = "127.0.0.1"
                .SendData(sMessageSent)
            ENDWITH
        .txtMessage.value = ""
        endwith
    ENDPROC

ENDDEFINE

Define Class WSControl As OleControl
        OleClass='MSWinsock.Winsock.1'
        Top = 228
        Left = 24
        Height = 100
        Width = 100
        Name = "winsock1"

    PROCEDURE Init
        this.object.Protocol = 1
    ENDPROC
    
    PROCEDURE DataArrival
        LPARAMETERS bytestotal
        sMessage = SPACE(bytestotal)
        thisform.winsock1.object.GetData(@sMessage)
        thisform.edtChat.Value = thisform.edtChat.Value + sMessage + CHR(13)
    ENDPROC

Enddefine

Back to Microsoft: Visual FoxPro FAQ Index
Back to Microsoft: Visual FoxPro Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close