Jump to content
Sign in to follow this  
JavaScript_Freek

Changing the Server IPs to conntect in Chat

Recommended Posts

JavaScript_Freek

Alright I've started to work on my own instant messeneger and I want the people's server to connect to 75.126.226.2 (mystikfilms.com). BUt it connects to 192.168.1.100. I tried changing some thing but no avail. Someone please help?

Server

HotKeySet("{ESC}", "OnAutoItExit")

Global Const $PASS = "dd"

Global Const $IP = @IPADDRESS1
Global Const $PORT = 31758

Global $listen
Global $list[17][2] ; [ index ][ socket, username ]

TCPStartup()

$listen = TCPListen($IP, $PORT, 17)
If $listen = -1 Then mError("Unable to connect.", 1)

TrayTip("Urban Chat", "Server is connected on:" & @CRLF & $IP & ":" & $PORT, 15, 1)
_ReduceMemory()

While 1
   _CheckConn()
   Sleep(0)
   $sock = TCPAccept($listen)
   If $sock = -1 Then ContinueLoop
   _AddConn($sock)
   _ReduceMemory()
WEnd


Func _AddConn( $iSocket )
   ; recieve the username from the client
   Local $recv = _SockRecv($iSocket), $temp
   If @error Then
      _DelConn($iSocket)
   Else
      ; if they are trying to login as admin then check the password
      If StringLeft($recv, 7) = Chr(1) & "admin" & Chr(2) Then
         $temp = StringSplit($recv, Chr(2))
         If _RC4(BinaryString("0x" & $temp[2]), _Time()) == $PASS and not $list[16][0] Then
            $list[16][0] = $iSocket
            $list[16][1] = "admin"
            TCPSend($iSocket, Chr(1) & "accepted")
            _SendUsers($iSocket)
            _SendAll(Chr(1) & "adduser" & Chr(2) & "admin", "", $iSocket)
            Return 1
         Else
            TCPSend($iSocket, Chr(1) & "rejected")
         EndIf
      Else
         ; find an empty space in the userlist
         For $i = 0 to 15
            If not $list[$i][0] Then
               $list[$i][0] = $iSocket
               $list[$i][1] = $recv
               TCPSend($iSocket, Chr(1) & "accepted")
               _SendUsers($iSocket)
               _SendAll(Chr(1) & "adduser" & Chr(2) & $recv, "", $iSocket)
               Return 1
            EndIf
         Next
         ; too many users, reject the connection
         TCPSend($iSocket, Chr(1) & "rejected")
         TCPCloseSocket($iSocket)
      EndIf
   EndIf
   Return 0
EndFunc

Func _DelConn( $iSocket )
   For $i = 0 to 16
      If $list[$i][0] = $iSocket Then
         TCPCloseSocket($iSocket)
         $list[$i][0] = 0
         $list[$i][1] = ""
         Return 1
      EndIf
   Next
   Return 0
EndFunc

Func _CheckConn()
   Local $recv = ""
   For $i = 0 to 16
      If $list[$i][0] Then
         $recv = TCPRecv($list[$i][0], 2048)
         If @error Then
            ; disconnect client
            TCPCloseSocket($list[$i][0])
            _SendAll(Chr(1) & "deluser" & Chr(2) & $list[$i][1])
            $list[$i][0] = 0
            $list[$i][1] = ""
         Else
            If $recv Then
               ; parse the command(s)
               $recv = StringSplit($recv, Chr(1))
               For $j = 1 to $recv[0]
                  $temp = StringSplit($recv[$j], Chr(2))
                  Switch $temp[1]
                     Case "bye"
                        TCPCloseSocket($list[$i][0])
                        _SendAll(Chr(1) & "deluser" & Chr(2) & $list[$i][1])
                        $list[$i][0] = 0
                        $list[$i][1] = ""
                     Case "kick"
                        _Kick($temp[2])
                     Case "PM"
                        _PM($temp[2], $temp[3], $list[$i][1])
                     Case Else
                        If $recv[$j] Then _SendAll($recv[$j], $list[$i][1])
                  EndSwitch
               Next
            EndIf
         EndIf
      EndIf
   Next
