Jump to content
Sign in to follow this  
JavaScript_Freek

TCP Chat "user has joined"

Recommended Posts

JavaScript_Freek

How would you go along by adding that. I have tried and nothing.

Client.au3

#include <GUIConstants.au3>
Opt("GUIOnEventMode", 1)

Global Const $PORT = 31758
Global $list, $sock
Global $userlist[17][2]  ; [ index ][ username, ctrl id ]

TCPStartUp()

$gui = GUICreate("Login", 260, 100)
GUICtrlCreateLabel("Server IP", 10, 10, 60, 20)
GUICtrlCreateLabel("Username", 10, 40, 60, 20)
$ipctrl = GUICtrlCreateEdit(@IPADDRESS1, 80, 10, 170, 20, $ES_READONLY)
$userctrl = GUICtrlCreateInput("", 80, 40, 170, 20)
$login = GUICtrlCreateButton("Login", 170, 70, 80, 20, $BS_DEFPUSHBUTTON)

GUICtrlSetOnEvent($login, "_Login")
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

GUISetState()
$loop = True

While $loop
   Sleep(20)
WEnd

$gui = GUICreate("Urban Chat - User ("& $user & ")", 460, 230)
$list = GUICtrlCreateListView("         Users       ", 350, 10, 100, 210)
$edit = GUICtrlCreateEdit("Welcome to Urban Chat. "& $user & " Follow the rules!" & @CRLF & @CRLF , 10, 70, 330, 150, $ES_READONLY + $WS_VSCROLL + $ES_AUTOVSCROLL)
$input = GUICtrlCreateInput("", 10, 10, 330, 20)
$send = GUICtrlCreateButton("Send", 10, 40, 80, 20, $BS_DEFPUSHBUTTON)
$pm = GUICtrlCreateButton("PM", 100, 40, 80, 20)

GUICtrlSetOnEvent($send, "_Send")
GUICtrlSetOnEvent($pm, "_PM")
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

GUICtrlSetState($input, $GUI_FOCUS)
GUISetState()

_ReduceMemory()

While 1
   Sleep(0)
   $recv = TCPRecv($sock, 2048)
   If @error Then mError("Connection lost.", 1)
   If not $recv Then ContinueLoop
   ; parse the command(s)
   $recv = StringSplit($recv, Chr(1))
   For $i = 1 to $recv[0]
      $temp = StringSplit($recv[$i], Chr(2))
      Switch $temp[1]
         Case "rejected"
            mError("Connection rejected.", 1)
         Case "kick"
            mError("Connection kicked.", 1)
         Case "accepted"
            If $USER = "admin" Then
               $kick = GUICtrlCreateButton("Kick", 190, 40, 80, 20)
               GUICtrlSetOnEvent($kick, "_Kick")
            EndIf
         Case "adduser"
            _AddUser($temp[2])
         Case "deluser"
            _DelUser($temp[2])
         Case "exit"
            mError("Server has closed.", 1)
         Case Else
            If $recv[$i] Then GUICtrlSetData($edit, $recv[$i] & @CRLF, 1)
      EndSwitch
   Next
   _ReduceMemory()
WEnd


Func _Login()
   $loop = False
   Global Const $IP = GUICtrlRead($ipctrl)
   Global Const $USER = GUICtrlRead($userctrl)
   GUIDelete($gui)
   If $USER = "admin" Then
      Local $pass = InputBox("Enter Password:", "Please enter the server password below.", "", "")
      If @error Then Exit
      $sock = TCPConnect($IP, $PORT)
      If $sock = -1 Then mError("Internet needs to be turned on.", 1)
   Else
      $sock = TCPConnect($IP, $PORT)
      If $sock = -1 Then mError("Internet needs to be turned on.", 1)
      TCPSend($sock, $USER)
   EndIf
EndFunc

Func _Send()
   If not GUICtrlRead($input) Then Return 0
   TCPSend($sock, StringReplace(StringReplace(GUICtrlRead($input), Chr(1), ""), Chr(2), ""))
   GUICtrlSetData($input, "")
   Return 1
EndFunc

Func _PM()
   If not GUICtrlRead($input) Then Return 0
   Local $user = GUICtrlRead($list), $data = StringReplace(StringReplace(GUICtrlRead($input), Chr(1), ""), Chr(2), "")
   If not $user Then Return 0
   $user = GUICtrlRead($user)
   TCPSend($sock, Chr(1) & "PM" & Chr(2) & $user & Chr(2) & $data)
   GUICtrlSetData($input, "")
   GUICtrlSetData($edit, "PM sent to: " & $user & "" & @CRLF & "" & $data & "" & @CRLF, 1)
   Return 1
