Jump to content

BTCP


CodyBarrett
 Share

Recommended Posts

I KNOW! there are many a many TCP chat rooms out there in autoitworld of scripting

BUT! i have made a multi client chatroom and im kinda proud of it, so incase you want... the files are here:

Multiclient SERVER:

#include <MISC.AU3>
#include <String.au3>
#include <ARRAY.AU3>
#include <Date.AU3>
HotKeySet ('!+^{ESC}','_Exit')
TCPStartup ()
$MainSocket = TCPListen (@IPAddress1,14789,500)
Dim $sckt[500 + 1][3]
For $a = 0 To 500
    For $b = 0 To 2
        $sckt[$a][$b] = 0
    Next
Next
While 1
    _Accept ()
    For $a = 1 To 500
        If $sckt[$a][0] <> 0 Then 
            $Rcv = TCPRecv ($sckt[$a][0],1000000)
            If $Rcv <> '' Then
                If $Rcv = '>>' & $sckt[$a][1] & ' Has Left The Room.' Then _DC ($sckt[$a][0])
                For $b = 1 To 500
                    TCPSend ($sckt[$b][0], _NowTime () & $Rcv)
                Next
            EndIf
        EndIf
    Next
    TrayTip ('Stats','Clients=' & $sckt[0][0],5)
    Sleep (10)
WEnd
Func _Accept ()
    $SOCKET = TCPAccept ($MainSocket)
    If $SOCKET = -1 then Return
    For $a = 1 To 500
        If $sckt[$a][0] = 0 Then
            Do
                $Rcv = TCPRecv ($SOCKET,1000000)
            Until $Rcv <> ''
            If StringLeft ($Rcv, 4) = 'Data' Then
                $b = _StringBetween ($Rcv,'(',')')
                If Not IsArray ($b) Then
                    TCPCloseSocket ($SOCKET)
                    Return
                Else
                    $sckt[$a][0] = $SOCKET
                    $sckt[$a][1] = $b[0]
                    $sckt[$a][2] = $b[1]
                    $sckt[0][0] += 1
                    Return
                EndIf
            EndIf
        EndIf
    Next
EndFunc
Func _DC ($a)
    For $b = 1 To 500
        If $sckt[$b][0] <> 0 Then
            If $sckt[$b][0] = $a Then 
                TCPCloseSocket ($sckt[$b][0])
                $sckt[0][0] -= 1
                Return
            EndIf
        EndIf
    Next
EndFunc
Func _Exit ()
    For $a = 1 To 500
        If $sckt[$a][0] <> 0 Then
            TCPSend ($sckt[$a][0],'DC')
            TCPCloseSocket ($sckt[$a][0])
        EndIf
    Next
    TCPCloseSocket ($MainSocket)
    TCPShutdown ()
    Exit
EndFunc

Client...

#include <GUICONSTANTS.AU3>
#include <WINDOWSCONSTANTS.AU3>
#include <STATICCONSTANTS.AU3>
#include <EDITCONSTANTS.AU3>
#include <GUIEDIT.AU3>
#include <MISC.AU3>
#Include <WinAPI.au3>
Opt ('GUIoneventmode', 1)
TCPStartup ()
$IP = 4444
$iIP = 0
$PORT = @IPAddress1
$iPORT = 0
$Name = @UserName
$SOCKET = -1
$GUI = 0
$GUI2 = 0
$C_CI = BitOR ($SS_CENTER,$SS_CENTERIMAGE)
$GuiX = 700
$GuiY = 500
$sName = StringTrimRight (@ScriptName,4)
$GUI = _GC ($sName,$GuiX,$GuiY,0x1)

$bConnect = _GccB ('Connect',20,40,200,20)
GUICtrlSetOnEvent (-1, '_Connect')
    
$iName = GUICtrlCreateInput ($Name,240,40,200,20,$C_CI)
    GUICtrlSetBkColor (-1, 0x0)
    GUICtrlSetFont (-1, 10, 400, '', 'Fixedsys')
    GUICtrlSetColor (-1, 0xFFFFFF)
    
$bName = _GccB ('Change Name',460,40,220,20)
GUICtrlSetOnEvent (-1,'_ChangeName')