EndFunc

Func _SendAll( $str, $from = "", $except = 0 )
   ; send a message to all users
   For $i = 0 to 16
      If $list[$i][0] and $list[$i][0] <> $except Then
         If $from and StringLeft($str, 1) <> Chr(1) Then
            TCPSend($list[$i][0], $from & " > " & $str)
         Else
            TCPSend($list[$i][0], $str)
         EndIf
      EndIf
   Next
EndFunc

Func _Kick( $user )
   ; kick a user and close their connection
   For $i = 0 to 15
      If $list[$i][1] = $user Then
         TCPSend($list[$i][0], Chr(1) & "kick")
         TCPCloseSocket($list[$i][0])
         $list[$i][0] = 0
         _SendAll(Chr(1) & "deluser" & Chr(2) & $list[$i][1])
         $list[$i][1] = ""
         Return 1
      EndIf
   Next
   Return 0
EndFunc

Func _PM( $user, $str, $from )
   ; private message another user
   For $i = 0 to 16
      If $list[$i][1] = $user Then
         TCPSend($list[$i][0], "~ " & $from & " > " & $str)
         Return 1
      EndIf
   Next
   Return 0
EndFunc

Func _SendUsers( $iSocket )
   ; give the userlist to a connecting client
   For $i = 0 to 16
      If $list[$i][0] Then TCPSend($iSocket, Chr(1) & "adduser" & Chr(2) & $list[$i][1])
   Next
EndFunc

Func _SockRecv( $iSocket, $iBytes = 2048 )
   Local $sData = ""
   While $sData = ""
      $sData = TCPRecv($iSocket, $iBytes)
      If @error Then Return SetError(1, 0, 0)
   Wend
   Return $sData
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 OnAutoItExit()
   _SendAll(Chr(1) & "exit")
   TCPShutdown()
   Exit