EndFunc

Func _Kick()
   Local $user = GUICtrlRead($list)
   If not $user Then Return 0
   $user = GUICtrlRead($user)
   TCPSend($sock, Chr(1) & "kick" & Chr(2) & $user)
   GUICtrlSetData($edit, "<** Kick: " & $user & " **>" & @CRLF, 1)
   Return 1
EndFunc

Func _AddUser( $user )
   If $user = "admin" and not $userlist[16][0] Then
      $userlist[16][0] = $user
      $userlist[16][1] = GUICtrlCreateListViewItem($user, $list)
      Return 1
   EndIf
   For $i = 0 to 15
      If not $userlist[$i][0] Then
         $userlist[$i][0] = $user
         $userlist[$i][1] = GUICtrlCreateListViewItem($user, $list)
         Return 1
      EndIf
   Next
   Return 0
EndFunc

Func _DelUser( $user )
   For $i = 0 to 16
      If $userlist[$i][0] = $user Then
         $userlist[$i][0] = ""
         GUICtrlDelete($userlist[$i][1])
         $userlist[$i][1] = 0
         Return 1
      EndIf
   Next
   Return 0
EndFunc

Func _ReduceMemory()
   DllCall("psapi.dll", "int", "EmptyWorkingSet", "long", -1)
EndFunc

Func mError( $sText, $iFatal = 0, $sTitle = "Error", $iOpt = 0 )
   Local $ret = MsgBox(48 + 4096 + 262144 + $iOpt, $sTitle, $sText)
   If $iFatal Then Exit
   Return $ret
EndFunc

Func _RC4( $sData, $sKey )   ; based on code by Valik
   Local $l[256], $i, $t, $x, $y, $s = ""
   Local $iLen = StringLen($sKey)
   For $c = 0 to 255
      $l[$c] = $c
   Next
   For $c = 0 to 255
      $i = Mod(Asc(StringMid($sKey, Mod($c, $iLen) + 1, 1)) + $l[$c] + $i, 256)
      $t = $l[$c]
      $l[$c] = $l[$i]
      $l[$i] = $t
   Next
   For $c = 1 to StringLen($sData)
      $x = Mod($x + 1, 256)
      $y = Mod($l[$x] + $y, 256)
      $t = $l[$x]
      $l[$x] = $l[$y]
      $l[$y] = $t
      $s &= Chr(BitXOR(Asc(StringMid($sData, $c, 1)), $l[Mod($l[$x] + $l[$y], 256)]))
   Next
   Return $s
EndFunc

Func _Time()   ; based on code by jjohn and his time sync script
   ; 129.6.15.28 = time-a.nist.gov
   Local $conn = TCPConnect("129.6.15.28", 37), $recv = ""
   If $conn = -1 or @error Then Return SetError(1, 0, "...")
   While $recv = ""
      $recv = TCPRecv($conn, 512)
      If @error Then Return SetError(2, 0, "...")
   WEnd
   TCPCloseSocket($conn)
   Return StringTrimRight(Asc(StringMid($recv,1,1))*256^3+Asc(StringMid($recv,2,1))*256^2+Asc(StringMid($recv,3,1))*256+Asc(StringMid($recv,4,1)),1)
EndFunc

Func _Exit()
   Exit
EndFunc

Func OnAutoItExit()
   TCPSend($sock, Chr(1) & "bye"

[center]Cookyx.com :: Simple LAN Chat[/center]

Share this post


Link to post
Share on other sites
The Kandie Man

Too much code to mess with. I wrote a chat program a while back and the way I did it was when someone joined the server it would broadcast a message to all currently connected clients that a new user had joined while it was adding the new user to its list of users in an array.

I hope this gives you an idea of what you have to do.

-The Kandie Man ;-)


"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Share this post


Link to post
Share on other sites
acidfear

have it check the socket. If it's the first time it has connected or sent data to the server, have it send that message off of a trigger.

you'd probably have to write a temp file or something on the server side to hold the list of currently connected sockets. Then just compare it through the list.

Share this post


Link to post
Share on other sites
JavaScript_Freek

i quite frankly don't get what im suppose to do.


[center]Cookyx.com :: Simple LAN Chat[/center]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×