; This is the server 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
; Check if @IPAddress1 works, if not, have server user enter IP manually.
Dim $IPA1DoesntWork = MsgBox(4+32, "IP Tester", "Do you see 127.0.0.1 as your IP?" & @CRLF & "Your IP: " & @IPAddress1)
If($IPA1DoesntWork == 7) Then
$ServIP = @IPAddress1
Else
$ServIP = InputBox("Server IP?", "What is your IP?")
If(@error == 1 OR $ServIP == "") Then
MsgBox(0+64, "Program Termination", "Invalid IP. The program has been terminated.")
Exit
EndIf
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)
; Create a Listening Socket and check if properly created
$LSock = TCPListen($ServIP, 33891)
If ($LSock == -1) Then
MsgBox(0+16, "Error", "There was an error creating the listening socket." & @CRLF & "The program will now terminate.")
Exit
EndIf
; Looks for a client connection
Do
$CSock = TCPAccept($LSock)
Until $CSock <> -1
; 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