EndFuncoÝ÷ Ù°¥éíý¶®¶­sb6æ6ÇVFRfÇC´uT6öç7FçG2æS2fwC°¤÷BgV÷C´uTöäWfVçDÖöFRgV÷C²Â ¤vÆö&Â6öç7Bb33cµõ%BÒ3sS¤vÆö&Âb33c¶Æ7BÂb33c·6ö6°¤vÆö&Âb33c·W6W&Æ7E³uÕ³%в²æFWÕ²W6W&æÖRÂ7G&ÂBÐ ¥D57F'EW ¢b33c¶wVÒuT7&VFRgV÷C´ÆövâgV÷C²Â#c¤uT7G&Ä7&VFTÆ&VÂgV÷Cµ6W'fW"gV÷C²ÂÂÂcÂ#¤uT7G&Ä7&VFTÆ&VÂgV÷CµW6W&æÖRgV÷C²ÂÂCÂcÂ#¢b33c¶7G&ÂÒuT7G&Ä7&VFTçWBDE$U53ÂÂÂsÂ#¢b33c·W6W&7G&ÂÒuT7G&Ä7&VFTçWBgV÷C²gV÷C²ÂÂCÂsÂ#¢b33c¶ÆövâÒuT7G&Ä7&VFT'WGFöâgV÷C´ÆövâgV÷C²ÂsÂsÂÂ#Âb33c´%5ôDTeU4%UEDôâ ¤uT7G&Å6WDöäWfVçBb33c¶ÆövâÂgV÷CµôÆövâgV÷C²¤uT6WDöäWfVçBb33c´uTôUdTåEô4Äõ4RÂgV÷CµôWBgV÷C² ¤uT6WE7FFR¢b33c¶Æö÷ÒG'VP ¥vÆRb33c¶Æö÷¢6ÆVW#¥tVæ@ ¢b33c¶wVÒuT7&VFRgV÷CµW&&â6BÒW6W"gV÷C²fײb33c·W6W"fײgV÷C²gV÷C²ÂCcÂ#3¢b33c¶Æ7BÒuT7G&Ä7&VFTÆ7EfWrgV÷C²W6W'2gV÷C²Â3SÂÂÂ#¢b33c¶VFBÒuT7G&Ä7&VFTVFBgV÷C²gV÷C²ÂÂsÂ33ÂSÂb33c´U5õ$TDôäŲb33cµu5õe45$ôIJb33c´U5ôUDõe45$ôÄ¢b33c¶çWBÒuT7G&Ä7&VFTçWBgV÷C²gV÷C²ÂÂÂ33Â#¢b33c·6VæBÒuT7G&Ä7&VFT'WGFöâgV÷Cµ6VæBgV÷C²ÂÂCÂÂ#Âb33c´%5ôDTeU4%UEDôâ¢b33c·ÒÒuT7G&Ä7&VFT'WGFöâgV÷CµÒgV÷C²ÂÂCÂÂ# ¤uT7G&Å6WDöäWfVçBb33c·6VæBÂgV÷Cµõ6VæBgV÷C²¤uT7G&Å6WDöäWfVçBb33c·ÒÂgV÷CµõÒgV÷C²¤uT6WDöäWfVçBb33c´uTôUdTåEô4Äõ4RÂgV÷CµôWBgV÷C² ¤uT7G&Å6WE7FFRb33c¶çWBÂb33c´uTôdô5U2¤uT6WE7FFR ¥õ&VGV6TÖVÖ÷' ¥vÆR¢6ÆVW¢b33c·&V7bÒD5&V7bb33c·6ö6²Â#C¢bW'&÷"FVâÔW'&÷"gV÷C´6öææV7FöâÆ÷7BâgV÷C²Â¢bæ÷Bb33c·&V7bFVâ6öçFçVTÆö÷¢²'6RFR6öÖÖæB2¢b33c·&V7bÒ7G&æu7ÆBb33c·&V7bÂ6"¢f÷"b33c¶ÒFòb33c·&V7e³Ð b33c·FV×Ò7G&æu7ÆBb33c·&V7e²b33c¶ÒÂ6"" 7vF6b33c·FV׳Р66RgV÷C·&V¦V7FVBgV÷C° ÔW'&÷"gV÷C´6öææV7Föâ&V¦V7FVBâgV÷C²Â 66RgV÷C¶¶6²gV÷C° ÔW'&÷"gV÷C´6öææV7Föâ¶6¶VBâgV÷C²Â 66RgV÷C¶66WFVBgV÷C° bb33cµU4U"ÒgV÷C¶FÖâgV÷C²FVà b33c¶¶6²ÒuT7G&Ä7&VFT'WGFöâgV÷C´¶6²gV÷C²ÂÂCÂÂ# uT7G&Å6WDöäWfVçBb33c¶¶6²ÂgV÷Cµô¶6²gV÷C² VæD` 66RgV÷C¶FGW6W"gV÷C° ôFEW6W"b33c·FV׳%Ò 66RgV÷C¶FVÇW6W"gV÷C° ôFVÅW6W"b33c·FV׳%Ò 66RgV÷C¶WBgV÷C° ÔW'&÷"gV÷Cµ6W'fW"26Æ÷6VBâgV÷C²Â 66RVÇ6P bb33c·&V7e²b33c¶ÒFVâuT7G&Å6WDFFb33c¶VFBÂb33c·&V7e²b33c¶Òfײ5$Äb VæE7vF6¢æW@¢õ&VGV6TÖVÖ÷'¥tVæ@  ¤gVæ2ôÆövâ¢b33c¶Æö÷ÒfÇ6P¢vÆö&Â6öç7Bb33c´ÒuT7G&Å&VBb33c¶7G&¢vÆö&Â6öç7Bb33cµU4U"ÒuT7G&Å&VBb33c·W6W&7G&¢uTFVÆWFRb33c¶wV¢bb33cµU4U"ÒgV÷C¶FÖâgV÷C²FVà Æö6Âb33c·72ÒçWD&÷gV÷C´VçFW"77v÷&C¢gV÷C²ÂgV÷CµÆV6RVçFW"FR6W'fW"77v÷&B&VÆ÷râgV÷C²ÂgV÷C²gV÷C²ÂgV÷C¹RgV÷C² bW'&÷"FVâW@ b33c·6ö6²ÒD56öææV7Bb33c´Âb33cµõ%B bb33c·6ö6²ÒÓFVâÔW'&÷"gV÷CµVæ&ÆRFò6öææV7BâgV÷C²Â¢VÇ6P b33c·6ö6²ÒD56öææV7Bb33c´Âb33cµõ%B bb33c·6ö6²ÒÓFVâÔW'&÷"gV÷CµVæ&ÆRFò6öææV7BâgV÷C²Â D56VæBb33c·6ö6²Âb33cµU4U"¢VæD`¤VæDgVæ0 ¤gVæ2õ6VæB¢bæ÷BuT7G&Å&VBb33c¶çWBFVâ&WGW&â¢D56VæBb33c·6ö6²Â7G&æu&WÆ6R7G&æu&WÆ6RuT7G&Å&VBb33c¶çWBÂ6"ÂgV÷C²gV÷C²Â6""ÂgV÷C²gV÷C²¢uT7G&Å6WDFFb33c¶çWBÂgV÷C²gV÷C²¢&WGW&â¤VæDgVæ0 ¤gVæ2õÒ¢bæ÷BuT7G&Å&VBb33c¶çWBFVâ&WGW&â¢Æö6Âb33c·W6W"ÒuT7G&Å&VBb33c¶Æ7BÂb33c¶FFÒ7G&æu&WÆ6R7G&æu&WÆ6RuT7G&Å&VBb33c¶çWBÂ6"ÂgV÷C²gV÷C²Â6""ÂgV÷C²gV÷C²¢bæ÷Bb33c·W6W"FVâ&WGW&â¢b33c·W6W"ÒuT7G&Å&VBb33c·W6W"¢D56VæBb33c·6ö6²Â6"fײgV÷CµÒgV÷C²fײ6""fײb33c·W6W"fײ6""fײb33c¶FF¢uT7G&Å6WDFFb33c¶çWBÂgV÷C²gV÷C²¢uT7G&Å6WDFFb33c¶VFBÂgV÷C²fÇC²¢¢Ò6VçBFó¢gV÷C²fײb33c·W6W"fײgV÷C²¢¢fwC²gV÷C²fײ5$ÄbfײgV÷C²fÇC²¢¢gV÷C²fײb33c¶FFfײgV÷C²¢¢fwC²gV÷C²fײ5$Äb¢&WGW&â¤VæDgVæ0 ¤gVæ2ô¶6²¢Æö6Âb33c·W6W"ÒuT7G&Å&VBb33c¶Æ7B¢bæ÷Bb33c·W6W"FVâ&WGW&â¢b33c·W6W"ÒuT7G&Å&VBb33c·W6W"¢D56VæBb33c·6ö6²Â6"fײgV÷C¶¶6²gV÷C²fײ6""fײb33c·W6W"¢uT7G&Å6WDFFb33c¶VFBÂgV÷C²fÇC²¢¢¶6³¢gV÷C²fײb33c·W6W"fײgV÷C²¢¢fwC²gV÷C²fײ5$Äb¢&WGW&â¤VæDgVæ0 ¤gVæ2ôFEW6W"b33c·W6W"¢bb33c·W6W"ÒgV÷C¶FÖâgV÷C²æBæ÷Bb33c·W6W&Æ7E³eÕ³ÒFVà b33c·W6W&Æ7E³eÕ³ÒÒb33c·W6W  b33c·W6W&Æ7E³eÕ³ÒÒuT7G&Ä7&VFTÆ7EfWtFVÒb33c·W6W"Âb33c¶Æ7B &WGW&â¢VæD`¢f÷"b33c¶ÒFòP bæ÷Bb33c·W6W&Æ7E²b33c¶Õ³ÒFVà b33c·W6W&Æ7E²b33c¶Õ³ÒÒb33c·W6W  b33c·W6W&Æ7E²b33c¶Õ³ÒÒuT7G&Ä7&VFTÆ7EfWtFVÒb33c·W6W"Âb33c¶Æ7B &WGW&â VæD`¢æW@¢&WGW&â¤VæDgVæ0 ¤gVæ2ôFVÅW6W"b33c·W6W"¢f÷"b33c¶ÒFò` bb33c·W6W&Æ7E²b33c¶Õ³ÒÒb33c·W6W"FVà b33c·W6W&Æ7E²b33c¶Õ³ÒÒgV÷C²gV÷C° uT7G&ÄFVÆWFRb33c·W6W&Æ7E²b33c¶Õ³Ò b33c·W6W&Æ7E²b33c¶Õ³ÒÒ &WGW&â VæD`¢æW@¢&WGW&â¤VæDgVæ0 ¤gVæ2õ&VGV6TÖVÖ÷'¢FÆÄ6ÆÂgV÷C·6æFÆÂgV÷C²ÂgV÷C¶çBgV÷C²ÂgV÷C´V×Gv÷&¶æu6WBgV÷C²ÂgV÷C¶ÆöærgV÷C²ÂÓ¤VæDgVæ0 ¤gVæ2ÔW'&÷"b33c·5FWBÂb33c¶fFÂÒÂb33c·5FFÆRÒgV÷C´W'&÷"gV÷C²Âb33c¶÷BÒ¢Æö6Âb33c·&WBÒ×6t&÷C²Cb²#c#CB²b33c¶÷BÂb33c·5FFÆRÂb33c·5FWB¢bb33c¶fFÂFVâW@¢&WGW&âb33c·&W@¤VæDgVæ0 ¤gVæ2õ$3Bb33c·4FFÂb33c·4¶W²&6VBöâ6öFR'fÆ°¢Æö6Âb33c¶Å³#SeÒÂb33c¶Âb33c·BÂb33c·Âb33c·Âb33c·2ÒgV÷C²gV÷C°¢Æö6Âb33c¶ÆVâÒ7G&ætÆVâb33c·4¶W¢f÷"b33c¶2ÒFò#SP b33c¶Å²b33c¶5ÒÒb33c¶0¢æW@¢f÷"b33c¶2ÒFò#SP b33c¶ÒÖöB627G&ætÖBb33c·4¶WÂÖöBb33c¶2Âb33c¶ÆVâ²Â²b33c¶Å²b33c¶5Ò²b33c¶Â#Sb b33c·BÒb33c¶Å²b33c¶5Ð b33c¶Å²b33c¶5ÒÒb33c¶Å²b33c¶Ð b33c¶Å²b33c¶ÒÒb33c·@¢æW@¢f÷"b33c¶2ÒFò7G&ætÆVâb33c·4FF b33c·ÒÖöBb33c·²Â#Sb b33c·ÒÖöBb33c¶Å²b33c·Ò²b33c·Â#Sb b33c·BÒb33c¶Å²b33c·Ð b33c¶Å²b33c·ÒÒb33c¶Å²b33c·Ð b33c¶Å²b33c·ÒÒb33c·@ b33c·2f׳Ò6"&Eõ"627G&ætÖBb33c·4FFÂb33c¶2ÂÂb33c¶Å´ÖöBb33c¶Å²b33c·Ò²b33c¶Å²b33c·ÒÂ#SbÒ¢æW@¢&WGW&âb33c·0¤VæDgVæ0 ¤gVæ2õFÖR²&6VBöâ6öFR'¦¦öâæB2FÖR7æ267&@¢²#ãbãRã#ÒFÖRÖææ7Bæv÷`¢Æö6Âb33c¶6öæâÒD56öææV7BgV÷C³#ãbãRã#gV÷C²Â3rÂb33c·&V7bÒgV÷C²gV÷C°¢bb33c¶6öæâÒÓ÷"W'&÷"FVâ&WGW&â6WDW'&÷"ÂÂgV÷C²âââgV÷C²¢vÆRb33c·&V7bÒgV÷C²gV÷C° b33c·&V7bÒD5&V7bb33c¶6öæâÂS" bW'&÷"FVâ&WGW&â6WDW'&÷""ÂÂgV÷C²âââgV÷C²¢tVæ@¢D56Æ÷6U6ö6¶WBb33c¶6öæâ¢&WGW&â7G&æuG&Õ&vB627G&ætÖBb33c·&V7bÃã#Seã2´627G&ætÖBb33c·&V7bÃ"ã#Seã"´627G&ætÖBb33c·&V7bÃ2ã#Sb´627G&ætÖBb33c·&V7bÃBÃäVæDgVæ0 ¤gVæ2ôWB¢W@¤VæDgVæ0 ¤gVæ2öäWFôDWB¢D56VæBb33c·6ö6²Â6"fײgV÷C¶'RgV÷C²¢D56WFF÷vâ¢W@¤VæDgVæ

