; This is the client program for the IM program.
; Author: Zach Boatrite - boaty3
; Date: 28 September 2009
#include <TCPFunctions.au3>
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <Misc.au3>
; The Server IP and CSock's IP
Dim $ServIP, $AccIP
; The Listening Socket and Connection Socket
Dim $LSock, $CSock
; The GUI and the controls
Dim $GUI, $Edit, $Label
; The received and sent text
Dim $RecvText, $SendText
$ServIP = InputBox("Server IP?", "What is the server IP?")
If(@error == 1 OR $ServIP == "") Then
MsgBox(0+64, "Program Termination", "Invalid IP. The program has been terminated.")
Exit
EndIf
If (TCPStartup() == 0) Then ; Starts the TCP services and checks that they're properly started.
MsgBox(0+16, "Error", "There was an error starting the TCP services." & @CRLF & "The program will now terminate.")
Exit
EndIf
; Create the GUI
$GUI = GUICreate("Server (IP: " & $ServIP & ")", 300, 200)
$EditRecv = GUICtrlCreateEdit("", 0, 0, 300, 100, $ES_READONLY, $ES_MULTILINE)
$EditSend = GUICtrlCreateEdit("", 0, 100, 300, 100, $ES_AUTOVSCROLL)
GUISetState(@SW_SHOW)
; Attempt to connect to the Listening Socket and check if properly connected.
$CSock = TCPConnect($ServIP, 33891)
If ($CSock == -1) Then
MsgBox(0+16, "Error", "There was an error connecting to the listening socket." & @CRLF & "The program will now terminate.")
Exit
EndIf
; Retrieve the IP of the connected client
$AccIP = SocketToIP($CSock)
; Begin the loop to keep the GUI & program running
While GUIGetMsg() <> $GUI_EVENT_CLOSE
; Try to receive 2048 bytes (256 charactesr), check if failed.
$RecvText = TCPRecv($CSock, 2048)
; If @error Then Exit ; This line may need to be removed
; If text was received, write it to the RecvEdit control
If $RecvText <> "" Then
GUICtrlSetData($EditRecv, GUICtrlRead($EditRecv) & @CRLF & $AccIP & " > " & $RecvText)
EndIf
If(_IsPressed("0D") And (ControlGetFocus($GUI) = 'Edit2') And WinActivate($GUI, '')) Then
SendText()
EndIf
WEnd
; Now that we're done, close the connection
TCPCloseSocket($CSock)
If (TCPShutdown() == 0) Then ; Stops the TCP services and checks that they've been properly stopped.
MsgBox(0+16, "Error", "There was an error stoping the TCP services." & @CRLF & "The program will now terminate.")
Exit
EndIf
; -------------------------------------------------
; Function to Send data
; -------------------------------------------------
Func SendText()
$SendText = GUICtrlRead($EditSend)
TCPSend($CSock, $SendText)
GUICtrlSetData($EditSend, "")
EndFunc