Sign in to follow this  
Followers 0
Nuffilein805

another chat/client (up to 100 clients)

43 posts in this topic

#1 ·  Posted (edited)

ok, here's what i made again

it's just a little chatprog for up to 100 clients

i took some parts of the help-file to make it fit to what i need

well, it works, but the servers quite slow, so i suggest you to not write messages from here

anyway here's the code:

server:

;chat with 100 clients
;made by Marten Zimmermann

#include <guiconstants.au3>
global $g_port = 34000
Global $g_IP = @IPADDRESS1
Global $MainSocket
dim $ConnectedSocket[101]
dim $recv[101]
dim $user[101]
dim $pw[101]
for $i = 1 to 100 step 1
    $ConnectedSocket[$i] = -1
Next


tcpstartup()

$MainSocket = TCPListen($g_IP, $g_port,  100 )
If $MainSocket = -1 Then Exit
$RogueSocket = -1

$main = GUICreate ("Chat Server", 500, 200)
$input = GUICtrlCreateInput ("", 10, 10, 280)
$edit = GUICtrlCreateEdit ("", 10, 40, 280, 110, $ES_READONLY + $ES_MULTILINE + $WS_VSCROLL + $ES_AUTOVSCROLL)
$userlist = GUICtrlCreateEdit ("Server", 300, 10, 190, 180, $ES_READONLY + $ES_MULTILINE + $WS_VSCROLL + $ES_AUTOVSCROLL + $ES_WANTRETURN)
$send = GUICtrlCreateButton ("Send", 10, 160, 280, 20, $BS_DEFPUSHBUTTON)
GUICtrlSetState ($input, $GUI_FOCUS)
GUISetState()

$users = "Server" & @CRLF
$users1 = $users
while 1
    $msg = GUIGetMsg()
    if $msg = $GUI_EVENT_CLOSE Then
        for $i = 1 to 100 step 1
            if $ConnectedSocket[$i] <> -1 Then
                tcpsend ($ConnectedSocket[$i], "~~bye")
            EndIf
        Next
        tcpshutdown()
        Exit
    EndIf
    
    if $msg = $send Then
        if GUICtrlRead ($input) <> "" Then
            for $i = 1 to 100 step 1
                if $ConnectedSocket[$i] <> -1 Then
                    tcpsend ($ConnectedSocket[$i], "Server: " & guictrlread ($input))
                EndIf
            Next
            GUICtrlSetData ($edit, guictrlread($input) & @crlf & guictrlread ($edit))
            GUICtrlSetData ($input, "")
        EndIf
    EndIf
    
    for $i = 1 to 100 step 1
        if $ConnectedSocket[$i] <> -1 Then
            $recv[$i] = tcprecv($ConnectedSocket[$i], 512)
            $err = @error
            if $recv[$i] = "~~bye" Then
                $ConnectedSocket[$i] = -1
                tcpclosesocket($ConnectedSocket[$i])
                $test = $user[$i] & "|"
                $users = StringReplace ($users, $test, "")
                GUICtrlSetData ($userlist, $users)
                if StringReplace ($users1, "|", @crlf) <> $users Then
                    for $j = 1 to 100
                        if $ConnectedSocket[$i] <> -1 Then
                            tcpsend($ConnectedSocket[$i], "~~users:" & $users1)
                        EndIf
                    Next
                EndIf
            EndIf
            
            if StringInStr ($recv[$i], "~~us:") Then
                $user[$i] = stringtrimleft ($recv[$i], 5)
                $test = $user[$i] & @CRLF
                if not StringInStr ($users, $test) Then
                    if iniread ("chat.ini", "Users", $user[$i], "") <> "" Then
                        tcpsend ($ConnectedSocket[$i], "~~password")
                    Else
                        tcpsend ($ConnectedSocket[$i], "~~accepted")
                        USERLIST()
                    EndIf
                EndIf
            EndIf
            
            if StringInStr ($recv[$i], "~~pw:") Then
                $pw[$i] = StringTrimLeft ($recv[$i], 5)
                if iniread ("chat.ini", "Users", $user[$i], "") = $pw[$i] then
                    tcpsend($ConnectedSocket[$i], "~~accepted")
                    USERLIST()
                Else
                    tcpsend ($ConnectedSocket[$i], "~~password")
                EndIf
            EndIf
            
            if $recv[$i] <> "" and $err = 0 and $recv[$i] <> "~~bye" and not StringInStr($recv[$i], "~~us:") and not StringInStr($recv[$i], "~~pw:") Then
                for $j = 1 to 100 step 1
                    if $ConnectedSocket[$j] <> -1 Then
                        tcpsend($ConnectedSocket[$j], $user[$i] & ": " & $recv[$i])
                    EndIf
                Next
                GUICtrlSetData ($edit, $user[$i] & ": " & $recv[$i] & @crlf & guictrlread($edit))
            EndIf
            
        Else
            $ConnectedSocket[$i] = tcpaccept($MainSocket)
        EndIf
    Next