Thank you. :)


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

Share this post


Link to post
Share on other sites
tAKTelapis

Hmm, somehow i dont think you wrote that script..

Eitherway, @ipaddress1 -> is your local IP.

Hope that helps.

Share this post


Link to post
Share on other sites
Gif

Hmm, somehow i dont think you wrote that script..

Eitherway, @ipaddress1 -> is your local IP.

Hope that helps.

for local ip of first adaptor

@IpAddress1

and for external

#include <INet.au3>
_GetIP()
Edited by c4nm7

Share this post


Link to post
Share on other sites
JavaScript_Freek

What should I change so EVERYONE connects to: 75.126.226.2

Server.au3

HotKeySet("{ESC}", "OnAutoItExit")

Global Const $PASS = "dd"

Global Const $IP = @IPADDRESS1
Global Const $PORT = 31758

Global $listen
Global $list[17][2] ; [ index ][ socket, username ]

TCPStartup()

$listen = TCPListen($IP, $PORT, 17)
If $listen = -1 Then mError("Unable to connect.", 1)

TrayTip("Urban Chat", "Server is connected on:" & @CRLF & $IP & ":" & $PORT, 15, 1)
_ReduceMemory()

While 1
   _CheckConn()
   Sleep(0)
   $sock = TCPAccept($listen)
   If $sock = -1 Then ContinueLoop
   _AddConn($sock)
   _ReduceMemory()
