Jump to content

chat script not working


Recommended Posts

Hey xD, for a long time i've been wanting to understand Au3's TCP and i havn't rly understood it, but earlier today i read alot in the forum and now i think i've figure'd out how it works but my script ain't working for some reason??

Server:

Dim $user[101]
$user[0] = 1
TCPStartup()
$socket = TCPListen("127.0.0.1",6162)
If $socket = -1 Then
    MsgBox(0,"Error","You fail!")
    Exit
EndIf

While 1
    If $user[$user[0]] = -1 Then
        $user[$user[0]] = TCPAccept($socket)
        If $user[$user[0]] <> -1 Then
            $user[0] = $user[0] +1
            MsgBox(0,"","1") ;I just have this msgbox here so if i can see if it comes this far, sadly he doesn't for some reason.
            TCPSend($user[$user[0]], "Connected!"& @CRLF)
            TCPSend($user[$user[0]], "Welcome to chat server #1"& @CRLF)
        EndIf
    EndIf
    $do = 1
    Do
        $data = TCPRecv($user[$do],1000)
        If $data <> "" Then Send1()
        $do = $do +1
    Until $do >= $user[0]
WEnd

Func Send1()
    $send1 = 1
    Do
        TCPSend($user[$send1],$data)
        $send1 = $send1 +1
    Until $send1 = $user[0]
EndFunc

Client:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <GUIConstants.au3>
#include <EditConstants.au3>    
    
    
TCPStartup()

$socket = TCPConnect("127.0.0.1",6162)
If $socket <= 0 Then 
    MsgBox(0,"", "Couldn't connect to server")
    Exit
EndIf

$NAME = InputBox("Namecheck","Please enter your name before entering.")

GUICreate("WIGGY'S AWESOME MEGA FUCKING IMAB CHAT CLIENT!!!",500,300)
    $chat = GUICtrlCreateEdit("Chat UI loaded ready to go online!"& @CRLF,5,5,490,260)
    GUICtrlCreateInput("",5,265,490)
GUISetState(@SW_SHOW)

While 1
    $gui = GUIGetMsg()
    
    $data = TCPRecv($socket,500)
    If $Data <> "" Then
        MsgBox(0,"","")
        $read = GUICtrlRead($chat)
        GUICtrlSetData($chat,$read & $Data)
    EndIf
    If $gui = $GUI_EVENT_CLOSE Then Exit
WEnd
Link to comment
Share on other sites

Take look at my old post

There you have basic client and small server, change it to suit your need.

As for whats wrong with your script, i dono i got lost figuring out what is "If $user[$user[0]] = -1 Then"

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 
Link to comment
Share on other sites

Hi and Welcome to the forums!!

SWEEEEEEEEDEN!

That's all I wanted to say.

:huh2:

I like sweeeeeeden and swedish pepole, they are much nicer than norweigan pepole.

[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Link to comment
Share on other sites

Hi and Welcome to the forums!!

SWEEEEEEEEDEN!

That's all I wanted to say.

:huh2:

thanks i guess ^^'

Take look at my old post

There you have basic client and small server, change it to suit your need.

As for whats wrong with your script, i dono i got lost figuring out what is "If $user[$user[0]] = -1 Then"

Well, i started just looking at the TCP and took the help client & server, but as i figure'd out it could only handle 1 client... atleast i didn't get it to handle more then 1, so i asked my brother ^^' and he said i should make an array, so "$user" is what ur script call "$ConnectedSocket", "$user[0]" holds how many client we got connected, and then the rest of the faks are holding our connected ip's...

I'm looking at your topic and tried your server & client, but it also only seems to handle 1 client... so i belive i'm in need of more help...

Link to comment
Share on other sites

Posted Image

Look at picture and youl see 3 comps on network connected with driveresial as their identificator (you can put username from your script as identificator and test it, if you have more than 1 comp ofc. :alien: ), i never deleted code for more that one client in that post.

The only thing you need to do in there is the things i suggested in that topic youl need to add code for when the user dcc to remove it from array.

The picture up there is mesedup only becase when i connect 3 clients on my network, they are so fast that printscreen cant get fully refreshed gui in time :huh2:

Edit: and ofc you need to put in clients script the IP adress of server.

Its simplescript that is working with more that 1 client ,nothing else. ;)

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 
Link to comment
Share on other sites

Posted Image

Look at picture and youl see 3 comps on network connected with driveresial as their identificator (you can put username from your script as identificator and test it, if you have more than 1 comp ofc. :alien: ), i never deleted code for more that one client in that post.

The only thing you need to do in there is the things i suggested in that topic youl need to add code for when the user dcc to remove it from array.

The picture up there is mesedup only becase when i connect 3 clients on my network, they are so fast that printscreen cant get fully refreshed gui in time :huh2:

Edit: and ofc you need to put in clients script the IP adress of server.

Its simplescript that is working with more that 1 client ,nothing else. ;)

umm, honestly, i didn't understand a single fuck of that..

anyway i made a new script with ur #2 link... works fine ^^' but for some reason asoon as i change my ip from "127.0.0.1" to my int ip "**.**.***.**" the script doesn't even want to start anymore...

Server:

Example()