$Console = GUICtrlCreateEdit ('',20,70,$GuiX - 40, $GuiY - 190,BitOR($ES_MULTILINE,$WS_VSCROLL, $ES_READONLY))
    GUICtrlSetColor (-1,0xFFFFFF)
    GUICtrlSetBkColor (-1, 0x0)
    GUICtrlSetFont (-1, 10,900,'','Tahoma')
    
$Input = GUICtrlCreateEdit ('',20,$GuiY - 100,$GuiX - 40, $GuiY - 420,BitOR($ES_MULTILINE,$WS_VSCROLL))
    GUICtrlSetColor (-1,0xFFFFFF)
    GUICtrlSetBkColor (-1, 0x0)
    GUICtrlSetFont (-1, 10,900,'','Tahoma')
    GUICtrlSetLimit (-1, 250,0)
    
While 1
    If _IsPressed ('0D') And ControlGetFocus ($GUI,'') = 'Edit3' Then
        $a = GUICtrlRead ($Input)
        If $a <> '' And Not StringIsSpace ($a) Then _SendDATA ($a)
    EndIf
    If $Name = GUICtrlRead ($iName) Then 
        GUICtrlSetState ($bName,$GUI_DISABLE)
    Else
        GUICtrlSetState ($bName,$GUI_ENABLE)        
    EndIf
    If $SOCKET <> -1 Then
        $Rcv = TCPRecv ($SOCKET,1000000)
        If $Rcv <> '' Then
            If $Rcv = 'DC' Then 
                Sleep (2000)
                WinActivate ($GUI,'')
                _Exit ()
            EndIf
            _GUICtrlEdit_AppendText ($Console, $Rcv)
        EndIf
    EndIf
    Sleep (100)
WEnd
Func _ChangeName ()
    $a = GUICtrlRead ($iName)
    If $SOCKET <> -1 Then TCPSend ($SOCKET,'>>' & $Name & ' Has Changed their Name to : ' & $a & @CRLF)
    $Name = $a
EndFunc
Func _Connect ()
    If WinExists ('Connection Data','') Then Return
    _GC ('Connection Data',200,140,-1)
    $iIP = GUICtrlCreateInput (@IPAddress1,20,20,160,20,$C_CI)
    $iPORT = GUICtrlCreateInput (14789,20,60,160,20,BitOR ($C_CI,$ES_NUMBER))
    $bOK = _GccB ('Ok',20,100,70,20)
    GUICtrlSetOnEvent (-1,'_ConOK')
    $bCancel = _GccB ('Cancel',110,100,70,20)
    GUICtrlSetOnEvent (-1,'_WinClose')
EndFunc
Func _ConOK ()
    $IP = GUICtrlRead ($iIP)
    $PORT = GUICtrlRead ($iPORT)
    TCPStartup ()
    WinActivate ('Connection Data','')
    _WinClose ()
    GUICtrlSetData ($Console,'>>Connecting :' & @CRLF & @TAB & 'IP=' & $IP & @CRLF & @TAB & 'PORT=' & $PORT & @CRLF)
    For $a = 0 To 10
        $SOCKET = TCPConnect ($IP,$PORT)
        If $SOCKET <> -1 Then ExitLoop
        TCPCloseSocket ($SOCKET)
        GUICtrlSetData ($Console,StringReplace (GUICtrlRead ($Console),'>>Connecting...' & $a * 10 - 10 & '%' & @CRLF,''))
        If $a = 10 Then
            $a = MsgBox (5,'Error..','There has been an Error Connecting to the Server' & @CRLF & @TAB & 'ErrorCode=' & @error,'',$GUI)
            Select
                Case $a = 4
                    $a = 0
                Case $a = 2
                    TCPCloseSocket ($SOCKET)
                    TCPShutdown ()
                    GUICtrlSetData ($Console,'')
                    ExitLoop
            EndSelect
        EndIf
        _GUICtrlEdit_AppendText ($Console,'>>Connecting...' & $a * 10 & '%' & @CRLF)
        Sleep (100)
    Next
    If $a <> 2 Then
        GUICtrlSetState ($bConnect,$GUI_DISABLE)
        _GUICtrlEdit_AppendText ($Console,'>>Connection Complete.' & @CRLF)
        TCPSend ($SOCKET,'Data(' & $Name & ')(' & @UserName & ')')
        Sleep (500)
        TCPSend ($SOCKET,'>>' & $Name & ' Has Joined the Room.' & @CRLF)
    Else
        _GUICtrlEdit_AppendText ($Console,'>>Connection Canceled.' & @CRLF)
    EndIf