WEnd


Func _AddConn( $iSocket )
   ; recieve the username from the client
   Local $recv = _SockRecv($iSocket), $temp
   If @error Then
      _DelConn($iSocket)
   Else
      ; if they are trying to login as admin then check the password
      If StringLeft($recv, 7) = Chr(1) & "admin" & Chr(2) Then
         $temp = StringSplit($recv, Chr(2))
         If _RC4(BinaryString("0x" & $temp[2]), _Time()) == $PASS and not $list[16][0] Then
            $list[16][0] = $iSocket
            $list[16][1] = "admin"
            TCPSend($iSocket, Chr(1) & "accepted")
            _SendUsers($iSocket)
            _SendAll(Chr(1) & "adduser" & Chr(2) & "admin", "", $iSocket)
            Return 1
         Else
            TCPSend($iSocket, Chr(1) & "rejected")
         EndIf
      Else
         ; find an empty space in the userlist
         For $i = 0 to 15
            If not $list[$i][0] Then
               $list[$i][0] = $iSocket
               $list[$i][1] = $recv
               TCPSend($iSocket, Chr(1) & "accepted")
               _SendUsers($iSocket)
               _SendAll(Chr(1) & "adduser" & Chr(2) & $recv, "", $iSocket)
               Return 1
            EndIf
         Next
         ; too many users, reject the connection
         TCPSend($iSocket, Chr(1) & "rejected")
         TCPCloseSocket($iSocket)
      EndIf
   EndIf
   Return 0