Func Example()
    TCPStartup()
    
    $socket = TCPListen("127.0.0.1",6162)

    If $socket = -1 Then Exit
        
    Dim $user[101]
    $user[0] = 1
    $do = 1
    Do
        $user[$do] = -1
        $do = $do +1
    Until $do = 101
    $num = 0
    
    Do
        $user[$user[0]] = TCPAccept($socket)
    Until $user[$user[0]] <> -1
    $num = $num & $user[0] & ","
    $user[0] = $user[0] +1


While 1
    $user[$user[0]] = TCPAccept($socket)
    If $user[$user[0]] <> -1 Then
        $num = $num & $user[0] & ","
        $user[0] = $user[0] +1
    EndIf

    $recv = TCPRecv($user[$num], 2048)

    If $recv <> "" Then TCPSend($user[$num],$recv)
WEnd
    If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)

    TCPShutdown()
EndFunc   
Func SocketToIP($SHOCKET)
    Local $sockaddr, $aRet

    $sockaddr = DllStructCreate("short;ushort;uint;char[8]")

    $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
            "ptr", DllStructGetPtr($sockaddr), "int*", DllStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
        If Not @error Then $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf

    $sockaddr = 0

    Return $aRet
EndFunc   ;==>SocketToIP

Client:

#include <GUIConstantsEx.au3>



;==============================================
;==============================================
;CLIENT! Start Me after starting the SERVER!!!!!!!!!!!!!!!
;==============================================
;==============================================
$name = "Wiggy boy"
Example()

Func Example()
    TCPStartup()

    ; Initialize a variable to represent a connection
    ;==============================================
    $socket = -1

    ;Attempt to connect to SERVER at its IP and PORT 33891
    ;=======================================================
    $socket = TCPConnect("127.0.0.1",6162)

    ; If there is an error... show it
    If @error Then
        MsgBox(4112, "Error", "TCPConnect failed with WSA error: " & @error)
        ; If there is no error loop an inputbox for data
        ;   to send to the SERVER.
    Else
        GUICreate("Chatit", 500, 300)
        $edit = GUICtrlCreateEdit("",5,5,490, 230)
        $input = GUICtrlCreateInput("",5,240,400,50)
        GUICtrlCreateButton("Send",410,235, 100,100)
        GUISetState(@SW_SHOW)
        
        While 1
            $gui = GUIGetMsg()
            $recdata = TCPRecv($socket,2000)
            If $recdata <> "" Then
                $data = GUICtrlRead($edit)
                GUICtrlSetData($edit, $data & $recdata)
            EndIf
            Select
            Case $gui = $input
                $inputdata = GUICtrlRead($input)
                TCPSend($socket,$name &" says:"& $inputdata & @CRLF)
                GUICtrlSetData($input,"")
            EndSelect
            If $gui = $GUI_EVENT_CLOSE Then Exit
        WEnd
    EndIf
EndFunc   ;==>Example
Link to comment
Share on other sites

Packet Forwarding (Router -> Server.exe)

Link to comment
Share on other sites

Ok guys, i'm using a version my brother made me, it's working rly fine, i'm only having 1 problem...

when the server sends "~name" the client returns it's name, but the server ain't able to add it on the list for some reason O.o? or atleast nothing shows up on the list

Func checkConnect()
    $sockettemp = TCPAccept($socket)
    if $sockettemp <> -1 Then
        $user[0][0] = $user[0][0] +1
        $user[$user[0][0]][0] = $sockettemp ;adding the connected socket to an array
        TCPSend($user[$user[0][0]][0],"~name") ;sends "~name" to the new connectedsocket
        Name()
    EndIf
EndFunc

Func Name()
    $data = ""
    While 1
        $data = TCPRecv($user[$user[0][0]][0],1000) ;waiting for data
        If $data <> "" Then
            $data = $user[$user[0][0]][1] ;adding name to connectedsocket's array

            GUICtrlSetData($list,$user[$user[0][0]][1]) ;adding connectedsocket's name to the connected's list
            start()
        EndIf
    WEnd
EndFunc

should work fine huh? well, for some reason it doesn't...

Link to comment
Share on other sites

Ok guys, i'm using a version my brother made me, it's working rly fine, i'm only having 1 problem...

when the server sends "~name" the client returns it's name, but the server ain't able to add it on the list for some reason O.o? or atleast nothing shows up on the list

Func checkConnect()
    $sockettemp = TCPAccept($socket)
    if $sockettemp <> -1 Then
        $user[0][0] = $user[0][0] +1
        $user[$user[0][0]][0] = $sockettemp ;adding the connected socket to an array
        TCPSend($user[$user[0][0]][0],"~name") ;sends "~name" to the new connectedsocket
        Name()
    EndIf
EndFunc

Func Name()
    $data = ""
    While 1
        $data = TCPRecv($user[$user[0][0]][0],1000) ;waiting for data
        If $data <> "" Then
            $data = $user[$user[0][0]][1] ;adding name to connectedsocket's array

            GUICtrlSetData($list,$user[$user[0][0]][1]) ;adding connectedsocket's name to the connected's list
            start()
        EndIf
    WEnd
EndFunc

should work fine huh? well, for some reason it doesn't...

lol, no worries i found the problem...

$data = $user[$user[0][0]][1] ;adding name to connectedsocket's array (fail, should be like...)

$user[$user[0][0]][1] = $data ;adding name to connectedsocket's array
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...