WEnd

func USERLIST()
    for $i = 1 to 100 step 1
        if $ConnectedSocket[$i] <> -1 Then
            $test = $user[$i] & @CRLF
            if not stringinstr ($users, $test) Then
                $users = $users & $user[$i] & @CRLF
            EndIf
        Elseif $ConnectedSocket[$i] = -1 Then
        ;$users = stringreplace ($users, $user[$i], "")
        EndIf        
    Next
    
    $users1 = StringReplace ($users, @CRLF , "|")
    for $i = 1 to 100 step 1
        if $ConnectedSocket[$i] <> -1 Then
            tcpsend ($ConnectedSocket[$i], "~~users:" & $users1)
        EndIf
    Next
    
    GUICtrlSetData ($userlist, $users)
EndFunc

and the client:

;client for 100client-server
;made by Marten Zimmermann

#include <guiconstants.au3>
global $connect_port = 34000

$ip = InputBox ("IP-Adress", "Gimme the IP-Adress of the Server", @IPAddress1)

tcpstartup()

$socket = tcpconnect ($ip, $connect_port)
if $socket = -1 then
    Exit
EndIf

Do
    Do
        $user = InputBox ("Connected", "Connection established please type in your desired Username")
        if @error = 1 Then
            $end = msgbox (4, "End client", "Sure you want to exit?")
            if $end = 6 Then
                Exit
            EndIf
        EndIf
    until StringLen ($user) > 3 and StringLen ($user) < 12
    tcpsend ($socket, "~~us:" & $user)

    Do
        $recv = tcprecv ($socket, 512)
        if $recv = "~~password" Then
            $pw = InputBox ("Password required", "Please type in the password for " & $user, "", "*")
            tcpsend ($socket, "~~pw:" & $pw)
        EndIf
    until $recv = "~~accepted" or $recv = "~~rejected" or @error = 1
until $recv = "~~accepted" or $recv = "~~rejected"

if $recv = "~~rejected" Then
    msgbox (0, "Failure", "Maximum number of users reached or Server currently not availible, please try again later")
    Exit
EndIf

$main = GUICreate ("Chat Client - Connection established", 500, 200)
$input = GUICtrlCreateInput ("", 10, 10, 280)
$edit = GUICtrlCreateEdit ("", 10, 40, 280, 110, $ES_READONLY + $ES_MULTILINE + $WS_VSCROLL + $ES_AUTOVSCROLL)
$userlist = GUICtrlCreateEdit ("", 300, 10, 190, 180, $ES_READONLY + $ES_MULTILINE + $WS_VSCROLL + $ES_AUTOVSCROLL)
$send = GUICtrlCreateButton ("Send", 10, 160, 280, 20, $BS_DEFPUSHBUTTON)
GUICtrlSetState ($input, $GUI_FOCUS)
GUISetState()

while 1
    $msg = GUIGetMsg()
    if $msg = $GUI_EVENT_CLOSE Then
        tcpsend ($socket, "~~bye")
        sleep (5000)
        tcpshutdown()
        Exit
    EndIf
    
    if $msg = $send Then
        if guictrlread ($input) <> "" Then
            tcpsend ($socket, guictrlread($input))
            GUICtrlSetData ($input, "")
        EndIf
    EndIf
    
    $recv = tcprecv ($socket, 512)
    $err = @error
    
    if $recv = "~~bye" Then
        GUICtrlSetData ($edit, "~~Connection Lost" & @CRLF & GUICtrlRead($edit))
        tcpshutdown()
        ExitLoop
    EndIf
    
    if $recv <> "" and $err = 0 and not StringInStr ($recv, "~~users:") Then
        GUICtrlSetData ($edit, $recv & @CRLF & GUICtrlRead($edit))
    EndIf
    
    if StringInStr($recv, "~~users:") then
        $users = StringTrimLeft ($recv, 8)
        $users = StringReplace ($users, "|", @crlf)
        GUICtrlSetData ($userlist, $users)
    EndIf
    
WEnd


Func OnAutoItExit()
   TCPSend( $socket, "~~bye" )
   TCPCloseSocket( $socket )
   TCPShutDown()
EndFunc

now there's just 1 question left: who needs this?

i thought about making a portal for some games with this, but don't know by now what to do.

maybe you got some suggestions

ps: if you use this code for 1 of your project please tell me