EndFunc

Func _DelConn( $iSocket )
   For $i = 0 to 16
      If $list[$i][0] = $iSocket Then
         TCPCloseSocket($iSocket)
         $list[$i][0] = 0
         $list[$i][1] = ""
         Return 1
      EndIf
   Next
   Return 0
EndFunc

Func _CheckConn()
   Local $recv = ""
   For $i = 0 to 16
      If $list[$i][0] Then
         $recv = TCPRecv($list[$i][0], 2048)
         If @error Then
            ; disconnect client
            TCPCloseSocket($list[$i][0])
            _SendAll(Chr(1) & "deluser" & Chr(2) & $list[$i][1])
            $list[$i][0] = 0
            $list[$i][1] = ""
         Else
            If $recv Then
               ; parse the command(s)
               $recv = StringSplit($recv, Chr(1))
               For $j = 1 to $recv[0]
                  $temp = StringSplit($recv[$j], Chr(2))
                  Switch $temp[1]
                     Case "bye"
                        TCPCloseSocket($list[$i][0])
                        _SendAll(Chr(1) & "deluser" & Chr(2) & $list[$i][1])
                        $list[$i][0] = 0
                        $list[$i][1] = ""
                     Case "kick"
                        _Kick($temp[2])
                     Case "PM"
                        _PM($temp[2], $temp[3], $list[$i][1])
                     Case Else
                        If $recv[$j] Then _SendAll($recv[$j], $list[$i][1])
                  EndSwitch
               Next
            EndIf
         EndIf
      EndIf
   Next
