Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VFP Chat Client

Networking

VFP Chat Client

by  craigsboyd  Posted    (Edited  )
Slighthaze = [color blue]NULL[/color]
[img http://www.sweetpotatosoftware.com/ttimages/vfpchat.gif]
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:
[ul][li]Choosing protocol TCP/UDP[/li][li]Binding to different ports[/li][li]Chat Server to act as relay[/li][li]HTML and/or XML support[/li][li]Compression and Encryption of data sent[/li][li]Sending Files across[/li][li]Blocking[/li][li]Who's Online/Offline List[/li][/ul]
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
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top