updated the server (the last 1 was downloaded 37 times)

31.10.05

updated the server (last 1 downloaded 22 times)

updated the client (last 1 downloaded 55 times)

new functions:

~~pm:USERNAME TEXT (for private message)

~~kick:USERNAME (to kick a specific user - works at the server only)

refreshing the userlist works now

client3.au3

server3.au3

Edited by Nuffilein805

Share this post


Link to post
Share on other sites



@Nuffilein805, interesting. I will have to give it a try..

Cheers.. B)

Share this post


Link to post
Share on other sites

Hi, i could run this chat-server for you if you want.

Email me on vic@taftea.se for further information.

Cheers

Share this post


Link to post
Share on other sites

quik note, make it infinite servers or possible like 30000, something beyond 100 because there are more users then 100 on this forum, just a thought B)

Share this post


Link to post
Share on other sites

Hi Thanks for this code!

It works great! B)

I've find a small bug...

If I type a message, and I press on {enter} it works... (client)

But the server i must click on the 'Send' button.

How must I fix that? :$

Share this post


Link to post
Share on other sites

@autoitsmith: ok i'll upgrade it when i'm home today, now i just got internet, but i don't know if a server can handle 30000 clients

@yosemite: it should work because send is the default-button

if your running the server on a slow pc (233MHz) it can take up to 5min till the message is sent, i discovered that recently, trying to fix this as well

Share this post


Link to post
Share on other sites

Hey AutoIt Smith... if you want it to run more then 100 clients, then just find this little line here

for $i = 1 to 100 step 1

and change 100 to whatever number you want to be the highest, then find this little line here

$MainSocket = TCPListen($g_IP, $g_port,  100 )

and change the 100 here to the same number you changed the 100 in the last line to....

im not 100% sure, but im pretty sure this is what does it

Share this post


Link to post
Share on other sites

If that is the case, the max number of clients should be a variable, not a magic number...

$MAX_CLIENTS = 100 maybe?

Share this post


Link to post
Share on other sites

I have a litte request...

I want make a function that if I type 'cdtray' (server) that all clients open the cdtray...

How can I make this? :$

Can I use this function?

if $msg = $send Then
        if guictrlread ($input) <> "" Then
            tcpsend ($socket, guictrlread($input))
            GUICtrlSetData ($input, "")
        EndIf
    EndIf

Thanks

YoseMite

Share this post


Link to post
Share on other sites

I have a litte request...

I want make a function that if I type 'cdtray' (server) that all clients open the cdtray...

How can I make this? :$

Can I use this function?

if $msg = $send Then
        if guictrlread ($input) <> "" Then
            tcpsend ($socket, guictrlread($input))
            GUICtrlSetData ($input, "")
        EndIf
    EndIf

Thanks

YoseMite

In what kind of environment would this be a desirable activity to globally interrupt multiple people by ejecting their CD tray or other remotely controlled activities? Can you please take out life insurance - you may need it if your deployed nasty actually works...

Damn skript kiddies! B)

Share this post


Link to post
Share on other sites

Lol that as a hidden function would be funny! People are chatting and all of a your cdtray opens^^

No, but your script is top!

Share this post


Link to post
Share on other sites

Lol that as a hidden function would be funny! People are chatting and all of a your cdtray opens^^

No, but your script is top!

Maybe funny if the payload is opening the CD tray within earshot, but what if it was something more sinister across the Internet running on a raft of zombie computers? See where I'm coming from?

Share this post


Link to post
Share on other sites

Yes, i know what yuo mean and i am aware of the abuse of autoit to write "evil" tools.

I don't like the idea of remotecontrolling without the user knowing it, but only something like open cdtray?^^

Gets kinda annoying as soon as everybody knows the command^^

Share this post


Link to post
Share on other sites

first of all, i don't like this kinda functions included, though it is possible

i won't tell you how, you need to find that out by yourself

the next functions to be included are:

kick by admin(by now just server)

private message

thats all i'm going to add by now

if you need more functions, just tell me (if they are useful) i'm going to include them as well

Share this post


Link to post
Share on other sites

a function like update the user list would be so great BTW ty for the great program


My Scripts:Radioblog Club Music DownloaderOther stuff:Fun movieIm serious read the help file it helps :PFight 'Till you drop. Never stop, You Cant give up. Til you reach the top Fight! you’re the best in town Fight!

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

If you want function writing just goto my RS 1.0 thread. I made a hybrid one way server for controlling aspects of the home computers environment. I'm sure it will be helpful. Just search Remote Server 1.0

\EDIT LINK http://www.autoitscript.com/forum/index.ph...topic=15408&hl=

Edited by AutoIt Smith

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  
Followers 0