EndFunc

Func _SendAll( $str, $from = "", $except = 0 )
   ; send a message to all users
   For $i = 0 to 16
      If $list[$i][0] and $list[$i][0] <> $except Then
         If $from and StringLeft($str, 1) <> Chr(1) Then
            TCPSend($list[$i][0], $from & " says: " & $str)
         Else
            TCPSend($list[$i][0], $str)
         EndIf
      EndIf
   Next
EndFunc

Func _Kick( $user )
   ; kick a user and close their connection
   For $i = 0 to 15
      If $list[$i][1] = $user Then
         TCPSend($list[$i][0], Chr(1) & "kick")
         TCPCloseSocket($list[$i][0])
         $list[$i][0] = 0
         _SendAll(Chr(1) & "deluser" & Chr(2) & $list[$i][1])
         $list[$i][1] = ""
         Return 1
      EndIf
   Next
   Return 0
EndFunc

Func _PM( $user, $str, $from )
   ; private message another user
   For $i = 0 to 16
      If $list[$i][1] = $user Then
         TCPSend($list[$i][0], "PM sent by: " & $from & " : " & $str)
         Return 1
      EndIf
   Next
   Return 0
EndFunc

Func _SendUsers( $iSocket )
   ; give the userlist to a connecting client
   For $i = 0 to 16
      If $list[$i][0] Then TCPSend($iSocket, Chr(1) & "adduser" & Chr(2) & $list[$i][1])
   Next
EndFunc

Func _SockRecv( $iSocket, $iBytes = 2048 )
   Local $sData = ""
   While $sData = ""
      $sData = TCPRecv($iSocket, $iBytes)
      If @error Then Return SetError(1, 0, 0)
   Wend
   Return $sData
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 OnAutoItExit()
   _SendAll(Chr(1) & "exit")
   TCPShutdown()
   Exit
EndFunc

[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  

×