EndFunc
Func _WinClose ()
    GUIDelete (WinGetHandle ('[active]',''))
EndFunc
Func _SendDATA ($a)
    $DATA = '>>' & $Name & ' : ' & @CRLF & @TAB & StringReplace ($a,@CRLF,'') & @CRLF
    If $SOCKET <> -1 Then TCPSend ($SOCKET, $DATA)
    GUICtrlSetData ($Input,'')
    GUICtrlSetBkColor ($Input,0x4E4E4E)
    Sleep (300)
    GUICtrlSetBkColor ($Input,0x0)
EndFunc
Func _GccB ($bText,$x,$y,$w,$h)
    GUICtrlCreateLabel ('',$x,$y,$w,$h)
    GUICtrlSetBkColor (-1, 0x414141)
    GUICtrlSetState (-1,$GUI_DISABLE)
    GUICtrlCreateLabel ('',$x,$y + $h / 2,$w,$h / 2)
    GUICtrlSetBkColor (-1,0x313131)
    GUICtrlSetState (-1,$GUI_DISABLE)
    $a = GUICtrlCreateLabel ($bText,$x,$y,$w,$h,$C_CI)
    GUICtrlSetBkColor (-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetFont (-1, 10, 400, '', 'Fixedsys')
    GUICtrlSetColor (-1, 0xFFFFFF)
    GUICtrlSetCursor (-1, 0)
    Return $a
EndFunc
Func _GC ($wTitle,$x,$y,$wStyle)
    Select
        Case $wStyle = 1 Or $wStyle = -1
            $GUIStyle = BitOR($WS_DLGFRAME,$WS_POPUP, $WS_VISIBLE)
            $GUIStyleEx = $WS_EX_WINDOWEDGE
            If $wStyle = -1 Then $GUIStyleEx = BitOR ($GUIStyleEx, $WS_EX_TOPMOST)
        Case $wStyle = 0
            $GUIStyle = -1
            $GUIStyleEx = -1
    EndSelect
    $a = GUICreate ($wTitle, $x,$y, -1, -1,$GUIStyle,$GUIStyleEx)
    If $wStyle = 1 Then
        GUICtrlCreateLabel ('',0,0,$GuiX,10)
        GUICtrlSetBkColor (-1, 0x414141)
        GUICtrlSetState (-1,$GUI_DISABLE)
        GUICtrlCreateLabel ('',0,10,$GuiX,10)
        GUICtrlSetBkColor (-1,0x313131)
        GUICtrlSetState (-1,$GUI_DISABLE)
        GUICtrlCreateLabel (WinGetTitle ($a), 40, 0, $GuiX - 40, 20, $C_CI,$GUI_WS_EX_PARENTDRAG)
        GUICtrlSetBkColor (-1, $GUI_BKCOLOR_TRANSPARENT)
        GUICtrlSetFont (-1, 10, 400, '', 'Fixedsys')
        GUICtrlSetColor (-1, 0xFFFFFF)
        GUICtrlCreateLabel ('x', 0, 0, 20, 20, $C_CI)
        GUICtrlSetCursor (-1, 0)
        GUICtrlSetBkColor (-1, $GUI_BKCOLOR_TRANSPARENT)
        GUICtrlSetFont (-1, 10, 400, '', 'Fixedsys')
        GUICtrlSetColor (-1, 0xFFFFFF)
        GUICtrlSetOnEvent (-1, '_Exit')
        GUICtrlCreateLabel ('_', 20, 0, 20, 20, $C_CI)
        GUICtrlSetCursor (-1, 0)
        GUICtrlSetBkColor (-1, $GUI_BKCOLOR_TRANSPARENT)
        GUICtrlSetFont (-1, 10, 400, '', 'Fixedsys')
        GUICtrlSetColor (-1, 0xFFFFFF)
        GUICtrlSetOnEvent (-1, '_Minn')
    EndIf
    GUISetOnEvent ($GUI_EVENT_CLOSE, '_Exit', $a)
    GUISetState ()
    Return $a
EndFunc
Func _onclick ()
    GUICtrlSetColor (@GUI_CtrlId,0x0)
    GUICtrlSetBkColor (@GUI_CtrlId,0x4E4E4E)
    Sleep (300)
    GUICtrlSetColor (@GUI_CtrlId,0xFFFFFF)
    GUICtrlSetBkColor (@GUI_CtrlId,$GUI_BKCOLOR_TRANSPARENT)
EndFunc
Func _Minn ()
    _onclick ()
    WinSetState ($GUI, '', @SW_MINIMIZE)
EndFunc
Func _Exit ()
    If Not WinActive ($GUI) Then Return
    TCPSend ($SOCKET, '>>' & $Name & ' Has Left The Room.' & @CRLF)
    TCPCloseSocket ($SOCKET)
    TCPShutdown ()
    Exit
EndFunc

and im currently working on :

Client-Admin status

One-Vs-one Game

Pming

Client-Disconnectotherclient

Displaypics

anyother ideas? <img src="http://www.autoitscript.com/forum/public/style_emoticons/<#EMO_DIR#>/biggrin.gif" style="vertical-align:middle" emoid=":)" border="0" alt="biggrin.gif" />

Edited by CodyBarrett
Link to comment
Share on other sites

thank you, as you can see the server disconnects the client by NAME, font&Background&Port&IP&Name&maxconnections are editable, if there are any bugs please say so.

Link to comment
Share on other sites

OK i have Finished the NEW VERSION... (will upload code later on today)

Stuff added :

*Client-admin

*clients disconecting other clients

*Admin password is hardcoded in but is unuiqe for every computer

*when name changes it replaces the old name of the client in the $Clients[0][0] Array

i not at my copmuter right now will upload later today... and like always please tell if Bugs, or if you have ideas...

Link to comment
Share on other sites

Func Broadcast($Msg)
    For $i = 0 To $MaxConnections
        If $Clients[$i][0] <> -1  Then TCPSend($Clients[$i][0],$Msg)
    Next
EndFuncoÝ÷ ÚÈhºW[y«­¢+ÙÕ¹    ɽÍÐ ÀÌØí5ͤ(%½ÈÀÌØí¤ôÀQ¼ÀÌØí5á
½¹¹Ñ¥½¹Ì($%%ÀÌØí
±¥¹ÑÍlÀÌØí¥ulÁtÐìÀQ¡¸Q
AM¹ ÀÌØí
±¥¹ÑÍlÀÌØí¥ulÁt°ÀÌØí5ͤ(%9áÐ)¹Õ¹

Only two things are infinite, the universe and human stupidity, and i'm not sure about the former -Alber EinsteinPractice makes perfect! but nobody's perfect so why practice at all?http://forum.ambrozie.ro

Link to comment
Share on other sites

huh... wow, why did i miss it... anyway ill add that.. DONE ok here is the version i abstained to upload.... ok its at the top

and uhh multilplayer, would be possible but not at the top of my list right now ill work on it, gotta find a game first..... anyway ill get to that later..

VERSION #4 IS AT THE TOP

Link to comment
Share on other sites

ERROR REPORT

the client actually cannot disconnect another client... there is an error with the $Clients[$i][0] but not sure... any help

Link to comment
Share on other sites

  • 5 months later...

DID NOT update code.. just reposted it because the code was corrupted

Link to comment
Share on other sites

that would be nice... but also kind of unneeded.. seeing how its a CHATROOM.. where as many people as you can handle CAN be on it... imagine 30 ppl typing at the same time... what would your Label do? flicker... OR display the last person that is typing... its possible to do this but i dont see the point

Link to comment
Share on other sites

its a good idea... for 1 on 1 messenging... i might add that into my current chatroom project OECR... yes i have a lot XD and none of them are finished

Link to comment
Share on other sites

  • 2 months later...

NEW VERSION

ALrighty... i looked through some UNfinished projects of mine.

came up with this, i took out everything that was unneeded and left the basics. its still a functioning chat (COMPLETED as far as im concerned)

but can only do these:

*logon

*change name

*send

*recv

*Disconnect on server shutdown

i changed the GUI also :)

Link to comment
Share on other sites

  • 2 months later...

Can you please re-post or either send me the older version with admin and stuff, I really think that that was better and nicer than the simple one.

Me and my friends tried out the latest version and it didn't work. We turned off our Firewalls and I hosted the serverin my PC. I'm the only one who can connect? Can someone here explain to me? Thanks

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...